-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseConnector.java
More file actions
44 lines (38 loc) · 1.6 KB
/
Copy pathDatabaseConnector.java
File metadata and controls
44 lines (38 loc) · 1.6 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
import java.sql.Connection;
import java.sql.DriverManager;
/**
* DatabaseConnector - Centralized database connection manager
* This class provides a single point of connection to the PostgreSQL database
*/
public class DatabaseConnector {
// Database credentials - CHANGE THESE TO MATCH YOUR SETUP
private static final String URL = "jdbc:postgresql://localhost:5432/Synapse Fest"; // ← Change this!
private static final String USER = "postgres"; // ← Change if different
private static final String PASSWORD = "postgres"; // ← Change this!
/**
* Creates and returns a connection to the database
* @return Connection object
* @throws SQLException if connection fails
*/
public static Connection getConnection() throws SQLException {
try {
// Load PostgreSQL JDBC Driver
Class.forName("org.postgresql.Driver");
return DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
throw new SQLException("PostgreSQL JDBC Driver not found. Did you add the .jar file?", e);
}
}
/**
* Test the database connection
*/
public static void testConnection() {
try (Connection conn = getConnection()) {
System.out.println("✓ Database connection successful!");
System.out.println("✓ Connected to: " + conn.getMetaData().getDatabaseProductName());
} catch (SQLException e) {
System.err.println("✗ Database connection failed!");
System.err.println("Error: " + e.getMessage());
}
}
}