-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabaseConnection.java
More file actions
57 lines (48 loc) · 1.33 KB
/
Copy pathDatabaseConnection.java
File metadata and controls
57 lines (48 loc) · 1.33 KB
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
package lms;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* Manages database connection
*/
public class DatabaseConnection {
/**
* JDBC database connection String.
*/
private static String url = "jdbc:mysql://127.0.0.1:3306/librarymanagementsystem";
private static String username = "root";
private static String password = "P0PcOrN*14";
//private static String otherPassword = "d@nNsH3rM2020";
/**
* Opens database connection
* @return DB connection
*/
public static Connection openDatabase() {
Connection connection = null;
try {
// load the appropriate MySQL driver
Class.forName("com.mysql.cj.jdbc.Driver");
// create connection using JDBC driver
connection = DriverManager.getConnection(
DatabaseConnection.url,
DatabaseConnection.username,
DatabaseConnection.password);
} catch (ClassNotFoundException CNFe) {
CNFe.printStackTrace();
} catch (SQLException SQLe) {
SQLe.printStackTrace();
}
return connection;
}
/**
* Closes given database connection.
* @param connection - connection to be closed
*/
public static void closeDatabase(Connection connection) {
try {
connection.close();
} catch (SQLException SQLe){
SQLe.printStackTrace();
}
}
}