-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbConnect.java
More file actions
39 lines (33 loc) · 1.16 KB
/
Copy pathDbConnect.java
File metadata and controls
39 lines (33 loc) · 1.16 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
package com.db;
import java.sql.*;
public class DbConnect {
private static final String URL = "jdbc:mysql://localhost:3306/hospital";
private static final String USERNAME = "root";
private static final String PASSWORD = "root";
private static final String DRIVER_CLASS = "com.mysql.cj.jdbc.Driver";
static {
try {
Class.forName(DRIVER_CLASS);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("Error loading MySQL JDBC Driver", e);
}
}
public static Connection getConnection() {
try {
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Error connecting to the database", e);
}
}
public static void close(Connection connection, PreparedStatement stmt, ResultSet rs) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}