This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySQLConnect.java
53 lines (44 loc) · 1.54 KB
/
MySQLConnect.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
package databases.mysql;
import utility.PropertiesManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* A MySQL database client offering functionalities to connect and disconnect database
*/
public class MySQLConnect {
/**
* Database connection properties
*/
private static final String DB_DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://" +
PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_IP) + ":" +
PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_PORT) + "?" +
"useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&" +
"user=" + PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_USR) + "&" +
"password=" + PropertiesManager.getInstance().getProperty(PropertiesManager.MYSQL_PASS);
/**
* Connects to MySQL database
* @return instance of db connection
*/
public static Connection connectDatabase() {
try {
Class.forName(DB_DRIVER);
return DriverManager.getConnection(DB_URL);
} catch (ClassNotFoundException | SQLException e) {
System.err.println("Could not open DB connection: " + e.getMessage());
return null;
}
}
/**
* Closes a MySQL database connection
* @param connection MySQL connection to close
*/
public static void closeConnection(Connection connection) {
try {
connection.close();
} catch (SQLException e) {
System.err.println("Could not close DB connection: " + e.getMessage());
}
}
}