-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBConnect.java
More file actions
294 lines (259 loc) · 9.52 KB
/
DBConnect.java
File metadata and controls
294 lines (259 loc) · 9.52 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
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
package speedmatch;
import java.net.InetAddress;
import java.sql.*;
import java.util.ArrayList;
import java.io.*;
import javax.sql.*;
import javax.swing.JOptionPane;
public class DBConnect {
private Connection con;
private String event;
private String username = "****"; //Enter username here
private String password = "*****"; //Enter password here
private String tmpdir = System.getProperty("java.io.tmpdir")+Processing.getPathSep();
public DBConnect(String eventname) {
con = null;
this.event = DBTools.sanitize(eventname);
}
public DBConnect(Dating d) {
con = null;
this.event = DBTools.sanitize(d.getName());
}
public void setEventname(String eventname) {
this.event = DBTools.sanitize(eventname);
}
public DBConnect() {
con=null;
}
// Allows to specify user-defined login credentials.
public void setLogin(String usr, String pwd) {
this.username = usr;
this.password = pwd;
}
// Searches for database server and establishes DB-connection.
public void initConnection() {
this.initConnection(DBTools.getDevices());
}
// Establishes DB-connection using predefined IP
public void initConnection(InetAddress ia) {
try {
String ip = ia.getHostAddress();
String url = "jdbc:mysql://"+ip+":3306/speedmatch";
Class.forName ("com.mysql.jdbc.Driver");
con = DriverManager.getConnection (url,username,password);
}catch(Exception e) {
JOptionPane.showMessageDialog(null, "Unable to connect to database: "+Processing.getLineSep()+e.getMessage()+ Processing.getLineSep() + Processing.getLineSep()+"Check if username and password are correct, if you have selected the right server and"+Processing.getLineSep()+"if you have granted remote access permissions to the user in the RDBMS", "CONNECTION ERROR", JOptionPane.ERROR_MESSAGE);
}
}
// Creates a table for a specific event
public void createEventTable() {
try {
StringBuilder stm = new StringBuilder("CREATE TABLE ");
stm.append(event);
stm.append(" (id INTEGER AUTO_INCREMENT, fname varchar(20), lname varchar(20), gender INTEGER, fa VARCHAR(50), dik VARCHAR(50), ereig VARCHAR(50), feier VARCHAR(50), rev VARCHAR(50), PRIMARY KEY(id));");
PreparedStatement pstm = con.prepareStatement(stm.toString());
pstm.executeUpdate();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Error while updating Database "+e.getMessage(), "DATABASE ERROR", JOptionPane.ERROR_MESSAGE);
}
}
// Adds a participant to the Database
public int insertParticipant(String fname, String lname, int gender, String fa, String dik, String ereig, String feier, String rev) {
try {
PreparedStatement ps = con.prepareStatement("INSERT INTO "+event+" (fname, lname, gender, fa, dik, ereig, feier, rev) VALUES (?,?,?,?,?,?,?,?);");
ps.setString(1, fname);
ps.setString(2, lname);
ps.setInt(3, gender);
ps.setString(4, fa);
ps.setString(5, dik);
ps.setString(6, ereig);
ps.setString(7, feier);
ps.setString(8, rev);
ps.executeUpdate();
ps.close();
Statement s = con.createStatement();
ResultSet rs = s.executeQuery("SELECT LAST_INSERT_ID();");
int id = -99;
while(rs.next()) {
id = rs.getInt(1);
}
s.close();
return id;
}catch(Exception e) {
JOptionPane.showMessageDialog(null, "Error while updating Database "+e.getMessage(), "DATABASE ERROR", JOptionPane.ERROR_MESSAGE);
return -99;
}
}
// Retrieves information from database and adds them to a dating-object
public Dating dbToEvent(Dating d) {
try {
boolean replace = true;
if (d.getNetParticipants().size()>0) {
int uin = JOptionPane.showConfirmDialog(null, "Your event already contains participants. Do you want to replace them?","Warning",JOptionPane.YES_NO_OPTION);
if(uin == JOptionPane.NO_OPTION) {
replace=false;
}
}
if(replace) {
d.clearParticipants();
int count = 0;
StringBuilder sb = new StringBuilder("SELECT * FROM ");
sb.append(DBTools.sanitize(d.getName()));
sb.append(";");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sb.toString());
while(rs.next()) {
int id = rs.getInt("id");
String fname = rs.getString("fname");
String lname = rs.getString("lname");
int gnd = rs.getInt("gender");
String fa = rs.getString("fa");
String dik = rs.getString("dik");
String ereig = rs.getString("ereig");
String feier = rs.getString("feier");
String rev = rs.getString("rev");
d.addParticipant(id, fname, lname, gnd, fa, dik, ereig, feier, rev);
d.addToFile(tmpdir);
count++;
}
JOptionPane.showMessageDialog(null, "Successfully loaded "+count+" Participants from Database.", "Success", JOptionPane.INFORMATION_MESSAGE);
return d;
}
}catch(SQLException e) {
JOptionPane.showMessageDialog(null, "Error while reading from Database "+e.getMessage(), "DATABASE ERROR", JOptionPane.ERROR_MESSAGE);
}catch(Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "An unexpected error occured: "+e.toString(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
finally {
return null; //Should never be reached
}
}
public void deletePartByID(int id) {
try {
StringBuilder sb = new StringBuilder("DELETE FROM ");
sb.append(event);
sb.append(" WHERE id = ?;");
PreparedStatement ps = con.prepareStatement(sb.toString());
ps.setInt(1, id);
ps.executeUpdate();
}catch(Exception e) {
JOptionPane.showMessageDialog(null, "Error while reading from Database "+e.getMessage(), "DATABASE ERROR", JOptionPane.ERROR_MESSAGE);
}
}
public void delete() {
try {
String sql = "DROP TABLE "+this.event+";";
String sq2 = "DELETE FROM zzzmemory WHERE name = '"+this.event+"';";
Statement stm = con.createStatement();
stm.executeUpdate(sql);
stm.executeUpdate(sq2);
}catch(Exception e) {
JOptionPane.showMessageDialog(null, "Error while updating Database "+e.getMessage(), "DATABASE ERROR", JOptionPane.ERROR_MESSAGE);
}
}
// Returns an array containing the names of all tables in the Database,
// i.e. the names of all events
public String[] getEventsFromDB() {
try {
ArrayList<String> elist = new ArrayList<String>();
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery("show tables;");
while(rs.next()) {
String tmp = rs.getString(1);
if(!tmp.startsWith("zzz")) {
elist.add(tmp);
}
}
String[] earr = new String[elist.size()];
earr = elist.toArray(earr);
return earr;
}catch(Exception e) {
JOptionPane.showMessageDialog(null, "Error while reading from Database "+e.getMessage(), "DATABASE ERROR", JOptionPane.ERROR_MESSAGE);
return null;
}
}
//Checks whether there is already a table with the same name
public boolean eventExists() {
String[] ea = this.getEventsFromDB();
for(int i=0; i<ea.length; i++) {
if(ea[i].equals(this.event)) {
return true;
}
}
return false;
}
// Stored a Dating-Object as BLOB in Database
public void storeInDB(Dating d) {
try {
PreparedStatement ps = con.prepareStatement("INSERT INTO zzzmemory VALUES (?,?,?);");
File tmp = File.createTempFile(d.getName(), ".tmp");
FileOutputStream fout = new FileOutputStream(tmp);
ObjectOutputStream oout = new ObjectOutputStream(fout);
oout.writeObject(d);
oout.close();
fout.close();
FileInputStream fin = new FileInputStream(tmp);
ps.setString(1, d.getName());
ps.setInt(2, d.getMaxPart());
ps.setBinaryStream(3, fin);
ps.executeUpdate();
fin.close();
}catch(Exception e) {
JOptionPane.showMessageDialog(null, "An unexpected error occured while writing BLOB to DB: "+Processing.getLineSep()+e.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
//Updates BLOB on Server
public void updateFile(Dating d) {
try {
PreparedStatement ps = con.prepareStatement("UPDATE zzzmemory SET file=? WHERE name=?;");
File tmp = File.createTempFile(d.getName(), ".tmp");
FileOutputStream fout = new FileOutputStream(tmp);
ObjectOutputStream oout = new ObjectOutputStream(fout);
oout.writeObject(d);
oout.close();
fout.close();
FileInputStream fin = new FileInputStream(tmp);
ps.setBinaryStream(1, fin);
ps.setString(2, d.getName());
ps.executeUpdate();
fin.close();
}catch(Exception e) {
JOptionPane.showMessageDialog(null, "An unexpected error occured while updating BLOB in DB: "+Processing.getLineSep()+e.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
// Reads the BLOB from the Database and returns it as a Dating-Object
public Dating readDatingFromDB(String name) {
try {
File tmp = File.createTempFile(name+"_in", ".tmp");
FileOutputStream out = new FileOutputStream(tmp);
PreparedStatement ps = con.prepareStatement("SELECT file FROM zzzmemory WHERE name = ?;");
ps.setString(1, name);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
InputStream in = rs.getBinaryStream("file");
byte[] buffer = new byte[1024];
while(in.read(buffer)>0) {
out.write(buffer);
}
}
FileInputStream fin = new FileInputStream(tmp);
ObjectInputStream oin = new ObjectInputStream(fin);
Dating d = (Dating) oin.readObject();
oin.close();
fin.close();
return d;
}catch(Exception e) {
JOptionPane.showMessageDialog(null, "An unexpected error occured while reading BLOB from DB: "+Processing.getLineSep()+e.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
return null;
}
}
// Closes Database Connection
public void close() {
try {
con.close();
}catch(Exception e) {
JOptionPane.showMessageDialog(null, "Error while closing Database Connection: "+e.getMessage(), "CONNECTION ERROR", JOptionPane.ERROR_MESSAGE);
}
}
}