5
5
import com .jgcomptech .tools .dialogs .MessageBox ;
6
6
import com .jgcomptech .tools .dialogs .MessageBoxButtons ;
7
7
import com .jgcomptech .tools .dialogs .MessageBoxIcon ;
8
+ import org .apache .commons .dbcp2 .BasicDataSource ;
9
+ import org .apache .commons .dbcp2 .cpdsadapter .DriverAdapterCPDS ;
10
+ import org .apache .commons .dbcp2 .datasources .SharedPoolDataSource ;
8
11
12
+ import javax .naming .InitialContext ;
13
+ import javax .naming .NamingException ;
14
+ import javax .sql .DataSource ;
9
15
import java .sql .*;
10
16
import java .util .ArrayList ;
11
17
12
18
/**
13
19
* Database object that allows communication with a SQL database
14
20
*/
15
21
public class Database implements AutoCloseable {
16
- private java .sql .Connection conn = null ;
17
- private String connString = "" ;
22
+ private java .sql .Connection conn ;
23
+ private String connString ;
18
24
final private String username ;
19
25
final private String password ;
20
26
final private String dbName ;
21
27
final private DatabaseType dbType ;
22
- private Info info = null ;
23
- private Connection connection = null ;
24
- private Tasks tasks = null ;
28
+ private String dbDriver ;
29
+ private Info info ;
30
+ private Connection connection ;
31
+ private Tasks tasks ;
25
32
26
33
/**
27
34
* Creates a database object with the specified parameters
@@ -36,9 +43,11 @@ public Database(String dbFilePath, String username, String password, DatabaseTyp
36
43
37
44
case H2 :
38
45
connString = "jdbc:h2:" + dbFilePath ;
46
+ dbDriver = "org.h2.Driver" ;
39
47
break ;
40
48
case SQLite :
41
49
connString = "jdbc:sqlite:" + dbFilePath ;
50
+ dbDriver = "org.sqlite.JDBC" ;
42
51
break ;
43
52
}
44
53
@@ -97,16 +106,21 @@ public class Connection {
97
106
* @param showStatusAlert Specifies if message box should be shown
98
107
* @throws SQLException if error occurs
99
108
*/
100
- public void connect (boolean showStatusAlert ) throws SQLException {
101
- try {
102
- conn = DriverManager .getConnection (connString , username , password );
109
+ public void connect (boolean showStatusAlert ) throws SQLException {
110
+ try (final BasicDataSource ds = new BasicDataSource ()) {
111
+ ds .setDriverClassName (dbDriver );
112
+ ds .setUrl (connString );
113
+ ds .setUsername (username );
114
+ ds .setPassword (password );
115
+
116
+ conn = ds .getConnection ();
103
117
104
118
if (showStatusAlert ) MessageBox .show ("Connection to database has been established." , "Database Alert" ,
105
119
"Database Alert" , MessageBoxIcon .INFORMATION );
106
120
} catch (SQLException e ) {
107
121
if (showStatusAlert ) {
108
122
if (e .getMessage ().contains ("Database may be already in use" )) {
109
- DialogResult result = MessageBox .show ("\" " + dbName + "\" is currently in use!\n Please Close any open connections!" ,
123
+ final DialogResult result = MessageBox .show ("\" " + dbName + "\" is currently in use!\n Please Close any open connections!" ,
110
124
"Error!" , "Database Error" , MessageBoxButtons .RetryCancel , MessageBoxIcon .ERROR );
111
125
if (result .equals (DialogResult .RETRY )) connect (showStatusAlert );
112
126
if (result .equals (DialogResult .CANCEL )) throw e ;
@@ -196,7 +210,7 @@ public boolean TableExists(String tableName) throws SQLException {
196
210
*/
197
211
public ArrayList getTablesList () throws SQLException {
198
212
199
- ArrayList <String > listOfTables = new ArrayList <String >();
213
+ ArrayList <String > listOfTables = new ArrayList <>();
200
214
201
215
DatabaseMetaData md = conn .getMetaData ();
202
216
0 commit comments