61 lines
1.7 KiB
Java
61 lines
1.7 KiB
Java
package at.reisinger.server.helper;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.Statement;
|
|
|
|
/**
|
|
* Hilfsfunktionen für die verwendung der Postgres DB
|
|
*/
|
|
public class PostgresHelper {
|
|
|
|
/**
|
|
* Verbindet mit der Datenbank
|
|
* @return Das Connection Objekt
|
|
*/
|
|
public static Connection con() {
|
|
Connection c = null;
|
|
try {
|
|
Class.forName("org.postgresql.Driver");
|
|
c = DriverManager
|
|
.getConnection("jdbc:postgresql://postgres.dergeorg.at:5432/mtcg",
|
|
"user", "user");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
|
System.exit(0);
|
|
}
|
|
return c;
|
|
}
|
|
|
|
/**
|
|
* Führt ein Sql statement ohne rückgabe aus, mit message nachricht
|
|
* @param sql Sql command
|
|
* @return True bei erfolg, sonst false
|
|
*/
|
|
public static boolean executeUpdateMessage(String sql, String message){
|
|
System.out.println(message);
|
|
return executeUpdate(sql);
|
|
}
|
|
|
|
/**
|
|
* Führt ein Sql statement ohne rückgabe aus
|
|
* @param sql Sql command
|
|
* @return True bei erfolg, sonst false
|
|
*/
|
|
public static boolean executeUpdate(String sql){
|
|
Connection c = con();
|
|
Statement stmt;
|
|
try {
|
|
stmt = c.createStatement();
|
|
stmt.executeUpdate(sql);
|
|
stmt.close();
|
|
c.close();
|
|
} catch (Exception e) {
|
|
System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|