MysqlConn.java
3.76 KB
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
package com.glsx.connection;
import com.glsx.conf.ConfigurationManager;
import java.sql.*;
public class MysqlConn {
private static Connection connection = null;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception ex) {
ex.printStackTrace();
}
}
private MysqlConn() {
}
public static Connection getConnection(String url) {
if (connection == null) {
synchronized (MysqlConn.class) {
if (connection == null) {
try {
connection = DriverManager.getConnection(url);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
return connection;
}
public static void executeUpdate(Connection connection, String sql) {
PreparedStatement pstmt = null;
try {
connection.setAutoCommit(false);
pstmt = connection.prepareStatement(sql);
pstmt.executeUpdate();
connection.commit();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
try {
pstmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void executeQuery(Connection connection, String sql, QueryCallback callback) {
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = connection.prepareStatement(sql);
rs = pstmt.executeQuery();
callback.process(rs);
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
// 流式查询
public static void executeFlowQuery(Connection connection, String sql, QueryCallback callback) {
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
pstmt.setFetchSize(Integer.MIN_VALUE);
pstmt.setFetchDirection(ResultSet.FETCH_REVERSE);
rs = pstmt.executeQuery();
callback.process(rs);
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void close(Connection connection) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public static interface QueryCallback {
void process(ResultSet rs) throws Exception;
}
public static void main(String[] args) {
Connection conn = MysqlConn.getConnection(ConfigurationManager.getProperty("testUrl"));
// for (int i = 0; i < 100; i++) {
// String sql = "insert into t_user(username,password) values('name"+(100+i) + "','pw"+(100+i) + "')";
// System.out.println(sql);
// MysqlConn.executeUpdate(conn, sql);
// }
final StringBuffer buffer = new StringBuffer();
MysqlConn.executeQuery(conn, "select username,password from t_user limit 6", new QueryCallback() {
@Override
public void process(ResultSet rs) throws Exception {
while (rs.next()) {
// System.out.println(rs.getString(1));
buffer.append(rs.getString(1));
}
}
});
System.out.println(buffer.toString());
MysqlConn.close(conn);
}
}