本來不想寫這么簡單人文章,在百度上搜索我這個標題,完全符合標題的一大堆。但我按照那些文章?lián)v鼓了很久,就是不行。
我的環(huán)境:MySQL:mysql-essential-5.1.51-win32
jdbc驅(qū)動
Eclipse:任意版本,免費的,可以百度的到。
1.MySQL安裝,不會的朋友可以看這里的圖文講解http://www.ythuaji.com.cn/article/27551.html,很詳細。
下面來創(chuàng)建一個數(shù)據(jù):
1
2
3
4
|
mysql>CREATE DATABASE test; //創(chuàng)建一個數(shù)據(jù)庫 mysql>use test; //指定test為當前要操作的數(shù)據(jù)庫 mysql>CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); //創(chuàng)建一個表user,設置兩個字段。 mysql>INSERT INTO user VALUES( 'huzhiheng' , '123456' ); //插入一條數(shù)據(jù)到表中 |
2.打開Eclipse,創(chuàng)建一個項目(my),
操作:右鍵點擊my--->build Path--->add external Archiver...選擇jdbc驅(qū)動,點擊確定。
3.驅(qū)動已經(jīng)導入,下面我們來寫一個程序驗證一下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
import java.sql.*; public class MysqlJdbc { public static void main(String args[]) { try { Class.forName( "com.mysql.jdbc.Driver" ); //加載MYSQL JDBC驅(qū)動程序 //Class.forName( "org.gjt.mm.mysql.Driver" ); System. out .println( "Success loading Mysql Driver!" ); } catch (Exception e) { System. out .print( "Error loading Mysql Driver!" ); e.printStackTrace(); } try { Connection connect = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test" , "root" , "198876" ); //連接URL為 jdbc:mysql//服務器地址/數(shù)據(jù)庫名 ,后面的2個參數(shù)分別是登陸用戶名和密碼 System. out .println( "Success connect Mysql server!" ); Statement stmt = connect .createStatement(); ResultSet rs = stmt.executeQuery( "select * from user" ); // user 為你表的名稱 while (rs. next ()) { System. out .println(rs.getString( "name" )); } } catch (Exception e) { System. out .print( "get data error!" ); e.printStackTrace(); } } } |
點擊運行程序:
1
2
3
|
Success loading Mysql Driver! Success connect Mysql server! huzhiheng |
出現(xiàn)上面結果,說明你連接數(shù)據(jù)庫成功。
4.可以查看到MySQL里面的內(nèi)容,那我們是不是想往MySQL中插入數(shù)據(jù)呢。
下面的例子,往MySQL的user表中插入100條數(shù)據(jù)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
import java.sql.*; public class Myjproject { public static void main(String args[]) { try { Class.forName( "com.mysql.jdbc.Driver" ); //加載MYSQL JDBC驅(qū)動程序 //Class.forName( "org.gjt.mm.mysql.Driver" ); System. out .println( "Success loading Mysql Driver!" ); } catch (Exception e) { System. out .print( "Error loading Mysql Driver!" ); e.printStackTrace(); } try { Connection connect = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test" , "root" , "198876" ); int num=100; PreparedStatement Statement= connect .prepareStatement( "INSERT INTO user VALUES(?,?)" ); for ( int i=0;i<num;i++) //定義個100次的循環(huán),往表里插入一百條信息。 { Statement.setString(1, "chongshi" +i); Statement.setString(2, "bo" +i); Statement.executeUpdate(); } // } catch (ClassNotFoundException e) { // TODO Auto-generated catch block // System. out .println( "An error has occurred:" +e.toString()); // e.printStackTrace(); }catch(SQLException e) { } } } |
5.下面我們打開MySQL數(shù)據(jù)庫進行查看
1
2
3
4
5
|
mysql> show tatabases; //查看所數(shù)據(jù)庫 mysql> use test; //使test為當前要操作的數(shù)據(jù)庫 mysql> show tables; //查看當前數(shù)據(jù)庫的所有表 view sourceprint? mysql> select * from user ; //查看當前表( user )的所有信息 |
注意:如果不能正常連接你的數(shù)據(jù)庫,請檢查你代碼中,驅(qū)動、用戶名、密碼、表等信息是否對應無誤,不要把別人的代碼直接復制過來,看也不看就用。