diff --git a/src/main/java/de/hsel/itech/db/Database.java b/src/main/java/de/hsel/itech/db/Database.java index f5267a1..3b9267e 100644 --- a/src/main/java/de/hsel/itech/db/Database.java +++ b/src/main/java/de/hsel/itech/db/Database.java @@ -4,6 +4,9 @@ import de.hsel.itech.config.Configuration; import org.jetbrains.annotations.Nullable; import org.mariadb.jdbc.MariaDbPoolDataSource; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -18,15 +21,38 @@ import java.sql.SQLException; */ public class Database { + private static Database instance; + + public static Database getInstance() { + if(instance == null) + instance = new Database(); + return instance; + } + private MariaDbPoolDataSource dataSource; - public Database() { + private Database() { Configuration config = Configuration.get(); dataSource = new MariaDbPoolDataSource("jdbc:mysql://" + config.getDatabase().getHostname() + ":" + config.getDatabase().getPort() + "/" + config.getDatabase().getDatabase()); try { dataSource.setUser(config.getDatabase().getUsername()); dataSource.setPassword(config.getDatabase().getPassword()); dataSource.initialize(); + + Connection connection = getConnection(); + assert connection != null; + try(BufferedReader br = new BufferedReader(new FileReader(getClass().getClassLoader().getResource("database.sql").getFile()))) { + for (String line = br.readLine(); line != null; line = br.readLine()) { + PreparedStatement statement = connection.prepareStatement(line); + statement.executeUpdate(); + statement.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + finally { + connection.close(); + } } catch (SQLException e) { e.printStackTrace(); } diff --git a/src/main/java/de/hsel/itech/servlet/Database.java b/src/main/java/de/hsel/itech/servlet/Database.java index 7855d1f..a5c9a15 100644 --- a/src/main/java/de/hsel/itech/servlet/Database.java +++ b/src/main/java/de/hsel/itech/servlet/Database.java @@ -33,7 +33,7 @@ public class Database extends HttpServlet { out.println("