JDBC的六步:
1.注冊驅動
2.獲取數據庫的連接
3.獲取數據庫的操作對象
4.執行sql語句
5.處理查詢結果集(如果執行的語句中沒有select語句這一步不用寫)
6.關閉資源
第一步:注冊驅動
1
2
3
4
5
6
7
8
9
10
11
|
//異常一定是需要處理的 //根據版本不同書寫的代碼有一些變化,老版本是 DriverManager.register( new com.mysql.jdbc.Driver()); //或者 Class.forName( "com.mysql.jdbc.Driver" ); //新版本是 DriverManager.register( new com.mysql.cj.jdbc.Driver()); //或者 Class.forName( "com.mysql.cj.jdbc.Driver" ); |
第二步:獲取數據庫的連接
1
2
3
4
5
6
7
8
9
10
11
|
//因為要進行try..catch,還要關閉這個資源,因此寫在try外邊且賦值為空 Connection conn = null ; ... //返回值是Connection,老版本和新版的url書寫也不一樣 // jdbc:mysql://localhost:3306/t_use這個前面的都一樣,把t_use更改為自己的數據庫名 //老版 conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/t_use" , "用戶名" , "密碼" ); //新版 conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/t_use?serverTimezone=GMT%2B8" , "用戶名" , "密碼" ); |
第三步:獲取數據庫操作對象
1
2
3
4
5
|
//這個和剛才的Connection是一樣的 Statement st = null ; ... //這樣就把對象創建好了 st = conn.createStatement(); |
第四步:執行sql語句
1
2
3
4
5
6
7
8
9
10
|
//引號里面寫sql語句 String sql = " " ; //再用剛才創建好的對象接入這個sql語句 //這里還有需要說的,如果不是select語句就用下面這個,返回值是一個int,這個就不需要第五步了 st.executeUpdate(sql); //如果sql語句是select的話,就要用下面的這個語句了,還需要定義一個ResultSet對象,去接收這個值 //然后進行第五步的處理 ResultSet rs = null ; //和Connection一樣的 rs = st.executeQuery(sql); |
第五步:處理查詢結果集
1
2
3
4
|
//這個只有select語句才進行處理,不僅可以getString還可以getInt等 while (rs.next()){ String str = rs.getString( "需要輸出的字段的名字" ); } |
第六步:關閉資源
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
|
//前面五步都是在try里面進行的,第六步是在finally進行的 //關閉連接 if (rs != null ) { try { rs.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (st != null ) { try { st.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (conn != null ) { try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } |
關閉連接也是有順序的,先關ResultSet,再關Statement對象,最后關Connection對象.
完整代碼
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
|
package jdbc.com; import com.mysql.cj.protocol.Resultset; import java.sql.*; public class Test02 { public static void main(String[] args) { Connection conn = null ; Statement st = null ; ResultSet rt = null ; try { //注冊連接(可以寫到一行里面) //com.mysql.cj.jdbc.Driver Driver = new com.mysql.cj.jdbc.Driver(); DriverManager.registerDriver( new com.mysql.cj.jdbc.Driver()); //獲取數據庫連接 conn=DriverManager.getConnection( "jdbc:mysql://localhost:3306/liu2?serverTimezone=GMT%2B8" , "用戶名" , "密碼" ); //獲取數據庫操作對象 st = conn.createStatement(); //執行sql語句 String sql = "select ename,sal from emp order by sal desc" ; rt = st.executeQuery(sql); //處理查詢語句 while (rt.next()){ String ename = rt.getString( "ename" ); String sal = rt.getString( "sal" ); System.out.println(ename + "," + sal); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { //關閉連接 if (rt != null ) { try { rt.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (st != null ) { try { st.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (conn != null ) { try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } } } |
最后,這個如果要傳入值的話,可能會造成sql的注入,解決辦法就是把Statement變成PreparedStatement,就可以避免sql的注入問題了,只不過第三步和第四步代碼有點變化,就不再多說了。。
總結
到此這篇關于JDBC連接(與mysql連接)的文章就介紹到這了,更多相關JDBC與mysql連接內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/zhinaijiangya/article/details/116566015