-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseConnection.java
More file actions
71 lines (61 loc) · 2.54 KB
/
DatabaseConnection.java
File metadata and controls
71 lines (61 loc) · 2.54 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.sql.*;
public class DatabaseConnection {
private static final String URL = "jdbc:mysql://localhost:3306/medical_db";
private static final String USERNAME = "root";
private static final String PASSWORD = "1234"; // Change this to your MySQL password
public static Connection getConnection() throws SQLException {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
throw new SQLException("MySQL Driver not found", e);
}
}
public static void initializeDatabase() {
try (Connection conn = getConnection()) {
Statement stmt = conn.createStatement();
// Create database if not exists
stmt.executeUpdate("CREATE DATABASE IF NOT EXISTS medical_db");
stmt.executeUpdate("USE medical_db");
// Create tables
stmt.executeUpdate("""
CREATE TABLE IF NOT EXISTS doctors (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
specialization VARCHAR(100),
phone VARCHAR(15),
email VARCHAR(100)
)
""");
stmt.executeUpdate("""
CREATE TABLE IF NOT EXISTS patients (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
age INT,
phone VARCHAR(15),
address TEXT
)
""");
stmt.executeUpdate("""
CREATE TABLE IF NOT EXISTS appointments (
id INT AUTO_INCREMENT PRIMARY KEY,
patient_name VARCHAR(100) NOT NULL,
doctor_name VARCHAR(100) NOT NULL,
appointment_date DATE,
appointment_time TIME
)
""");
stmt.executeUpdate("""
CREATE TABLE IF NOT EXISTS departments (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
head_doctor VARCHAR(100),
location VARCHAR(100)
)
""");
System.out.println("Database initialized successfully!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}