-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryConnection.java
85 lines (75 loc) · 2.14 KB
/
QueryConnection.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
package bbtrial.nl.logicgate.ace;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class QueryConnection {
private Query query;
private Connection conn;
public QueryConnection(){
query = null;
conn = null;
}
public Query getQuery(){
return query;
}
//This method added for demo
public boolean establishConnection(String testConnName){
conn = new FakeConnection();
if(testConnName.equals("storage")){
query = new FakeStorageQuery(conn, System.err);
}
if(testConnName.equals("docDB")){
query = new FakeDocQuery(conn, System.err);
}
if(testConnName.equals("nDB")){
query = new FakeNQuery(conn, System.err);
}
if(testConnName.equals("pbDB")){
query = new FakePBQuery(conn, System.err);
}
return true;
}
public boolean establishConnection(String dbDriver, String dbURL, String loginName, String password){
conn = connect(dbDriver, dbURL, loginName, password);
if(conn!=null){
query = new Query(conn, System.err);
return true;
} else {return false;}
}
private Connection connect(String dbDriver, String dbURL, String loginName, String password){
Connection conn = null;
// start database driver and connect to db
try{
Class.forName(dbDriver);
conn = DriverManager.getConnection(dbURL, loginName, password);
}
catch(ClassNotFoundException e){
// driver not found
new WriteFile().appendFile(BriefBot.ERROR_FILE, "Driver not found, cannot connect to " + dbURL + "\n");
}
catch(SQLException e){
new WriteFile().appendFile(BriefBot.ERROR_FILE, "SQL error: cannot connect to " + dbURL + "\n");
}
return conn;
}
public boolean isConnected(){
if(conn==null){
return false;
} else {
return true;
}
}
public boolean close() {
try{
if(conn != null){
conn.close();
conn = null;
}
}
catch(SQLException e){ // something happened, perhaps check if connection was already closed?
new WriteFile().appendFile(BriefBot.ERROR_FILE, "storage close: Error closing database connection \n");
return false;
}
return true;
}
}