From 1b450b17dcb6bd0843cde6dfaeef7fb280e97696 Mon Sep 17 00:00:00 2001 From: XNonymous <56768074+XNonymous-N2@users.noreply.github.com> Date: Wed, 11 Oct 2023 17:55:57 +0200 Subject: [PATCH 1/2] Implemented ORMLite --- build.gradle | 13 +- .../database/DatabaseConnector.java | 139 +++++++++--------- .../database/type/DatabaseType.java | 17 +++ 3 files changed, 101 insertions(+), 68 deletions(-) diff --git a/build.gradle b/build.gradle index 99e2591..c86aaff 100644 --- a/build.gradle +++ b/build.gradle @@ -9,11 +9,22 @@ compileJava { group 'pl.nightdev701' repositories { + maven { + url 'https://jitpack.io' + } + mavenCentral() } dependencies { - implementation files("API/mysql-connector-j-8.1.0.jar") + // https://mvnrepository.com/artifact/com.mysql/mysql-connector-j + implementation 'com.mysql:mysql-connector-j:8.1.0' + + // https://mvnrepository.com/artifact/org.postgresql/postgresql + implementation 'org.postgresql:postgresql:42.6.0' + + // https://mvnrepository.com/artifact/com.j256.ormlite/ormlite-jdbc + implementation 'com.j256.ormlite:ormlite-jdbc:6.1' } java { diff --git a/src/main/java/pl/nightdev701/database/DatabaseConnector.java b/src/main/java/pl/nightdev701/database/DatabaseConnector.java index e00f505..7653c86 100644 --- a/src/main/java/pl/nightdev701/database/DatabaseConnector.java +++ b/src/main/java/pl/nightdev701/database/DatabaseConnector.java @@ -10,42 +10,71 @@ */ +import com.j256.ormlite.dao.Dao; +import com.j256.ormlite.dao.DaoManager; +import com.j256.ormlite.jdbc.JdbcPooledConnectionSource; +import com.j256.ormlite.table.TableUtils; import pl.nightdev701.database.formular.DatabaseFormular; import pl.nightdev701.database.type.DatabaseType; import pl.nightdev701.logger.AbstractLogger; import pl.nightdev701.logger.standard.DefaultLogger; -import java.sql.*; +import java.sql.SQLException; import java.util.logging.Level; public class DatabaseConnector { - private static int port; private final String ip; private final String user; private final String password; private final String database; private final DatabaseType databaseType; private final AbstractLogger logger; - private Connection connection; + private final String connectionURL; + private JdbcPooledConnectionSource connectionSource; + /** + * Creates a new instance of the DatabaseConnector + * + * @param formular + * @param databaseType + */ public DatabaseConnector(DatabaseFormular formular, DatabaseType databaseType) { - this.ip = formular.ip(); - this.user = formular.user(); - this.password = formular.password(); - this.database = formular.database(); - this.databaseType = databaseType; - - this.logger = new DefaultLogger(); + this(formular, databaseType, new DefaultLogger()); + } - if (databaseType == DatabaseType.MYSQL) { - setPort(3306); - } else if (databaseType == DatabaseType.POSTGRESQL) { - setPort(5432); - } + /** + * Creates a new instance of the DatabaseConnector with a custom port + * + * @param formular + * @param databaseType + * @param port + */ + public DatabaseConnector(DatabaseFormular formular, DatabaseType databaseType, int port) { + this(formular, databaseType, port, new DefaultLogger()); } + + /** + * Creates a new instance of the DatabaseConnector with a custom logger + * + * @param formular + * @param databaseType + * @param logger + */ public DatabaseConnector(DatabaseFormular formular, DatabaseType databaseType, AbstractLogger logger) { + this(formular, databaseType, databaseType.getDefaultPort(), logger); + } + + /** + * Creates a new instance of the DatabaseConnector with a custom port and logger + * + * @param formular + * @param databaseType + * @param port + * @param logger + */ + public DatabaseConnector(DatabaseFormular formular, DatabaseType databaseType, int port, AbstractLogger logger) { this.ip = formular.ip(); this.user = formular.user(); this.password = formular.password(); @@ -55,81 +84,57 @@ public DatabaseConnector(DatabaseFormular formular, DatabaseType databaseType, A this.logger = logger; if (databaseType == DatabaseType.MYSQL) { - setPort(3306); - } else if (databaseType == DatabaseType.POSTGRESQL) { - setPort(5432); - } - } - - /** - * connect to database - */ - public void connect() throws SQLException { - logger.log(Level.INFO, "Connect to database, with the type \"" + databaseType.name() + "\"!"); - if (databaseType == DatabaseType.MYSQL) { - connection = DriverManager.getConnection("jdbc:mysql://" + + connectionURL = "jdbc:mysql://" + ip + ":" + port + "/" + database + "?autoReconnect=true" + "&characterEncoding=utf8&useUnicode=true" + "&sessionVariables=storage_engine%3DInnoDB" + - "&interactiveClient=true&dontTrackOpenResources=true", user, password); + "&interactiveClient=true&dontTrackOpenResources=true"; } else if (databaseType == DatabaseType.POSTGRESQL) { - connection = DriverManager.getConnection("jdbc:postgresql://" + ip + ":" + port + "/" + database + + connectionURL = "jdbc:postgresql://" + ip + ":" + port + "/" + database + "?reWriteBatchedInserts=true" + - "&charSet=utf-8", user, password); + "&charSet=utf-8"; + } else { + connectionURL = null; } - logger.log(Level.INFO, "Connected to database!"); } /** - * close connection + * initialize connection */ - public void close() throws SQLException { - if (isConnected()) { - connection.close(); - logger.log(Level.INFO, "Connection closed"); - } - } + public void initialize() throws SQLException { + logger.log(Level.INFO, "Initializing database connection, with the type \"" + databaseType.name() + "\"!"); - /** - * read value from database - */ - public Object getDatabaseStatement(String command, String data) throws SQLException { - PreparedStatement statement = connection.prepareStatement(command); - ResultSet result = statement.executeQuery(); - result.next(); - return result.getObject(data); + connectionSource = new JdbcPooledConnectionSource(connectionURL); + connectionSource.setUsername(user); + connectionSource.setPassword(password); } /** - * execute action to database - */ - public void executeDatabaseStatement(String command) throws SQLException { - PreparedStatement statement = connection.prepareStatement(command); - statement.executeUpdate(); - } - - /** - * check if connected + * close connection */ - public boolean isConnected() throws SQLException { - if (connection == null) { - return false; - } - return !connection.isClosed(); + public void close() throws Exception { + connectionSource.close(); + logger.log(Level.INFO, "Connection closed"); } /** - * get port + * Creates a table for a DatabaseTable class if no table already exists with the same name + * + * @param dataClass your DatabaseTable class */ - public int getPort() { - return port; + public int createTableIfNotExists(Class dataClass) throws SQLException { + return TableUtils.createTableIfNotExists(connectionSource, dataClass); } /** - * set port + * Creates a new Dao with which you can work + * + * @param clazz your DatabaseTable class + * @return new Dao + * @throws SQLException */ - public void setPort(int port) { - this.port = port; + public , T> D createDao(Class clazz) throws SQLException { + return DaoManager.createDao(connectionSource, clazz); } } diff --git a/src/main/java/pl/nightdev701/database/type/DatabaseType.java b/src/main/java/pl/nightdev701/database/type/DatabaseType.java index f14d4c0..1832b66 100644 --- a/src/main/java/pl/nightdev701/database/type/DatabaseType.java +++ b/src/main/java/pl/nightdev701/database/type/DatabaseType.java @@ -5,5 +5,22 @@ public enum DatabaseType { MYSQL, POSTGRESQL, + ; + /** + * Gets the default port for the database type. + * + * @return the default port for the database type (3306 for MySQL, 5432 for PostgresSQL). + */ + public int getDefaultPort() { + int port = -1; + + if (this == MYSQL) { + port = 3306; + } else if (this == POSTGRESQL) { + port = 5432; + } + + return port; + } } From 095a4cde0a1d2fcb257e9abc476407111867ca3c Mon Sep 17 00:00:00 2001 From: XNonymous <56768074+XNonymous-N2@users.noreply.github.com> Date: Wed, 11 Oct 2023 18:02:28 +0200 Subject: [PATCH 2/2] Updated DatabaseType, better implementation for default port --- .../database/type/DatabaseType.java | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/main/java/pl/nightdev701/database/type/DatabaseType.java b/src/main/java/pl/nightdev701/database/type/DatabaseType.java index 1832b66..6cca411 100644 --- a/src/main/java/pl/nightdev701/database/type/DatabaseType.java +++ b/src/main/java/pl/nightdev701/database/type/DatabaseType.java @@ -3,24 +3,21 @@ /* database types */ public enum DatabaseType { - MYSQL, - POSTGRESQL, + MYSQL(3306), + POSTGRESQL(5432), ; + private final int defaultPort; + + DatabaseType(int defaultPort) { + this.defaultPort = defaultPort; + } + /** - * Gets the default port for the database type. - * - * @return the default port for the database type (3306 for MySQL, 5432 for PostgresSQL). + * Gets the default port for a database type + * @return defaultPort */ public int getDefaultPort() { - int port = -1; - - if (this == MYSQL) { - port = 3306; - } else if (this == POSTGRESQL) { - port = 5432; - } - - return port; + return defaultPort; } }