jdbc簡介
jdbc全稱為:java data base connectivity (java數(shù)據(jù)庫連接),可以為多種數(shù)據(jù)庫提供填統(tǒng)一的訪問。jdbc是sun開發(fā)的一套數(shù)據(jù)庫訪問編程接口,是一種sql級的api。它是由java語言編寫完成,所以具有很好的跨平臺特性,使用jdbc編寫的數(shù)據(jù)庫應(yīng)用程序可以在任何支持java的平臺上運(yùn)行,而不必在不同的平臺上編寫不同的應(yīng)用程序。
jdbc編程步驟
(1)加載驅(qū)動程序:
下載驅(qū)動包 : http://dev.mysql.com/downloads/connector/j/
解壓,得到 jar文件。將該文件復(fù)制到j(luò)ava工程目錄java resources/libraries/ 下,→ buildpath 。
(2)獲得數(shù)據(jù)庫連接
(3)創(chuàng)建statement對象:
(4)向數(shù)據(jù)庫發(fā)送sql命令
(5)處理數(shù)據(jù)庫的返回結(jié)果(resultset類)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
package com.baidu.emp.jdbctest; import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.statement; import com.mysql.jdbc.driver; /** * 開始使用jdbc連接數(shù)據(jù)庫 * @author admin * */ public class test001 { public static void main(string[] args) throws exception { /** * 加載驅(qū)動 */ // 方法一: /* * import java.sql.drivermanager; import com.mysql.jdbc.driver; */ // driver driver = new driver(); // drivermanager.registerdriver(driver); // 方法二:(推薦使用) class.forname("com.mysql.jdbc.driver"); /** * 創(chuàng)建鏈接 */ string url = "jdbc:mysql://localhost:3306/testjdbc"; string user = "root"; string password = "root"; connection connection = drivermanager.getconnection(url, user, password); // 創(chuàng)建statement對象 statement statement = connection.createstatement(); /** * 執(zhí)行sql,獲取結(jié)果集 */ string sql = "select * from test01"; resultset result = statement.executequery(sql); // 遍歷結(jié)果集 while (result.next()) { string name = result.getstring("name"); int id = result.getint("id"); system.out.println(name + "\t" + id); } /** * 關(guān)閉鏈接,釋放資源 */ result.close(); statement.close(); connection.close(); } } |
防止sql注入改用preparestatement
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
package com.boya.emp.jdbctest; import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; /** * sql注入,使用preparestatement對象進(jìn)行預(yù)編譯 * @author admin * */ public class test002 { public static void main(string[] args) throws exception { /** * 加載驅(qū)動 */ class .forname( "com.mysql.jdbc.driver" ); /** * 創(chuàng)建鏈接 */ string url = "jdbc:mysql://localhost:3306/testjdbc" ; string user = "root" ; string password = "root" ; connection connection = drivermanager.getconnection(url, user, password); // 寫sql string sql = "select * from test01 where id = ?" ; //創(chuàng)建statement對象,預(yù)編譯 preparedstatement statement = connection.preparestatement(sql); //設(shè)置參數(shù) statement.setint( 1 , 2 ); /** * 執(zhí)行sql,獲取結(jié)果集 */ resultset result = statement.executequery(); // 遍歷結(jié)果集 while (result.next()) { string name = result.getstring( "name" ); int id = result.getint( "id" ); system.out.println(name + "\t" + id); } /** * 關(guān)閉鏈接,釋放資源 */ result.close(); statement.close(); connection.close(); } } |
進(jìn)行代碼優(yōu)化,設(shè)置配置文件,工具類,實現(xiàn)增刪該查
增加配置文件方便修改數(shù)據(jù)庫,用戶登錄。。。
jdbc.properties(配置文件名)
1
2
3
4
|
drivername=com.mysql.jdbc.driver url=jdbc:mysql: //localhost:3306/testjdbc username=root password=root |
注意寫配置文件時中間不可以有空格,引號之類的
工具類:增強(qiáng)了代碼的復(fù)用性
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
package com.baidu.emp.utils; import java.io.inputstream; import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.util.properties; import org.junit.test; public class jdbcutils { static string driverclassname; static string url; static string user; static string password; static { // 創(chuàng)建配置文件對象 properties properties = new properties(); // 加載配置文件輸入流 inputstream inputstream = jdbcutils. class .getclassloader().getresourceasstream( "jdbc.properties" ); // 重新加載配置文件 try { properties.load(inputstream); // 獲取配置文件的值 driverclassname = properties.getproperty( "drivername" ); url = properties.getproperty( "url" ); user = properties.getproperty( "username" ); password = properties.getproperty( "password" ); class .forname(driverclassname); } catch (exception e) { // 拋出異常 throw new runtimeexception(e); } } /** * 獲取連接 */ @test public void testname() throws exception { system.out.println(driverclassname); } public static connection getconnection() { connection connection = null ; try { connection = drivermanager.getconnection(url, user, password); } catch (sqlexception e) { // 拋出異常 throw new runtimeexception(e); } return connection; } /** * 關(guān)閉鏈接,釋放資源 */ public static void close(connection connection, preparedstatement statement, resultset resultset) { try { if (resultset != null ) { resultset.close(); } resultset = null ; // 垃圾及時清除 //注意,不要弄成死循環(huán) close(connection, statement); } catch (sqlexception e) { throw new runtimeexception(e); } } /** * 增刪改釋放資源 */ public static void close(connection connection, preparedstatement statement) { try { if (connection != null ) { connection.close(); } connection = null ; if (statement != null ) { statement.close(); } statement = null ; } catch (sqlexception e) { throw new runtimeexception(e); } } } |
測試增刪改查:
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
package com.baidu.emp.jdbctest; import java.sql.connection; import java.sql.preparedstatement; import java.sql.resultset; import org.junit.after; import org.junit.before; import org.junit.test; import com.baidu.emp.utils.jdbcutils; /** * 使用jdbcutils連接數(shù)據(jù)庫進(jìn)行增刪改查 * * @author admin * */ public class test003 { // 初始化值 connection connection = null ; preparedstatement statement = null ; resultset result = null ; @before public void start() throws exception { // 創(chuàng)建鏈接 connection = jdbcutils.getconnection(); system.out.println( "創(chuàng)建鏈接" ); } @after public void end() throws exception { // 關(guān)閉鏈接 jdbcutils.close(connection, statement, result); system.out.println( "關(guān)閉鏈接" ); } /** *插入數(shù)據(jù) * @throws exception */ @test public void add() throws exception { string sql = "insert into test01 values(null,?)" ; statement = connection.preparestatement(sql); statement.setstring( 1 , "李四" ); int result = statement.executeupdate(); if (result!= 0 ) { system.out.println( "添加成功" ); } } /** * 刪除數(shù)據(jù) * @throws exception */ @test public void del() throws exception { string sql = "delete from test01 where id =?" ; statement = connection.preparestatement(sql); statement.setint( 1 , 3 ); int result = statement.executeupdate(); if (result!= 0 ) { system.out.println( "刪除成功" ); } } /** * 修改數(shù)據(jù) * @throws exception */ @test public void change() throws exception { string sql = "update test01 set name = ? where id = ?" ; statement = connection.preparestatement(sql); statement.setstring( 1 , "張飛" ); statement.setint( 2 , 2 ); int result = statement.executeupdate(); if (result!= 0 ) { system.out.println( "修改成功" ); } } /** * 查詢?nèi)繑?shù)據(jù) * @throws exception */ @test public void findall() throws exception { string sql = "select id , name from test01" ; statement = connection.preparestatement(sql); result = statement.executequery(); if (result.next()) { system.out.println( "查詢成功" ); } } /** * 條件查詢數(shù)據(jù) * @throws exception */ @test public void findone() throws exception { string sql = "select id , name from test01 where id = ?" ; statement = connection.preparestatement(sql); statement.setint( 1 , 2 ); result = statement.executequery(); if (result.next()) { system.out.println( "查詢成功" ); } } } |
以上就是相關(guān)知識以及相關(guān)代碼,感謝大家對服務(wù)器之家的支持。