This version brings significant enhancements to the database management system, including improved support for asynchronous SQL operations, better error handling, and advanced migration functionalities. The introduction of the SQL Factory class allows for better, easier management, execution of database connections and migration processes. Additionally, the deprecation of the SQL class indicates a shift towards more efficient and modern database management techniques and that is no longer supported.
Repository: https://github.com/TntTastisch/SQLAPI
Release & Changelog: https://github.com/TntTastisch/SQLAPI
You can additionally JdbcUrl arguments:
https://dev.mysql.com/doc/connector-j/8.1/en/connector-j-reference-configuration-properties.html
try {
SQLFactory factory = new SQLFactory(logger);
DatabaseType.setType(DatabaseType.MYSQL);
factory.createDatabaseConnection(8, 1, "host", "3306", "database", "user", "password", "additional arguments for mysql");
// or you can use this, when do you don't want any other arguments
factory.createDatabaseConnection("host", "3306", "database", "user", "password");
ConnectionAuthenticator connect = factory.getDatabaseConnection();
Connection connection = connect.getConnection();
if (connection == null) {
return;
}
} catch (SQLException | InterruptedException e) {
throw new RuntimeException(e);
}
Please make sure that this path and file exists
try {
SQLFactory factory = new SQLFactory(logger);
DatabaseType.setType(DatabaseType.SQLITE);
factory.createDatabaseConnection("./example.db");
ConnectionAuthenticator connect = factory.getDatabaseConnection();
Connection connection = connect.getConnection();
if (connection == null) {
return;
}
} catch (SQLException | InterruptedException e) {
throw new RuntimeException(e);
}
You have to add .join(); at the end, even not the code won't work.
/* The code before */
DatabaseCommand command = connect.getCommand();
command.update("CREATE TABLE IF NOT EXISTS users (id int(12) AUTO_INCREMENT PRIMARY KEY, email VARCHAR(64), username VARCHAR(16), password VARCHAR(255));").join();
/* The code afterwards */
/* The code before */
DatabaseCommand command = connect.getCommand();
command.update("INSERT INTO users (email, username, password) VALUES (?,?,?)", "user@example.org", "example", "tiny password").join();
/* The code afterwards */
/* The code before */
DatabaseCommand command = connect.getCommand();
command.largeUpdate("INSERT INTO users (email, username, password) VALUES (?,?,?)", "user@example.org", "example", "tiny password").join();
/* The code afterwards */
/* The code before */
DatabaseCommand command = connect.getCommand();
command.largeUpdate("TRUNCATE TABLE users").join();
/* The code afterwards */
/* The code before */
ResultSet result = command.query("SELECT * FROM users WHERE email = ?", "user@example.org").join();
int id = result.getInt("id");
String username = result.getString("username");
/* The code afterwards */
/* The code before */
ResultSet result = command.query("SELECT * FROM users").join();
int id = result.getInt("id");
String email = result.getString("email");
String username = result.getString("username");
/* The code afterwards */
/* The code before */
Migration migration = factory.createMigration("users", "id int(12) AUTO_INCREMENT PRIMARY KEY, email VARCHAR(64), username VARCHAR(16), password VARCHAR(255)", "id int(12) AUTO_INCREMENT PRIMARY KEY, email VARCHAR(64), username VARCHAR(16), password VARCHAR(255), firstname VARCHAR(255), lastname VARCHAR(255), description LONGTEXT");
migration.migrate(connection);
/* The code afterwards */
The command can't be executed if the connection is not exists. There will be no error message, just do nothing.
/* The code before */
connect.shutdown();