MysqlConn.java
5.75 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
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
package com.glsk.connection;
import com.glsk.conf.ConfigurationManager;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.*;
import java.util.ArrayList;
public class MysqlConn {
private static Connection connection = null;
static {
try {
// Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.cj.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();
}
}
}
}
try {
if (!connection.isValid(1)) {
close(connection);
connection = DriverManager.getConnection(url);
}
return connection;
} catch (SQLException e) {
e.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 ArrayList<ArrayList<String>> executeQuery(Connection connection, String sql, int columns) {
try {
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
ArrayList<ArrayList<String>> result = new ArrayList<>();
while (rs.next()) {
String temp = "";
ArrayList<String> colList = new ArrayList<>();
for (int i = 1; i <= columns; i++) {
colList.add(rs.getString(i));
}
result.add(colList);
}
rs.close();
ps.close();
return result;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
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) throws UnsupportedEncodingException {
// dbc:mysql://192.168.3.222:3308/scrm_account?user=os_user&password=os#123&autoReconnect=true&autoReconnectForPools=true
System.out.println("URL=" + ConfigurationManager.getProperty("mysql.url.xlyq_agi"));
String password = "v#@JrWCEH9g3";
String encodedPassword = URLEncoder.encode(password, "UTF-8");
String url = "jdbc:mysql://rm-wz9uqix4n8s738dq18o.mysql.rds.aliyuncs.com:3306/xlyq_agi"
+ "?user=bool"
+ "&password=" + encodedPassword
+ "&useSSL=false"
+ "&serverTimezone=Asia/Shanghai"
+ "&allowPublicKeyRetrieval=true"
+ "&autoReconnect=true"
+ "&autoReconnectForPools=true";
Connection conn = MysqlConn.getConnection(url);
// 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 theme,content_unit,post_content from xlyq_agi.fund_post\n" +
"where post_date = '2025-11-15' limit 10", 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);
}
}