/
MySQLManager.java
84 lines (70 loc) · 2.22 KB
/
MySQLManager.java
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
import java.sql.*;
import java.util.ArrayList;
public class MySQLManager {
private Connection con;
public MySQLManager() {
}
public MySQLManager(String url) throws SQLException {
con = DriverManager.getConnection(url);
}
public MySQLManager(String hostname, String port, String username, String password) throws SQLException {
String url = "jdbc:mysql://" + hostname + ":port" + "?user=" + username + "&password=" + password;
con = DriverManager.getConnection(url);
}
public MySQLManager(String url, String username, String password) throws SQLException{
String connectString = url + "?user=" + username + "&password=" + password;
con = DriverManager.getConnection(connectString);
}
public MySQLManager(String url, String username) throws SQLException{
String connectString = url + "?user=" + username;
con = DriverManager.getConnection(connectString);
}
public ArrayList<String[]> query(String query) throws SQLException{
ResultSet rs;
Statement stmt = con.createStatement();
rs = stmt.executeQuery(query);
ArrayList<String[]> result = getStrings(rs);
stmt.close();
return result;
}
private ArrayList<String[]> getStrings(ResultSet rs) {
try {
ResultSetMetaData meta = rs.getMetaData();
ArrayList<String[]> result = new ArrayList<String[]>();
int j = 0;
while(rs.next()) {
result.add(new String[meta.getColumnCount()]);
for(int i=0; i<meta.getColumnCount() ; i++) {
result.get(j)[i] = rs.getString(i+1);
}
j++;
}
rs.close();
return result;
} catch(SQLException e) {
System.err.println("Error reading result set");
e.printStackTrace();
}
System.err.println("returning null");
return null;
}
public int update(String update) throws SQLException {
Statement stmt = con.createStatement();
int result = stmt.executeUpdate(update);
stmt.close();
return result;
}
public int getNumCols(String table) throws SQLException {
Statement stmt = con.createStatement();
String query = "SELECT * FROM " + table;
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData meta = rs.getMetaData();
int result = meta.getColumnCount();
stmt.close();
rs.close();
return result;
}
public void close() throws SQLException {
con.close();
}
}