55import com .jgcomptech .tools .dialogs .MessageBox ;
66import com .jgcomptech .tools .dialogs .MessageBoxButtons ;
77import 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 ;
811
12+ import javax .naming .InitialContext ;
13+ import javax .naming .NamingException ;
14+ import javax .sql .DataSource ;
915import java .sql .*;
1016import java .util .ArrayList ;
1117
1218/**
1319 * Database object that allows communication with a SQL database
1420 */
1521public 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 ;
1824 final private String username ;
1925 final private String password ;
2026 final private String dbName ;
2127 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 ;
2532
2633 /**
2734 * Creates a database object with the specified parameters
@@ -36,9 +43,11 @@ public Database(String dbFilePath, String username, String password, DatabaseTyp
3643
3744 case H2 :
3845 connString = "jdbc:h2:" + dbFilePath ;
46+ dbDriver = "org.h2.Driver" ;
3947 break ;
4048 case SQLite :
4149 connString = "jdbc:sqlite:" + dbFilePath ;
50+ dbDriver = "org.sqlite.JDBC" ;
4251 break ;
4352 }
4453
@@ -97,16 +106,21 @@ public class Connection {
97106 * @param showStatusAlert Specifies if message box should be shown
98107 * @throws SQLException if error occurs
99108 */
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 ();
103117
104118 if (showStatusAlert ) MessageBox .show ("Connection to database has been established." , "Database Alert" ,
105119 "Database Alert" , MessageBoxIcon .INFORMATION );
106120 } catch (SQLException e ) {
107121 if (showStatusAlert ) {
108122 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!" ,
110124 "Error!" , "Database Error" , MessageBoxButtons .RetryCancel , MessageBoxIcon .ERROR );
111125 if (result .equals (DialogResult .RETRY )) connect (showStatusAlert );
112126 if (result .equals (DialogResult .CANCEL )) throw e ;
@@ -196,7 +210,7 @@ public boolean TableExists(String tableName) throws SQLException {
196210 */
197211 public ArrayList getTablesList () throws SQLException {
198212
199- ArrayList <String > listOfTables = new ArrayList <String >();
213+ ArrayList <String > listOfTables = new ArrayList <>();
200214
201215 DatabaseMetaData md = conn .getMetaData ();
202216
0 commit comments