This repository has been archived by the owner on Feb 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
DatabaseConnection.java
327 lines (296 loc) · 9.58 KB
/
DatabaseConnection.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
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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mpv5.db.common;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JProgressBar;
import mpv5.Main;
import mpv5.db.objects.User;
import mpv5.globals.LocalSettings;
import mpv5.globals.Messages;
import mpv5.logging.Log;
import mpv5.ui.dialogs.Popup;
/**
*
*
*/
public class DatabaseConnection {
private static java.sql.Connection conn;
private static DatabaseConnection connector;
/**
* @return the connector
*/
public static DatabaseConnection getConnector() {
return connector;
}
/**
* @return the user
*/
public static String getUser() {
return user;
}
/**
* @return the password
*/
public static String getPassword() {
return password;
}
/**
* @return the prefix
*/
public static String getPrefix() {
return prefix;
}
private ConnectionTypeHandler ctype;
private static String user;
private static String password;
private static String prefix = "";
/**
*
* @return Database connector
* @throws Exception
*/
public static synchronized DatabaseConnection instanceOf() throws Exception {
if (getConnector() == null || getConnector().getConnection() == null) {
connector = new DatabaseConnection();
getConnector().connect(false);
new connectionPing(connector).start();
}
return getConnector();
}
private Statement statement;
private JProgressBar prog;
/**
*
* @return
*/
public java.sql.Connection getConnection() {
return conn;
}
/**
* Test-Verbindung zur Datenbank herstellen.
*
* @param predefinedDriver
* @param user
* @param password
* @param location
* @param dbname
* @param create
* @return Connection
* @throws Exception
*/
public boolean connect(String predefinedDriver, String user, String password, String location, String dbname, String prefix, boolean create) throws Exception {
try {
ctype = new ConnectionTypeHandler();
getCtype().setDRIVER(predefinedDriver);
getCtype().setURL(location);
getCtype().setDBName(dbname);
getCtype().setPrefix(prefix);
DatabaseConnection.user = user;
DatabaseConnection.password = password;
DatabaseConnection.prefix = prefix;
DriverManager.registerDriver((Driver) Class.forName(getCtype().getDriver()).newInstance());
Log.Debug(this, "Driver: " + getCtype().getDriver());
} catch (Exception ex) {
Log.Debug(this, ex);
}
return reconnect(create);
}
public boolean reconnect(boolean create) throws SQLException {
Statement stmt;
String sql;
if (Log.isDebugging()) {
// Thread.dumpStack();
}
try {
Log.Debug(this, "RECONNECT::Datenbankverbindung: " + getCtype().getConnectionString(create));
conn = DriverManager.getConnection(getCtype().getConnectionString(create), user, password);
boolean result = true;
if (conn != null //&& conn.isValid(10)//does not work with MySQL Connector/J 5.0
) {//mysql (and others) need explicit create database, derby does it by itself
conn.setAutoCommit(true);
if (create && ConnectionTypeHandler.getDriverType() != ConnectionTypeHandler.DERBY) {
stmt = conn.createStatement();
if (User.PROPERTIES_OVERRIDE.hasProperty("drop_database_on_create")) {
try {
sql = "DROP DATABASE "
+ ConnectionTypeHandler.getDBNAME()
+ ";";
stmt.execute(sql);
} catch (SQLException ex) {
Log.Debug(this, "Database Error cleaning of old DB failed!");
}
}
try {
sql = "CREATE DATABASE "
+ ConnectionTypeHandler.getDBNAME()
+ " ;";
stmt.execute(sql);
} catch (SQLException ex) {
Log.Debug(ex);
result = Popup.OK_dialog("Could not create database " + ConnectionTypeHandler.getDBNAME() + ", did you already create it?", "Database Creation");
}
}
conn.setCatalog(ConnectionTypeHandler.getDBNAME());
connector = this;
return result;
} else {
throw new RuntimeException("Could not create connection: " + getCtype().getConnectionString(create));
}
} catch (SQLException ex) {
System.out.println("Database Error: " + ex.getMessage());
Popup.notice(ex.getLocalizedMessage());
Log.Debug(this, ex);
Log.Debug(this, ex.getNextException());
DatabaseConnection.shutdown();
throw ex;
}
}
/**
* Verbindung zur Datenbank herstellen.
*
* @return Connection
*/
private Connection connect(boolean create) throws Exception {
ctype = new ConnectionTypeHandler();
if (getCtype().getDriver() != null) {
Log.Debug(this, "Datenbanktreiber: " + getCtype().getDriver());
try {
Class.forName(getCtype().getDriver()).newInstance();
} catch (ClassNotFoundException ex) {
// Popup.error(ex);
//possibly not fatal, driver can have a different class [org.apache.derby.jdbc.ClientDriver,org.apache.derby.jdbc.EmbeddedDriver]
Log.Debug(this, ex.getMessage());
}
user = LocalSettings.getProperty("dbuser");
password = LocalSettings.getProperty("dbpassword");
prefix = LocalSettings.getProperty("dbprefix");
reconnect(create);
} else {
throw new UnsupportedOperationException("Datenbanktreiber: undefined");
}
return conn;
}
/**
* Verbindung trennen
*/
@SuppressWarnings("CallToThreadDumpStack")
public static void shutdown() {
try {
if (conn != null && !conn.isClosed()) {
if (ConnectionTypeHandler.getDriverType() == ConnectionTypeHandler.DERBY) {
try {
DriverManager.getConnection(
DatabaseConnection.instanceOf().getCtype().getConnectionString(false) + "shutdown=true;", user, password);
} catch (Exception ex) {
Log.Debug(DatabaseConnection.class, ex.getLocalizedMessage());
}
}
conn.close();
conn = null;
}
} catch (SQLException ex) {
ex.printStackTrace();
System.exit(1);
}
}
public boolean runQueries(String[] queries) throws SQLException {
if (prog != null) {
prog.setStringPainted(true);
prog.setMaximum(queries.length);
prog.setMinimum(0);
prog.setValue(0);
}
Statement stm = this.getConnection().createStatement();
// for (int i = 0; i < queries.length; i++) {
// stm.addBatch(queries[i]);
// }
// Log.PrintArray(queries);
try {
// stm.executeBatch();
for (int i = 0; i < queries.length; i++) {
try {
String string = queries[i];
if (prefix != null) {
string = string.replace(" table ", " table " + prefix);
string = string.replace(" TABLE ", " TABLE " + prefix);
string = string.replace(" on ", " on " + prefix);
string = string.replace(" ON ", " ON " + prefix);
string = string.replace(" into ", " into " + prefix);
string = string.replace(" INTO ", " INTO " + prefix);
}
Log.Print(string);
stm.execute(string);
if (prog != null) {
prog.setValue(i);
}
Thread.sleep(100);
} catch (Exception ex) {
throw ex;
}
}
return true;
} catch (Exception sQLException) {
Log.Debug(this, "-----------> "+sQLException.getMessage());
return false;
} finally {
if (prog != null) {
prog.setValue(0);
}
}
}
/**
* Set a progressbar
*
* @param progressbar
*/
public void setProgressbar(JProgressBar progressbar) {
prog = progressbar;
}
/**
* @return the ctype
*/
public ConnectionTypeHandler getCtype() {
return ctype;
}
/**
* @return the statement
*/
public Statement getStatement() {
return statement;
}
static class connectionPing extends Thread {
private DatabaseConnection c;
public connectionPing(DatabaseConnection c) {
this.c = c;
setPriority(MIN_PRIORITY);
}
@Override
public void run() {
try {
while (c.getConnection() != null || !c.getConnection().isClosed()) {
try {
sleep(180000);
} catch (InterruptedException ex) {
Log.Debug(ex);
}
try {
Log.Debug(this, "ping to " + c.getCtype().getURL());
c.runQueries(new String[]{"select count(ids) from groups"});
} catch (SQLException ex) {
Log.Debug(ex);
}
}
} catch (SQLException ex) {
Log.Debug(ex);
}
}
}
}