MysqlMutiConn.java
3.5 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
package com.glsk.connection;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Mysql连接,可以跨机器使用数据库连接
*/
public class MysqlMutiConn {
private static HashMap<String, Connection> mutiConnection = new HashMap<>();
public static void addConnection(String name, String url) {
try {
Connection connection = DriverManager.getConnection(url);
mutiConnection.put(name, connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getConnection(String name) {
return mutiConnection.get(name);
}
public static void close(Connection connection) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
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 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 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 interface QueryCallback {
void process(ResultSet rs) throws Exception;
}
public static void main(String[] args) {
}
}