EAA_MOD/src/main/java/net/saltymc/eaa/util/database/Postgre.java
2021-06-12 18:00:44 +02:00

55 lines
1.3 KiB
Java

package net.saltymc.eaa.util.database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Postgre {
private final String url = "jdbc:postgresql://localhost/myDB";
private final String user = "postgres";
private final String password = "root";
private static Postgre postgre;
public Connection connection;
public Postgre(){
this.connection = connect();
}
static Postgre getInstance(){
if (postgre == null)
postgre = new Postgre();
return postgre;
}
/**
* Connect to the PostgreSQL database
*
* @return a Connection object
*/
public Connection connect() {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
Class.forName("org.postgresql.Driver");
if (conn != null) {
System.out.println("Connected to the PostgreSQL server successfully.");
} else {
System.out.println("Failed to make connection!");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("PostgreSQL JDBC driver not found.");
e.printStackTrace();
}
return conn;
}
}