本文實例講述了JDBC操作數(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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
package cn.com.JDBC; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class CRUD { public static void main(String[] args) throws SQLException { // TODO Auto-generated method stub //create(); //update(); delete(); read(); } static void delete() throws SQLException { Connection conn= null ; Statement st= null ; ResultSet resultset= null ; try { //2.建立連接 conn=JdbcUtils.getConnection(); //單例設(shè)計模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.創(chuàng)建語句 st=conn.createStatement(); //4.執(zhí)行語句 String sql= "delete from user where id>5" ; int i=st.executeUpdate(sql); System.out.println( "i=" +i); } finally { JdbcUtils.free(resultset, st, conn); } } static void update() throws SQLException { Connection conn= null ; Statement st= null ; ResultSet resultset= null ; try { //2.建立連接 conn=JdbcUtils.getConnection(); //單例設(shè)計模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.創(chuàng)建語句 st=conn.createStatement(); //4.執(zhí)行語句 String sql= "update user set money=money+20" ; int i=st.executeUpdate(sql); System.out.println( "i=" +i); } finally { JdbcUtils.free(resultset, st, conn); } } static void create() throws SQLException { Connection conn= null ; Statement st= null ; ResultSet resultset= null ; try { //2.建立連接 conn=JdbcUtils.getConnection(); //單例設(shè)計模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.創(chuàng)建語句 st=conn.createStatement(); //4.執(zhí)行語句 String sql= "insert into user(name,birthday,money) values('wy','2011-09-23','2894656')" ; int i=st.executeUpdate(sql); System.out.println( "i=" +i); } finally { JdbcUtils.free(resultset, st, conn); } } static void read() throws SQLException { Connection conn= null ; Statement st= null ; ResultSet resultset= null ; try { //2.建立連接 conn=JdbcUtils.getConnection(); //單例設(shè)計模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.創(chuàng)建語句 st=conn.createStatement(); //4.執(zhí)行語句 resultset=st.executeQuery( "select id,name,birthday,money from user" ); //5.處理結(jié)果 while (resultset.next()) { System.out.println(resultset.getObject( "id" )); System.out.println(resultset.getObject( "name" )); System.out.println(resultset.getObject( "birthday" )); System.out.println(resultset.getObject( "money" )); } } finally { JdbcUtils.free(resultset, st, conn); } } } package cn.com.JDBC; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JdbcUtils { private static String url= "jdbc:mysql://localhost:3306/jdbc" ; private static String user= "root" ; private static String password= "123" ; private JdbcUtils() { } static { try { Class.forName( "com.mysql.jdbc.Driver" ); } catch (ClassNotFoundException e) { throw new ExceptionInInitializerError(e); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, user, password); } public static void free(ResultSet resultset,Statement st,Connection conn) { //6.釋放資源 try { if (resultset!= null ) resultset.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (st!= null ) st.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (conn!= null ) try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } |
希望本文所述對大家Java程序設(shè)計有所幫助。