我是靠谱客的博主 高高飞机,这篇文章主要介绍JDBC的五步,现在分享给大家,希望可以做个参考。

1. 简单五步

复制代码
1
2
3
4
5
6
7
8
9
10
1、加载驱动程序   Class.forName(DriverClass); 2、连接数据库   Connection connect = DriverManager.getConnection(DataBase URL); 3、创建语句   Statement statement = connect.createStatement(); 4、执行语句   ResultSet rs = statement.executeQuery(SQL语句); 5、处理ResultSet

2.JdbcUtil.java

复制代码
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package com.rimi.project.util; import java.io.IOException; 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.sql.Statement; import java.util.Properties; import javax.sql.rowset.CachedRowSet; import com.sun.rowset.CachedRowSetImpl; public class JDBCUtil { // 访问数据库的用户名 private static String userName = null; // 访问数据库的密码 private static String password = null; // 使用jdbc的数据库驱动 private static String driver = null; // 访问数据库的url格式 // jdbc代表协议,mysql代表子协议 localhost代表本地ip地址 3306代表端口 j1806代表指定的数据库名 // rewriteBatchedStatements 确定是否开启批处理 private static String url = null; static { // 在静态代码块中读取properties文件 // getClassLoader 定位类加载路径 // getResourceAsStream 以字节流的方式读出指定的文件 InputStream inputStream = JDBCUtil.class.getClassLoader().getResourceAsStream("datasource.properties"); // 利用Properties 工具类将输入字节流进行解析 Properties properties = new Properties(); try { properties.load(inputStream); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // 利用properties工具类取出配置文件信息 // getProperty方法根据配置文件中的变量名来获取变量值 driver = properties.getProperty("driver"); url = properties.getProperty("url"); userName = properties.getProperty("userName"); password = properties.getProperty("password"); try { inputStream.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // 第一步,注册加载驱动 // Class.forName 将指定路径下的类,加载进java虚拟机 try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } } // 第二步,获取默认的数据库名和用户名去建立数据库连接 public static Connection getConnection() { return getConnection(url,userName,password); } // 使用指定的用户名和数据库名建立连接 public static Connection getConnection(String url,String userName,String password) { Connection connection = null; try { connection = DriverManager.getConnection(url, userName, password); } catch (SQLException e) { e.printStackTrace(); } return connection; } // 第三步,建立普通通道 public static Statement createStatement(Connection connection) { Statement statement = null; try { statement = connection.createStatement(); } catch (SQLException e) { e.printStackTrace(); } return statement; } // 第四步,采用指定的数据库连接去执行sql并返回结果 public static boolean update(String sql,Connection connection) { Statement statement = createStatement(connection); int row = 0; try { row = statement.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { close(null, statement, connection); } if(row > 0) { return true; } else { return false; } } // 根据提供的sql,使用默认的数据库连接 执行sql并返回结果 public static boolean update(String sql) { Connection connection = getConnection(); return update(sql,connection); } // 使用指定的连接,执行查询操作 public static ResultSet query(String sql,Connection connection) { // 建立普通通道 Statement statement = createStatement(connection); ResultSet resultSet = null; CachedRowSet cachedRowSet = null; try { // 普通通道执行查询操作 resultSet = statement.executeQuery(sql); // 离线结果集合,允许将ResultSet数据拷贝到java虚拟机内存,即使连接中断,数据不会丢失 cachedRowSet = new CachedRowSetImpl(); // 拷贝数据到离线结果集合 cachedRowSet.populate(resultSet); } catch (SQLException e) { e.printStackTrace(); } finally { close(resultSet, statement, connection); } return cachedRowSet; } // 使用默认的连接执行查询操作 public static ResultSet query(String sql) { Connection connection = getConnection(); return query(sql,connection); } // 建立预编译通道 public static PreparedStatement createPreparedStatement(Connection connection, String sql) { PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(sql); } catch (SQLException e) { e.printStackTrace(); } return preparedStatement; } // 绑定参数 public static void bundleParam(PreparedStatement preparedStatement, String[] params) { if(null != params) { for(int index = 0; index < params.length; index++) { // 数组元素下标从0开始,而绑定参数,第一个参数下标为1,需要加1 try { preparedStatement.setString(index+1, params[index]); } catch (SQLException e) { e.printStackTrace(); } } } } // 根据指定的数据库连接,利用预编译通道执行sql public static boolean preUpdate(String sql, String[] params, Connection connection) { // 创建预编译通道 PreparedStatement preparedStatement = createPreparedStatement(connection, sql); // 给预编译通道绑定参数 bundleParam(preparedStatement, params); int row = 0; try { // 执行sql,返回受影响的行数 row = preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { close(null, preparedStatement, connection); } if(row > 0) { return true; } else { return false; } } // 使用默认的连接,执行预通道sql public static boolean preUpdate(String sql, String[] params) { // 获取默认连接 Connection connection = getConnection(); return preUpdate(sql, params, connection); } public static ResultSet preSelect(String sql,String[] params, Connection connection) { // 通过指定连接,建立sql预编译通道 PreparedStatement preparedStatement = createPreparedStatement(connection, sql); // 绑定参数 bundleParam(preparedStatement, params); ResultSet resultSet = null; CachedRowSet cachedRowSet = null; try { resultSet = preparedStatement.executeQuery(); // 创建一个离线结果集 cachedRowSet = new CachedRowSetImpl(); // 拷贝数据到离线结果集 cachedRowSet.populate(resultSet); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { close(resultSet, preparedStatement, connection); } return cachedRowSet; } public static ResultSet preSelect(String sql,String[] params) { Connection connection = getConnection(); return preSelect(sql,params,connection); } public static void close(ResultSet resultSet, Statement statement, Connection connection ) { // 第五步 释放资源 // try { // if(null != resultSet) { // resultSet.close(); // } // } catch (SQLException e) { // e.printStackTrace(); // } finally { // try { // if(null != preparedStatement) { // preparedStatement.close(); // } // } catch (SQLException e) { // e.printStackTrace(); // } finally { // try { // if(null != connection) { // connection.close(); // } // } catch (SQLException e) { // // e.printStackTrace(); // } // } // // } // if(null != resultSet) { try { resultSet.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(null != statement) { try { statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(null != connection) { try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

最后

以上就是高高飞机最近收集整理的关于JDBC的五步的全部内容,更多相关JDBC内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部