-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseConnection.java
More file actions
45 lines (40 loc) · 1.55 KB
/
DatabaseConnection.java
File metadata and controls
45 lines (40 loc) · 1.55 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
package com.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnection {
private static final String URL = "jdbc:postgresql://localhost:5432/testdb"; // Replace 'testdb' with your database name.
private static final String USER = "postgres"; // Replace with your PostgreSQL username.
private static final String PASSWORD = "Nidhidgowdacse@2027"; // Replace with your PostgreSQL password.
/**
* Establishes and returns a connection to the PostgreSQL database.
*
* @return A Connection object or null if the connection fails.
*/
public static Connection getConnection() {
try {
// Optional: Load the JDBC driver (newer versions of JDBC auto-register it)
// Class.forName("org.postgresql.Driver");
return DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
System.err.println("Database connection failed!");
e.printStackTrace();
return null;
}
}
/**
* Closes the provided Connection to release resources.
*
* @param connection The Connection to be closed.
*/
public static void closeConnection(Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
System.err.println("Failed to close connection!");
e.printStackTrace();
}
}
}
}