DBConnect + Postgreshelper + Load JDBC config from conf.ini
This commit is contained in:
@ -5,33 +5,90 @@ import org.ini4j.Wini;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Dient dem ein und auslesen von .ini Dateien
|
||||
*/
|
||||
public class ConfigHelper {
|
||||
|
||||
public static String standartConfig = "conf.ini";
|
||||
public static String standartConfig = "conf.ini"; // Config.ini befindet sich im Root Verzeichnis
|
||||
|
||||
|
||||
public static int getIniInt(String filename, String sectionName, String optionName) throws IOException {
|
||||
Wini ini = new Wini(new File(filename));
|
||||
/**
|
||||
* Liest einen Int aus der Config aus
|
||||
* @param filename Speicherort
|
||||
* @param sectionName Sections Name
|
||||
* @param optionName Options Name
|
||||
* @return Den angeforderten String
|
||||
*/
|
||||
public static int getIniInt(String filename, String sectionName, String optionName){
|
||||
Wini ini = null;
|
||||
try {
|
||||
ini = new Wini(new File(filename));
|
||||
} catch (IOException e) {
|
||||
LogHelper.error(e.getMessage(), e.getClass().getName());
|
||||
}
|
||||
assert ini != null;
|
||||
return ini.get(sectionName, optionName, int.class);
|
||||
}
|
||||
|
||||
public static String getIniString(String filename, String sectionName, String optionName) throws IOException {
|
||||
Wini ini = new Wini(new File(filename));
|
||||
/**
|
||||
* Liest einen String aus der Config aus
|
||||
* @param filename Speicherort
|
||||
* @param sectionName Sections Name
|
||||
* @param optionName Options Name
|
||||
* @return Den angeforderten String
|
||||
*/
|
||||
public static String getIniString(String filename, String sectionName, String optionName){
|
||||
Wini ini = null;
|
||||
try {
|
||||
ini = new Wini(new File(filename));
|
||||
} catch (IOException e) {
|
||||
LogHelper.error(e.getMessage(), e.getClass().getName());
|
||||
}
|
||||
assert ini != null;
|
||||
return ini.get(sectionName, optionName, String.class);
|
||||
}
|
||||
|
||||
public static void setIniString(String filename, String sectionName, String optionName, String value) throws IOException {
|
||||
Wini ini = new Wini(new File(filename));
|
||||
ini.put(sectionName, optionName, value);
|
||||
ini.store();
|
||||
/**
|
||||
* Setzt ein String in der Config
|
||||
* @param filename Speicherort
|
||||
* @param sectionName Sections Name
|
||||
* @param optionName Options Name
|
||||
* @param value Wert der eingetragen werden soll
|
||||
*/
|
||||
public static void setIniString(String filename, String sectionName, String optionName, String value) {
|
||||
Wini ini = null;
|
||||
try {
|
||||
ini = new Wini(new File(filename));
|
||||
ini.put(sectionName, optionName, value);
|
||||
ini.store();
|
||||
} catch (IOException e) {
|
||||
LogHelper.error(e.getMessage(), e.getClass().getName());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void setIniInt(String filename, String sectionName, String optionName, int value) throws IOException {
|
||||
Wini ini = new Wini(new File(filename));
|
||||
ini.put(sectionName, optionName, value);
|
||||
ini.store();
|
||||
/**
|
||||
* Setzt ein int in der Config
|
||||
* @param filename Speicherort
|
||||
* @param sectionName Sections Name
|
||||
* @param optionName Options Name
|
||||
* @param value Wert der eingetragen werden soll
|
||||
*/
|
||||
public static void setIniInt(String filename, String sectionName, String optionName, int value){
|
||||
Wini ini = null;
|
||||
try {
|
||||
ini = new Wini(new File(filename));
|
||||
ini.put(sectionName, optionName, value);
|
||||
ini.store();
|
||||
} catch (IOException e) {
|
||||
LogHelper.error(e.getMessage(), e.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt den namen des standard Config file zurück
|
||||
* @return Name des standard config files
|
||||
*/
|
||||
public static String getStandartConfig() {
|
||||
return standartConfig;
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import org.apache.log4j.*;
|
||||
*/
|
||||
public class LogHelper{
|
||||
/**
|
||||
* Logging in file und Console
|
||||
* Log info in file und Console
|
||||
* @param msg Nachricht in dem Log
|
||||
* @param name Name des Log Eintrags
|
||||
*/
|
||||
@ -15,20 +15,40 @@ public class LogHelper{
|
||||
getLog(name).info(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log info in file und Console
|
||||
* @param msg Nachricht in dem Log
|
||||
* @param name Name des Log Eintrags
|
||||
*/
|
||||
public static void warn(String msg, String name){
|
||||
getLog(name).warn(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log info in file und Console
|
||||
* @param msg Nachricht in dem Log
|
||||
* @param name Name des Log Eintrags
|
||||
*/
|
||||
public static void error(String msg, String name){
|
||||
getLog(name).error(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log info in file und Console
|
||||
* @param msg Nachricht in dem Log
|
||||
* @param name Name des Log Eintrags
|
||||
*/
|
||||
public static void fatal(String msg, String name){
|
||||
getLog(name).fatal(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instanziert den Logger
|
||||
* @param name Name des Loggers
|
||||
* @return Den Logger
|
||||
*/
|
||||
private static Logger getLog(String name){
|
||||
return Logger.getLogger(name); // Initialiesiert das Logging
|
||||
return Logger.getLogger(name); // Instanziert den Logger
|
||||
}
|
||||
|
||||
}
|
||||
|
60
src/tourplaner/business/PostgresHelper.java
Normal file
60
src/tourplaner/business/PostgresHelper.java
Normal file
@ -0,0 +1,60 @@
|
||||
package tourplaner.business;
|
||||
|
||||
|
||||
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://" + ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "db", "url") + ":" + ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "db", "port") + "/" + ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "db", "dbname"),
|
||||
ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "db", "user"), ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "db", "pw"));
|
||||
} catch (Exception e) {
|
||||
LogHelper.error(e.getMessage(), e.getClass().getName());
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
/**
|
||||
* Führt ein Sql statement ohne rückgabe aus, mit message nachricht
|
||||
* @param sql Sql command
|
||||
* @param message Mesasage die vor dem Durchführen angezeigt wird
|
||||
* @return True bei erfolg, sonst false
|
||||
*/
|
||||
public static boolean executeUpdateMessage(String sql, String message){
|
||||
LogHelper.info(message, "PostgresHelper");
|
||||
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) {
|
||||
LogHelper.error(e.getMessage(), e.getClass().getName());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ package tourplaner.business;
|
||||
|
||||
import javafx.stage.Stage;
|
||||
import org.apache.log4j.Logger;
|
||||
import tourplaner.data.DbConnect;
|
||||
import tourplaner.ui.PresenterMain;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -17,13 +18,10 @@ public class TourPlaner{
|
||||
* @param primaryStage Stage für den Presenter
|
||||
*/
|
||||
public TourPlaner(Stage primaryStage){
|
||||
try {
|
||||
String startText = ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "start", "message");
|
||||
LogHelper.info(startText, "TourPlaner");
|
||||
PresenterMain presenterMain = new PresenterMain(primaryStage); // Start Presenter/GUI
|
||||
} catch (IOException e) {
|
||||
LogHelper.fatal(e.getMessage(), "TourPlanerConfig");
|
||||
}
|
||||
String startText = ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "start", "message");
|
||||
LogHelper.info(startText, "TourPlaner");
|
||||
PresenterMain presenterMain = new PresenterMain(primaryStage); // Start Presenter/GUI
|
||||
new DbConnect().init();
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,37 @@
|
||||
package tourplaner.data;
|
||||
|
||||
import tourplaner.business.PostgresHelper;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Verwaltet die Datenbankverbindung zu dem Postgres Server
|
||||
*/
|
||||
public class DbConnect {
|
||||
private Connection c;
|
||||
private Statement stmt;
|
||||
|
||||
|
||||
/**
|
||||
* Erstellt alle Beispieldaten und simuliert somit den
|
||||
* Verbindungsaufbau zu einer DB
|
||||
*/
|
||||
public DbConnect() {
|
||||
this.c = null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Erstellt alle Tabellen die für den Betrieb der Software bennötigt werden
|
||||
* @return True bei erfolg, sonst error
|
||||
*/
|
||||
public boolean init() {
|
||||
// TODO: 26.02.2021 Alle sql im init sind noch falsch
|
||||
ArrayList<Boolean> errors = new ArrayList<>();
|
||||
errors.add(PostgresHelper.executeUpdateMessage("CREATE TABLE IF NOT EXISTS USERS (username TEXT PRIMARY KEY NOT NULL, nachname TEXT NOT NULL, email TEXT NOT NULL, password TEXT NOT NULL, bio TEXT, image TEXT, coins integer default 20 not null)", "User Table created"));
|
||||
return !errors.contains(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -22,11 +22,12 @@ public class PresenterMain {
|
||||
public PresenterMain(Stage primaryStage){
|
||||
Parent root = null;
|
||||
try {
|
||||
root = FXMLLoader.load(getClass().getResource("../ui/tourplaner.fxml"));
|
||||
root = FXMLLoader.load(getClass().getResource("tourplaner.fxml"));
|
||||
primaryStage.setTitle("DerGeorg Touren Planer");
|
||||
primaryStage.setScene(new Scene(root, 600, 600));
|
||||
primaryStage.show();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
LogHelper.error(e.getMessage(), "PresenterMain");
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user