1. ?下載 ?如:mysql-connector-java-5.1.22.zip ?解壓獲取 jar 連接包。
2. ? 引入項目:? ? ?右鍵項目名--->Build Path—>Configure Build Path... ?選擇 ?Libraries 然后再選擇 右邊
? ? ? ? ? ? ? ? ? ? ? ? 第二個選項Add External Jars 導入 mysql -.jar
3. ? 新建數據表:
? ?
CREATE TABLE `user` ( `ID` int (11 ) NOT NULL AUTO_INCREMENT, `Name` varchar( 32 ) NOT NULL, `Age` int (11 ) NOT NULL, `AddTime` datetime NOT NULL, PRIMARY KEY (`ID`) ) ENGINE =InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
?
4. 測試程序:
? ? ?
package App1.Rhythmk.com; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import com.mysql.jdbc.Statement; /* * * 建表腳本 CREATE TABLE `user` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `Name` varchar(32) NOT NULL, `Age` int(11) NOT NULL, `AddTime` datetime NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8; */ public class ThirdMysql { // final 類似 .NET 中 const private static final String DatabaseConURL = "jdbc:mysql://127.0.0.1:3306/rhythmkdb" ; /** * rhythmK * 1.右鍵項目名--->Build Path—>Configure Build Path... 會彈出來一個框 在那四個選項選擇 * Libraries 然后再選擇 右邊 第二個選項Add External Jars */ public static void main(String[] args) { // TODO Auto-generated method stub Test(); ExcuteByParam(); Add(); Update(); Select(); Del(); } public static void Test() { try { Connection con = null ; Class.forName( "com.mysql.jdbc.Driver" ).newInstance(); con = DriverManager .getConnection(DatabaseConURL, "root", "wangkun" ); Msg( "Mysql 連接成功。" ); } catch (Exception ex) { Msg(ex.toString()); } } public static void Add() { String sql = "Insert into `user` (Name ,Age, Addtime ) " ; sql += " values ('rhythmk',21,'2012-1-1') " ; try { Connection con = null ; Class.forName( "com.mysql.jdbc.Driver" ).newInstance(); con = DriverManager .getConnection(DatabaseConURL, "root", "wangkun" ); java.sql.Statement statement = con.createStatement(); statement.executeUpdate(sql); ResultSet result = statement .executeQuery( "select LAST_INSERT_ID() " ); int resultID = 0 ; if (result.next()) { // 此出索引不是從0開始 .. resultID = result.getInt(1 ); } Msg( "Add 數據庫得到返回ID" + resultID); } catch (Exception ex) { Msg(ex.toString()); } } public static void Update() { String sql = " Update `user` set Name= 'rhythmk.com' where ID<3 " ; try { Connection con = null ; Class.forName( "com.mysql.jdbc.Driver" ).newInstance(); con = DriverManager .getConnection(DatabaseConURL, "root", "wangkun" ); java.sql.Statement statement = con.createStatement(); int resultID = statement.executeUpdate(sql); Msg( "Update 數據庫得到返回ID" + resultID); } catch (Exception ex) { Msg(ex.toString()); } } public static void Del() { String sql = " Delete from `user` where ID=3 " ; try { Connection con = null ; Class.forName( "com.mysql.jdbc.Driver" ).newInstance(); con = DriverManager .getConnection(DatabaseConURL, "root", "wangkun" ); java.sql.Statement statement = con.createStatement(); int resultID = statement.executeUpdate(sql); Msg( "Del 數據庫得到返回ID" + resultID); } catch (Exception ex) { Msg(ex.toString()); } } public static void Select() { String sql = " Select ID,Name from `user` " ; try { Connection con = null ; Class.forName( "com.mysql.jdbc.Driver" ).newInstance(); con = DriverManager .getConnection(DatabaseConURL, "root", "wangkun" ); java.sql.Statement statement = con.createStatement(); ResultSet result = statement.executeQuery(sql); String resultStr = "" ; while (result.next()) { resultStr += "\r\n ID=" + result.getString(1 ); resultStr += ", Name=" + result.getString("Name" ); } Msg(resultStr); // statement.executeUpdate(sql); } catch (Exception ex) { Msg(ex.toString()); } } public static void ExcuteByParam() { String sql = "select * from `user` where Name =?" ; try { Connection con = null ; Class.forName( "com.mysql.jdbc.Driver" ).newInstance(); con = DriverManager .getConnection(DatabaseConURL, "root", "wangkun" ); java.sql.PreparedStatement preparedsm = con.prepareStatement(sql); preparedsm.setString( 1, "rhythmk" ); ResultSet result = preparedsm.executeQuery(); String resultStr = "" ; while (result.next()) { resultStr += "\r\n ID=" + result.getString(1 ); resultStr += ", Name=" + result.getString("Name" ); } Msg( "ExcuteByParam:\r\n" + resultStr); // statement.executeUpdate(sql); } catch (Exception ex) { Msg(ex.toString()); } } public static void Msg(String msg) { System.out.println(msg); } }
?
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
