學(xué)習(xí)了jdbc一段時(shí)間后感覺(jué)自己寫一個(gè)簡(jiǎn)單的封裝來(lái)試試,于是參考的一些資料就寫了一下不是多好,畢竟剛學(xué)也不太久
首先寫配置文件:直接在src下建立一個(gè)db.properties文件然后寫上內(nèi)容
1
2
3
4
5
|
<span style= "font-size:18px;" >MysqlDriver=com.mysql.jdbc.Driver MysqlURL=jdbc\:mysql\: //localhost\:3306/one User=root Pwd= 123456 </span> |
之后再寫一個(gè)類代碼如下
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
|
<span style= "font-size:18px;" > package cn.java.ad; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; /** * 本例是寫了一個(gè)小的封裝 * 對(duì)jdbc的封裝練習(xí) * @author hello * @version jdk 1.8 */ public class ReadMain { static Properties pos= null ; //設(shè)置靜態(tài)的在加載類的時(shí)候只需要一次 static { pos= new Properties(); //建立Peoperties用來(lái)讀取配置文件 try { //下面是用來(lái)讀取配置文件的 pos.load(Thread.currentThread().getContextClassLoader().getResourceAsStream( "db.properties" )); } catch (IOException e) { e.printStackTrace(); } } public static Connection getcon(){ //建立Connection連接 try { Class.forName(pos.getProperty( "MysqlDriver" )); //加載com.mysql.jdbc.Driver } catch (ClassNotFoundException e) { e.printStackTrace(); } try { //加載URL ,User,password return DriverManager.getConnection(pos.getProperty( "MysqlURL" ), pos.getProperty( "User" ),pos.getProperty( "Pwd" )); } catch (SQLException e) { e.printStackTrace(); } return null ; } public static void Close(ResultSet rs,Statement st,Connection co){ try { //關(guān)閉數(shù)據(jù)庫(kù)連接采用重載的方法便于封裝 if (rs!= null ) rs.close(); if (st!= null ) st.close(); if (co!= null ) co.close(); } catch (Exception e) { e.printStackTrace(); } } public static void Close(ResultSet rs,Connection co){ try { //關(guān)閉ResultSet Connection if (rs!= null ) rs.close(); if (co!= null ) co.close(); } catch (Exception e) { e.printStackTrace(); } } public static void Close(Connection co){ try { //關(guān)閉Connection if (co!= null ) co.close(); } catch (Exception e) { e.printStackTrace(); } } } //程序結(jié)束</span> |
之后寫主類代碼如下
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
|
<span style= "font-size:18px;" > package cn.java.ad; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; public class Main { public static void main(String[] args) { Connection con= null ; ResultSet res= null ; Statement sta= null ; String sql= null ; String name= "李雷" ; String sex= "男" ; PreparedStatement ps= null ; try { con=ReadMain.getcon(); sql= "insert into student(id,name,sex,phone)VALUES(1235,?,?,15896324131)" ; ps=con.prepareStatement(sql); //獲取sql語(yǔ)句 //在這里 the first parameter is 1, the second is 2, ... //x the parameter value //可以看出下標(biāo)是從1開(kāi)始的 ps.setString( 1 , name); //將對(duì)應(yīng)的name插入數(shù)據(jù)表中 ps.setString( 2 , sex); //將對(duì)應(yīng)的sex插入數(shù)據(jù)表中 ps.execute(); //執(zhí)行sql語(yǔ)句并且沒(méi)有返回值 System.out.println( "插入成功" ); } catch (Exception e) { e.printStackTrace(); } finally { ReadMain.Close(res, sta, con); //依次關(guān)閉連接 } } } </span> |
下面是兩張圖是建立db.properties的步驟
以上就是本文的全部?jī)?nèi)容,希望大家可以喜歡。