privatefinalConfigPoolFrameworkconfigPoolFramework=ConfigBuilder.getConfigBuilder().build(); // returns a default configuration// You do not have to change all values, the remaining values are filled with default values.
privatevoidtestQuery(){
this.asyncMySQLPoolHandler.executeQueryAsync("SELECT * FROM `"+"yourTable"+"`;").whenComplete((cachedRowSet,throwable)->{
try{
finalCollection<String> collection=newArrayList<>();
while(cachedRowSet.next()){
collection.add(cachedRowSet.getString(1));
}
// now you can work with the cachedRowSetcachedRowSet.close();
}catch(finalSQLExceptionexception){
exception.printStackTrace();
}
});
}
ReturnExecuteQuery:
ExecuteQueryAsync:
publicinttestQueryResult(finalStringvalue){
try{
finalCachedRowSetresultSet=this.asyncMySQLPoolHandler.executeQueryAsync("SELECT `yourColumn` FROM `"+"yourTable"+"` WHERE `yourValue`= '"+this.asyncMySQLPoolHandler.removeSQLInjectionPossibility(value)+"';").join();
if(resultSet.last()){
finalinttest=resultSet.getInt("yourColumn");
resultSet.close();
// return your async request resultreturntest;
}else{
resultSet.close();
// return a custom result if your request has failedreturn-1;
}
}catch(finalSQLExceptionexception){
exception.printStackTrace();
}
return-1;
}
ExecuteQueryInstantLastResultAsync:
publicinttest(finalStringvalue){
finalIntegerresult=(Integer)this.asyncMySQLPoolHandler.executeQueryInstantLastResultAsync("SELECT `yourColumn` FROM `"+"yourTable"+"` WHERE `yourValue`= '"+this.asyncMySQLPoolHandler.removeSQLInjectionPossibility(value)+"';","yourColumn").join();
returnresult!=null?result:-1;
}
ExecuteQueryInstantFirstResultAsync:
publicinttest(finalStringvalue){
finalIntegerresult=(Integer)this.asyncMySQLPoolHandler.executeQueryInstantFirstResultAsync("SELECT `yourColumn` FROM `"+"yourTable"+"` WHERE `yourValue`= '"+this.asyncMySQLPoolHandler.removeSQLInjectionPossibility(value)+"';","yourColumn").join();
returnresult!=null?result:-1;
}
ExecuteQueryInstantLastResultAsBooleanAsync:
publicbooleantest(finalStringvalue){
returnthis.asyncMySQLPoolHandler.executeQueryInstantLastResultAsBooleanAsync("SELECT `yourColumn` FROM `"+"yourTable"+"` WHERE `yourValue`= '"+this.asyncMySQLPoolHandler.removeSQLInjectionPossibility(value)+"';","yourColumn").join();
}
ExecuteQueryInstantFirstResultAsBooleanAsync:
publicbooleantest(finalStringvalue){
returnthis.asyncMySQLPoolHandler.executeQueryInstantFirstResultAsBooleanAsync("SELECT `yourColumn` FROM `"+"yourTable"+"` WHERE `yourValue`= '"+this.asyncMySQLPoolHandler.removeSQLInjectionPossibility(value)+"';","yourColumn").join();
}
ExecuteQueryInstantNextResultAsync:
publicbooleantest(finalStringvalue){
returnthis.asyncMySQLPoolHandler.executeQueryInstantNextResultAsync("SELECT * FROM `"+"yourTable"+"` WHERE `yourValue`= '"+this.asyncMySQLPoolHandler.removeSQLInjectionPossibility(value)+"';").join();
}
ExecuteUpdate with Statement:
privatevoidtestUpdate(finalStringyourValue){
this.asyncMySQLPoolHandler.executeUpdateAsync("INSERT INTO `"+"yourTable"+"` SET `yourColumn` = '"+this.asyncMySQLPoolHandler.removeSQLInjectionPossibility(yourValue)+"';").whenComplete((aVoid,throwable)->{
//now you can work with the result
});
}
ExecuteUpdate with PreparedStatement:
privatevoidtestUpdate(finalString...value){
this.asyncMySQLPoolHandler.executeUpdatePreparedStatementAsync("INSERT INTO `"+"yourTable"+"` (value1, value2, value3, value4) VALUES (?, ?, ?, ?)",value1,value2,value3,value4).whenComplete((aVoid,throwable)->{
//now you can work with the result
});
}
When using the api, SLF4J seems to through an exception every time at the beginning. After that every log is printed with red color in the console. All queries are executed though.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".SLF4J: Defaulting to no-operation (NOP) logger implementationSLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.Mai 10, 2021 11:46:03 VORM. de.mint.asyncmysqlpoolhandler.mainservice.AsyncMySQLPoolHandler createMessageINFORMATION: The pool was successfully created! [HIKARICP]Mai 10, 2021 11:46:03 VORM. de.mint.asyncmysqlpoolhandler.mainservice.AsyncMySQLPoolHandler createMessageINFORMATION: The pool was successfully closed! [HIKARICP]
The code producing the error above:
packageutil;
importde.mint.asyncmysqlpoolhandler.configservice.ConfigBuilder;
importde.mint.asyncmysqlpoolhandler.enumservice.EnumPoolFramework;
importde.mint.asyncmysqlpoolhandler.mainservice.AsyncMySQLPoolHandler;
importjava.util.concurrent.CompletableFuture;
importorg.intellij.lang.annotations.Language;
publicclassDatabaseUtil {
privatestaticfinalAsyncMySQLPoolHandlerasyncMySQLPoolHandler =
newAsyncMySQLPoolHandler(
"localhost",
3306,
"root",
"",
"test",
EnumPoolFramework.HIKARICP,
ConfigBuilder.getConfigBuilder().build());
publicstaticvoidconnect() {
DatabaseUtil.asyncMySQLPoolHandler.openPool();
}
publicstaticCompletableFuture<Void> update(@Language("MySQL") finalStringsql) {
returnDatabaseUtil.asyncMySQLPoolHandler.executeUpdateAsync(sql);
}
publicstaticvoiddisconnect() {
DatabaseUtil.asyncMySQLPoolHandler.closePool();
}
publicstaticvoidsetup() {
DatabaseUtil.update("CREATE TABLE IF NOT EXISTS `temp` (`first` INT, `second` VARCHAR(16));")
.join(); // so the connection only closes if the query got executed
}
publicstaticvoidmain(finalString[] args) {
DatabaseUtil.connect();
DatabaseUtil.setup();
DatabaseUtil.disconnect();
}
}
The code examples shown in the README are vulnerable to SQL Injections.
Please add save examples, so beginners are not just copying and pasting the examples and basically ripping their software.