JDBC(Java Data Base Connectivity,Java數(shù)據(jù)庫(kù)連接)是一種用于執(zhí)行SQL語(yǔ)句的Java API,可以為多種關(guān)系數(shù)據(jù)庫(kù)提供統(tǒng)一訪問(wèn),它由一組用Java語(yǔ)言編寫的類和接口組成。JDBC提供了一種基準(zhǔn),據(jù)此可以構(gòu)建更高級(jí)的工具和接口,使數(shù)據(jù)庫(kù)開發(fā)人員能夠編寫數(shù)據(jù)庫(kù)應(yīng)用程序。 JDBC并不能直接訪問(wèn)數(shù)據(jù)庫(kù),需要借助于數(shù)據(jù)庫(kù)廠商提供的JDBC驅(qū)動(dòng)程序。
數(shù)據(jù)庫(kù)連接
如果要在Java訪問(wèn)數(shù)據(jù)庫(kù),首先要加載一個(gè)數(shù)據(jù)庫(kù)驅(qū)動(dòng),數(shù)據(jù)庫(kù)驅(qū)動(dòng)只需要在第一次訪問(wèn)時(shí)加載一次。然后再每次訪問(wèn)數(shù)據(jù)庫(kù)時(shí)創(chuàng)建一個(gè)Connection實(shí)例,獲取數(shù)據(jù)庫(kù)連接,這樣就可以執(zhí)行操作數(shù)據(jù)庫(kù)的SQL語(yǔ)句。最后用完后釋放掉數(shù)據(jù)庫(kù)的連接。
數(shù)據(jù)庫(kù)驅(qū)動(dòng)類
不同的數(shù)據(jù)庫(kù)實(shí)現(xiàn)JDBC接口不同,所以就產(chǎn)生了不同的數(shù)據(jù)庫(kù)驅(qū)動(dòng)包。驅(qū)動(dòng)包就包含一些負(fù)責(zé)數(shù)據(jù)庫(kù)連接的類,把我們要操作的SQL語(yǔ)句傳遞到里面去。我的PC用的是SQL2012,所以我們要去這里http://www.microsoft.com/zh-cn/search/DownloadResults.aspx?q=jdbc下載驅(qū)動(dòng)
下完后在新建的java_project導(dǎo)入驅(qū)動(dòng)包
右擊選中項(xiàng)目>>Build Path >>Add External Archives... 選中下載解壓的文件
導(dǎo)入成功后的項(xià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
32
|
package com.Project_DataBase01; import java.sql.Connection; import java.sql.DriverManager; public class SelectQuery { private Connection conn; /* * 創(chuàng)建一個(gè)返回Connection的方法 */ public Connection getConnection(){ try { Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" ); System.out.println( "數(shù)據(jù)庫(kù)驅(qū)動(dòng)加載成功" ); conn=DriverManager.getConnection( "jdbc:sqlserver://localhost:1433;DatabaseName=java_conn_test" , "sa" , "123456" ); if (conn== null ){ System.out.println( "數(shù)據(jù)庫(kù)連接失敗" ); System.out.println( "-----------------------" ); } else { System.out.println( "數(shù)據(jù)庫(kù)連接成功" ); System.out.println( "-----------------------" ); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return conn; } } |
進(jìn)行SqlServe數(shù)據(jù)庫(kù)java_conn_test中的tb_User進(jìn)行數(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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
package com.Project_DataBase01; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; public class StartMain { private static Connection conn; public static void main(String[] args) { // TODO Auto-generated method stub conn= new SelectQuery().getConnection(); GetInsert(); GetSelect(); GetUpdate(); GetSelect(); GetDelete(); GetSelect(); } /* * INSERT */ public static void GetInsert(){ if(conn!=null){ //INSERT System.out.println("-----------INSERT------------"); int x=1+(int)(Math.random()*5000); String insert_str="INSERT INTO tb_User (UserName,UserPwd,UserId) VALUES ('name_"+x+"','pwd_"+x+"',NEWID())"; try { Statement insertstatement=conn.createStatement(); int result= insertstatement.executeUpdate(insert_str); if(result>0){ System.out.println("添加成功"); System.out.println("-----------------------"); } else { System.out.println("添加失敗"); System.out.println("-----------------------"); } } catch (Exception e) { System.out.println("添加失敗"); System.out.println("-----------------------"); // TODO: handle exception } } else { System.out.println("請(qǐng)檢查數(shù)據(jù)庫(kù)連接"); System.out.println("-----------------------"); } } /* * SELECT */ public static void GetSelect(){ if(conn!=null){ //SELECT System.out.println("-----------SELECT------------"); String select_str=" SELECT * FROM tb_User "; try { PreparedStatement selectps=conn.prepareStatement(select_str); ResultSet rs=selectps.executeQuery(); while (rs.next()) { String name=rs.getString("UserName"); String pwd=rs.getString("UserPwd"); String UserId=rs.getString("UserId"); System.out.println(name+"\t"+pwd+"\t"+UserId); } System.out.println("查詢成功"); System.out.println("-----------------------"); } catch (Exception e) { // TODO: handle exception System.out.println("查詢失敗"); System.out.println("-----------------------"); } } else { System.out.println("請(qǐng)檢查數(shù)據(jù)庫(kù)連接"); System.out.println("-----------------------"); } } /* * UPDATE */ public static void GetUpdate(){ if(conn!=null){ //UPDATE System.out.println("-----------INSERT------------"); String update_str="UPDATE tb_User SET UserPwd=UserPwd+'xxxxxxxx' WHERE UserId='fa562573-218a-4205-b67d-ebdfac3f8329'"; try { Statement updatestatement=conn.createStatement(); int result=updatestatement.executeUpdate(update_str); if(result>0){ System.out.println("修改成功!"); System.out.println("-----------------------"); }else { System.out.println("修改失敗"); System.out.println("-----------------------"); } } catch (Exception e) { // TODO: handle exception System.out.println("修改失敗"); System.out.println("-----------------------"); } } else { System.out.println("請(qǐng)檢查數(shù)據(jù)庫(kù)連接"); System.out.println("-----------------------"); } } /* * DELETE */ public static void GetDelete(){ if (conn!= null ){ //DELETE System.out.println( "-----------DELETE------------" ); String delete_str= "DELETE tb_User WHERE UserId!='fa562573-218a-4205-b67d-ebdfac3f8329'" ; try { Statement deletestatement=conn.createStatement(); int result=deletestatement.executeUpdate(delete_str); if (result> 0 ){ System.out.println( "刪除成功!" ); System.out.println( "-----------------------" ); } else { System.out.println( "刪除失敗" ); System.out.println( "-----------------------" ); } } catch (Exception e) { // TODO: handle exception System.out.println( "刪除失敗" ); System.out.println( "-----------------------" ); } } else { System.out.println( "請(qǐng)檢查數(shù)據(jù)庫(kù)連接" ); System.out.println( "-----------------------" ); } } } |
運(yùn)行程序:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/heyangyi/p/6237696.html