-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseConnection.java
More file actions
35 lines (27 loc) · 1.25 KB
/
DatabaseConnection.java
File metadata and controls
35 lines (27 loc) · 1.25 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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnection {
private static final String URL =
"jdbc:mysql://127.0.0.1:3306/library_management?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
private static final String USER = "root";
private static final String PASSWORD = "root@123"; // ← change this
public static Connection getConnection() {
try {
// Load MySQL driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Create and return connection
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/library_management", "root", "root@123");
System.out.println("Database connected successfully!");
return connection;
} catch (ClassNotFoundException e) {
System.out.println("MySQL JDBC Driver not found.");
e.printStackTrace();
throw new RuntimeException("Driver error");
} catch (SQLException e) {
System.out.println("Database connection failed.");
e.printStackTrace();
throw new RuntimeException("Connection error");
}
}
}