DBUtils
/**
* @Author: CodeSleep
* @Date: 2021-06-10 22:20
* @Description: //TODO DB数据库访问通用操作
*/
public class DBUtil {
/**
* @return Connection对象
* @throws ClassNotFoundException
* @throws SQLException
* @Author CodeSleep
* @Date: 2021-06-10 22:20
* @Description: //TODO 获取连接对象的方法
*/
public static Connection getConn() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取连接 Connection conn = DriverManager.getConnection(url,user,password)
String url = "jdbc:mysql://localhost:3306/newsdb?serverTimezone=Asia/Shanghai";
String user = "root";
String password = "795200";
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
/**
* @param sql 要执行的sql语句
* @param params 占位符?对应的数据
* @return int 受影响的行
* @Author CodeSleep
* @Date: 2021-06-10 22:20
* @Description: //TODO 通用的增加 删除和修改方法
*/
public static int exUpdate(String sql, Object... params) {
Connection conn = null;
PreparedStatement pstmt = null;
int n = 0;
try {
// 获取连接对象
conn = getConn();
// 创建pstmt对象
pstmt = conn.prepareStatement(sql);
// parmas当成数组来处理
if (params != null) {
for (int i = 0; i < params.length; i++) {
// 设置占位符对应参数值
pstmt.setObject(i + 1, params[i]);
}
}
System.out.println("pstmt :" + pstmt);
// 执行增删改方法
n = pstmt.executeUpdate();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
// 释放资源
closeAll(null, pstmt, conn);
}
// 返回受影响的行
return n;
}
/**
* @param sql sql语句
* @param cls Class类型对象
* @param params 占位符参数
* @return Object 实际上是一个List对象
* @Author CodeSleep
* @Date: 2021-06-10 22:21
* @Description: //TODO 通用查询方法
*/
public static Object exQuery(String sql, Class cls, Object... params) {
// list就是存放查询到到结果集
List list = new ArrayList();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 获取连接对象
conn = getConn();
// 创建pstmt对象
pstmt = conn.prepareStatement(sql);
// parmas当成数组来处理
if (params != null) {
for (int i = 0; i < params.length; i++) {
// 设置占位符对应参数值
pstmt.setObject(i + 1, params[i]);
}
}
System.out.println("pstmt :" + pstmt);
// 执行查询的方法
rs = pstmt.executeQuery();
//判断Class -->查询单个值
if (cls.getName().equals("java.lang.Object")) {
//单个值
if (rs.next()) {
return rs.getInt(1);
}
}
// 查询多个列 -> 遍历
while (rs.next()) {
//得到一行数据就调用convert方法将列的数据填充到一个对象中,然后返回
Object obj = convert(cls, rs);
//将对象添加到list集合中
list.add(obj);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} finally {
// 释放资源
closeAll(rs, pstmt, conn);
}
// 返回结果
return list;
}
/**
* @param countSQL 查询sql语句 count()
* @param cls Object
* @param params
* @return long记录数有几条
* @Author CodeSleep
* @Date: 2021/7/22 15:54
* @Description: //TODO 记录数查询
*/
public static long getTotalCount(String countSQL, Class cls, Object... params) {
//// select count(*) from
// (select * from news where (newstitle like ? or newscontent like ?) order by
//// pubdate desc) as temp
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
long result = 0;
try {
// 获取连接对象
conn = getConn();
// 创建pstmt对象
pstmt = conn.prepareStatement(countSQL);
// parmas当成数组来处理
if (params != null) {
for (int i = 0; i < params.length; i++) {
// 设置占位符对应参数值
pstmt.setObject(i + 1, params[i]);
}
}
System.out.println("pstmt :" + pstmt);
// 执行查询的方法
rs = pstmt.executeQuery();
// 判断Class -->查询单个值
if (cls.getName().equals("java.lang.Object")) {
// 单个值
if (rs.next()) {
result = rs.getLong(1);
}
}
} catch (SQLException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 释放资源
closeAll(rs, pstmt, conn);
}
return result;
}
/**
* @param sql
* @param cls
* @param pageNo
* @param pageSize
* @param params
* @return
* @Author CodeSleep
* @Date: 2021/7/22 15:54
* @Description: //TODO 分页查询
*/
public static PageData exQueryByPage(String sql, Class cls, int pageNo, int pageSize, Object... params) {
// sql ?
// select * from news where (newstitle like '%测%' or newscontent like '%测%')
// order by pubdate desc
// 1 查询记录数
// select count(*) from
// (select * from news where (newstitle like '%测%' or newscontent like '%测%')
// order by pubdate desc) as temp
String countSQL = "select count(*) from (" + sql + ") as temp";
long result = getTotalCount(countSQL, Object.class, params);
System.out.println("count result :" + result);
// 2 limit
// select * from news where (newstitle like '%测%' or newscontent like '%测%')
// order by pubdate desc
// limit 0,5
int start = (pageNo - 1) * pageSize;
sql = sql + " limit " + start + "," + pageSize;
// list就是存放查询到到结果集
List list = new ArrayList();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 获取连接对象
conn = getConn();
// 创建pstmt对象
pstmt = conn.prepareStatement(sql);
// parmas当成数组来处理
if (params != null) {
for (int i = 0; i < params.length; i++) {
// 设置占位符对应参数值
pstmt.setObject(i + 1, params[i]);
}
}
System.out.println("pstmt :" + pstmt);
// 执行查询的方法
rs = pstmt.executeQuery();
// 查询多个列 -> 遍历
while (rs.next()) {
// 得到一行数据就调用convert方法将列的数据填充到一个对象中,然后返回
// Object obj ->Type News
Object obj = convert(cls, rs);
// 将对象添加到list集合中
list.add(obj);
}
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 释放资源
closeAll(rs, pstmt, conn);
}
// 构造一个PageData对象
PageData pd = new PageData(pageNo, pageSize, result, list);
// 返回结果
return pd;
}
/**
* @param cls Classs
* @param rs 结果集
* @return Object
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
* @throws InvocationTargetException
* @Author CodeSleep
* @Date: 2021-06-10 22:21
* @Description: //TODO 结果集转换为Class对象
*/
private static Object convert(Class cls, ResultSet rs)
throws InstantiationException, IllegalAccessException, SQLException, InvocationTargetException {
Object obj = cls.newInstance();
// 检索此 ResultSet对象的列的数量,类型和属性。
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
String cname = rsmd.getColumnLabel(i);
// 开源组件:beanutil
// 1-》对象名
// 2->属性名
// 3->属性的值
BeanUtils.setProperty(obj, cname, rs.getObject(cname));
}
return obj;
}
/**
* @param rs 结果集ResultSet对象
* @param pstmt 预处理对象 PreparedStatement
* @param conn 连接对象 Connection
* @Author CodeSleep
* @Date: 2021-06-10 22:21
* @Description: //TODO 释放资源
*/
public static void closeAll(ResultSet rs, PreparedStatement pstmt, Connection conn) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
编辑 (opens new window)