diff --git a/src/main/java/pl/nightdev701/database/DatabaseConnector.java b/src/main/java/pl/nightdev701/database/DatabaseConnector.java index d58d06d..1e4bf9e 100644 --- a/src/main/java/pl/nightdev701/database/DatabaseConnector.java +++ b/src/main/java/pl/nightdev701/database/DatabaseConnector.java @@ -22,7 +22,6 @@ public class DatabaseConnector { private final String user; private final String password; private final String database; - private final String table; private final DatabaseType databaseType; private Connection connection; @@ -31,7 +30,6 @@ public DatabaseConnector(DatabaseFormular formular, DatabaseType databaseType) { this.user = formular.user(); this.password = formular.password(); this.database = formular.database(); - this.table = formular.table(); this.databaseType = databaseType; if (databaseType == DatabaseType.MYSQL) { @@ -42,60 +40,42 @@ public DatabaseConnector(DatabaseFormular formular, DatabaseType databaseType) { } /* connect to database */ - public void connect() { + public void connect() throws SQLException { System.out.println("Connect to database, with the type \"" + databaseType.name() + "\"!"); - try { - if (databaseType == DatabaseType.MYSQL) { - connection = DriverManager.getConnection("jdbc:mysql://" + - ip + ":" + port + "/" + database + "?autoReconnect=true" + - "&characterEncoding=utf8&useUnicode=true" + - "&sessionVariables=storage_engine%3DInnoDB" + - "&interactiveClient=true&dontTrackOpenResources=true", user, password); - } else if (databaseType == DatabaseType.POSTGRESQL) { - connection = DriverManager.getConnection("jdbc:postgresql://" + ip + ":" + port + "/" + database + - "?reWriteBatchedInserts=true" + - "&charSet=utf-8", user, password); - } - System.out.println("Connected to database!"); - } catch (SQLException e) { - e.printStackTrace(); + if (databaseType == DatabaseType.MYSQL) { + connection = DriverManager.getConnection("jdbc:mysql://" + + ip + ":" + port + "/" + database + "?autoReconnect=true" + + "&characterEncoding=utf8&useUnicode=true" + + "&sessionVariables=storage_engine%3DInnoDB" + + "&interactiveClient=true&dontTrackOpenResources=true", user, password); + } else if (databaseType == DatabaseType.POSTGRESQL) { + connection = DriverManager.getConnection("jdbc:postgresql://" + ip + ":" + port + "/" + database + + "?reWriteBatchedInserts=true" + + "&charSet=utf-8", user, password); } - System.out.println("Table: " + table); + System.out.println("Connected to database!"); } /* close connection */ - public void close() { + public void close() throws SQLException { if (isConnected()) { - try { - connection.close(); - System.out.println("Connection closed"); - } catch (SQLException e) { - e.printStackTrace(); - } + connection.close(); + System.out.println("Connection closed"); } } /* read value from database */ - public Object getDatabaseStatement(String command, String data) { - try { - PreparedStatement statement = connection.prepareStatement(command); - ResultSet result = statement.executeQuery(); - result.next(); - return result.getObject(data); - } catch (SQLException ex) { - ex.printStackTrace(); - return null; - } + public Object getDatabaseStatement(String command, String data) throws SQLException { + PreparedStatement statement = connection.prepareStatement(command); + ResultSet result = statement.executeQuery(); + result.next(); + return result.getObject(data); } /* execute action to database */ - public void executeDatabaseStatement(String command) { - try { - PreparedStatement statement = connection.prepareStatement(command); - statement.executeUpdate(); - } catch (SQLException ex) { - ex.printStackTrace(); - } + public void executeDatabaseStatement(String command) throws SQLException { + PreparedStatement statement = connection.prepareStatement(command); + statement.executeUpdate(); } /* check if connected */ diff --git a/src/main/java/pl/nightdev701/database/formular/DatabaseFormular.java b/src/main/java/pl/nightdev701/database/formular/DatabaseFormular.java index b3269f3..6f8e2a4 100644 --- a/src/main/java/pl/nightdev701/database/formular/DatabaseFormular.java +++ b/src/main/java/pl/nightdev701/database/formular/DatabaseFormular.java @@ -1,6 +1,6 @@ package pl.nightdev701.database.formular; /* record for easy variable handling */ -public record DatabaseFormular(String ip, String user, String password, String database, String table) { +public record DatabaseFormular(String ip, String user, String password, String database) { }