Skip to content

Commit 90808c5

Browse files
committed
Changed DriverManager to BasicDataSource
Added Maven Shade Plugin to package dependencies with jar Signed-off-by: Joshua Gager <jlgager@outlook.com>
1 parent 6cdd5f6 commit 90808c5

File tree

3 files changed

+60
-16
lines changed

3 files changed

+60
-16
lines changed

pom.xml

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,37 @@
1818
<target>1.8</target>
1919
</configuration>
2020
</plugin>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-shade-plugin</artifactId>
24+
<version>3.0.0</version>
25+
<executions>
26+
<!-- Run shade goal on package phase -->
27+
<execution>
28+
<phase>package</phase>
29+
<goals>
30+
<goal>shade</goal>
31+
</goals>
32+
<configuration>
33+
<transformers>
34+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
35+
<manifestEntries>
36+
<Main-Class>com.jgcomptech.tools.demo.Main</Main-Class>
37+
<Build-Number>123</Build-Number>
38+
</manifestEntries>
39+
</transformer>
40+
</transformers>
41+
</configuration>
42+
</execution>
43+
</executions>
44+
</plugin>
2145
</plugins>
2246
</build>
2347
<dependencies>
2448
<dependency>
2549
<groupId>org.apache.commons</groupId>
2650
<artifactId>commons-lang3</artifactId>
27-
<version>3.5</version>
51+
<version>RELEASE</version>
2852
</dependency>
2953
<dependency>
3054
<groupId>org.jetbrains</groupId>
@@ -34,12 +58,12 @@
3458
<dependency>
3559
<groupId>net.java.dev.jna</groupId>
3660
<artifactId>jna-platform</artifactId>
37-
<version>4.3.0</version>
61+
<version>RELEASE</version>
3862
</dependency>
3963
<dependency>
4064
<groupId>net.java.dev.jna</groupId>
4165
<artifactId>jna</artifactId>
42-
<version>4.3.0</version>
66+
<version>RELEASE</version>
4367
</dependency>
4468
<dependency>
4569
<groupId>junit</groupId>
@@ -49,13 +73,19 @@
4973
<dependency>
5074
<groupId>org.xerial</groupId>
5175
<artifactId>sqlite-jdbc</artifactId>
52-
<version>3.16.1</version>
76+
<version>RELEASE</version>
5377
</dependency>
5478
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
5579
<dependency>
5680
<groupId>com.h2database</groupId>
5781
<artifactId>h2</artifactId>
58-
<version>1.4.194</version>
82+
<version>RELEASE</version>
83+
</dependency>
84+
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
85+
<dependency>
86+
<groupId>org.apache.commons</groupId>
87+
<artifactId>commons-dbcp2</artifactId>
88+
<version>RELEASE</version>
5989
</dependency>
6090
</dependencies>
6191
</project>

src/main/java/com/jgcomptech/tools/databasetools/Database.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,30 @@
55
import com.jgcomptech.tools.dialogs.MessageBox;
66
import com.jgcomptech.tools.dialogs.MessageBoxButtons;
77
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;
811

12+
import javax.naming.InitialContext;
13+
import javax.naming.NamingException;
14+
import javax.sql.DataSource;
915
import java.sql.*;
1016
import java.util.ArrayList;
1117

1218
/**
1319
* Database object that allows communication with a SQL database
1420
*/
1521
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;
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!\nPlease Close any open connections!",
123+
final DialogResult result = MessageBox.show("\"" + dbName + "\" is currently in use!\nPlease 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

src/main/java/com/jgcomptech/tools/demo/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private static void Print(KeyPair keyPair) {
1919
public static void main(String[] args) {
2020
Print("------------------------------------");
2121
Print("------Java Ultimate Tools v1.2------");
22-
Print("-------Created by J&G CompTech-------");
22+
Print("-------Created by J&G CompTech------");
2323
Print("------------------------------------");
2424
Print("Loading Computer Info Please Wait...");
2525
ComputerInfo compinfo = new ComputerInfo();

0 commit comments

Comments
 (0)