Skip to content

Commit

Permalink
code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
NightDev701 committed Sep 30, 2023
1 parent 3eaf76a commit 033e9a3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 44 deletions.
66 changes: 23 additions & 43 deletions src/main/java/pl/nightdev701/database/DatabaseConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -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 */
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {

}

0 comments on commit 033e9a3

Please sign in to comment.