Java DBhelper类

  简单用Java写的连接mysql的DBhealper类,包含了基本的数据库连接,增删查改等功能。

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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import java.sql.*;

/**
* @author Eric_Chan
* @version 2016.10.11
*/
public class DBhelper
{
private String ipAddress = "127.0.0.1";
private int port = 3306;
private String user = "";
private String pwd = "";
private String dbName = "";
private Connection conn;
private static DBhealper instance = null; // 单例

/**
* 检查是否导入 mysql-connector-java.jar
*/
private DBhealper()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e)
{
System.out.println("没有正确导入 connector.jar\n 下载地址http://dev.mysql.com/downloads/connector/j/");
}
}

/**
* 获得唯一连接器
* @return DbConnection
*/
public static DBhealper getInstance()
{
if (DBhealper.instance == null)
{
instance = new DBhealper();
return DBhealper.instance;
}
else
{
return DBhealper.instance;
}
}

/**
* 连接数据库
*/
private void connect() throws SQLException
{
String url = String.format("jdbc:mysql://%s:%s/%s",this.ipAddress, this.port, this.dbName);
try
{
this.conn = DriverManager.getConnection(url, this.user, this.pwd);
}
catch (SQLException e)
{
System.out.println("连接失败\n");
e.printStackTrace();
}
}

/**
* 提供参数 并连接数据库 默认为本地数据库,端口为3306
* @param user 用户名
* @param pwd 密码
* @param dbName 数据库名
*/
public void connSQL(String user, String pwd, String dbName) throws SQLException
{
this.user = user;
this.pwd = pwd;
this.dbName = dbName;
this.connect();
System.out.println("连接成功\nconn-------------" + conn + '\n');
this.free(this.conn, null, null);
}
public void connSQL(String user, String pwd, String dbName, int port) throws SQLException
{
this.port = port;
this.connSQL(user, pwd, dbName);
}
public void connSQL(String user, String pwd, String dbName, String ipAddress) throws SQLException
{
this.ipAddress = ipAddress;
this.connSQL(user, pwd, dbName);
}
public void connSQL(String user, String pwd, String dbName,String ipAddress, int port) throws SQLException
{
this.ipAddress = ipAddress;
this.port = port;
this.connSQL(user, pwd, dbName);
}

/**
* 将表内数据输出至控制台
* @param tableName 表名
* @throws Exception
*/
public void showTable(String tableName) throws SQLException
{
this.connect();
Statement stmt = null;
ResultSet rs = null;
try
{
String sql = "SELECT * FROM " + tableName;
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
DBhealper.showResultSet(rs);
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
this.free(this.conn, stmt, rs);
}
}

/**
* 查【Query】
* 无参查找
* @param sql
* @return ResultSet
*/
public ResultSet executeQuery(String sql) throws SQLException
{
this.connect();
Statement stmt = null;
ResultSet rs = null;
try
{
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch (SQLException e)
{
e.printStackTrace();
this.free(conn, stmt, rs);
}
return rs;
}

/**
* 查【Query】
* 有参查找
* @param sql
* @param obj
* @return ResultSet
*/
public ResultSet executeQuery(String sql, Object... obj) throws SQLException
{
this.connect();
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < obj.length; i++)
{
pstmt.setObject(i + 1, obj[i]);
}
rs = pstmt.executeQuery();
}
catch (SQLException e)
{
e.printStackTrace();
this.free(conn, pstmt, rs);
}
return rs;
}
/**
* 更新【update】 无参更新
*
* @param sql
* @return int
*/
public int executeUpdate(String sql) {
this.connect();
Statement stmt = null;
int rs = 0;
try {
stmt = conn.createStatement();
rs = stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
this.free(conn, stmt, null);
}
return rs;
}

/**
* 更新【update】 有参更新
*
* @param sql
* @param obj
* @return int
*/
public int executeUpdate(String sql, Object... obj) {
this.connect();
PreparedStatement pstmt = null;
int rs = 0;
try {
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < obj.length; i++) {
pstmt.setObject(i + 1, obj[i]);
}
rs = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
this.free(conn, pstmt, null);
}
return rs;
}

/**
* 将ResultSet内的数据输出至控制台
* @param rs
* @throws SQLException
*/
public static void showResultSet(ResultSet rs) throws SQLException
{
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
// 输出列名
for (int i=1; i<=columnCount; i++)
{
System.out.print(rsmd.getColumnName(i));
System.out.print("(" + rsmd.getColumnTypeName(i) + ")");
System.out.print(" | ");
}
System.out.println();
// 输出数据
while (rs.next())
{
for (int i=1; i<=columnCount; i++)
{
System.out.print(rs.getString(i) + " | ");
}
System.out.println();
}
}

/**
* 判断记录是否存在
*
* @param sql
* @return Boolean
*/
public Boolean isExist(String sql) throws SQLException
{
this.connect();
Statement stmt = null;
Boolean isEx = false;
ResultSet rs = null;
try
{
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
rs.last();
isEx = rs.getRow()>0;
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
this.free(conn, stmt, rs);
}
return isEx;
}

/**
* 判断记录是否存在
* @param sql
* @return Boolean
*/
public Boolean isExist(String sql, Object... obj) throws SQLException
{
this.connect();
PreparedStatement pstmt = null;
Boolean isEx = false;
ResultSet rs = null;
try
{
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < obj.length; i++)
{
pstmt.setObject(i + 1, obj[i]);
}
rs = pstmt.executeQuery();
rs.last();
isEx = rs.getRow()>0;
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
this.free(conn, pstmt, rs);
}
return isEx;
}
/**
* 用来释放所有数据资源
* @param conn
* @param stmt
* @param rs
* @throws SQLException
*/
public void free(Connection conn, Statement stmt, ResultSet rs) throws SQLException
{
if (conn!=null)
{
try
{
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
if (stmt!=null)
{
try
{
stmt.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
if (rs!=null)
{
try
{
rs.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}

public void close()
{
try
{
this.conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}



public static void main(String args[]) throws SQLException
{
DBhealper connector = DBhealper.getInstance();
connector.connSQL("root", "1q2w3e", "student", 3307);
// connector.showTable("student");
//ResultSet rs = connector.executeQuery("SELECT * FROM student WHERE st_name=? and st_Password=?", "Jack", "1234");
//DBhealper.showResultSet(rs);
Boolean isEx = connector.isExist("SELECT * FROM student WHERE st_name=? and st_Password=?", "Jack", "1234");
System.out.println(isEx);
connector.close();
}


}
Have a nice day!