Coder Social home page Coder Social logo

mintui9976 / asyncmysqlpoolhandler Goto Github PK

View Code? Open in Web Editor NEW
3.0 0.0 2.0 139 KB

An simplify library to create jdbc pools via HikariCP. The Statement/PreparedStatement methods are performed asynchronously

License: MIT License

pool pooling pool-allocator pool-manager hikaricp hikari-connection-pool hikari-datasource jdbc jdbc-database jdbc-connector

asyncmysqlpoolhandler's Introduction

AsyncMySQLPoolHandler

forthebadge forthebadge forthebadge

GitHub release GitHub stars

An simplify library to create jdbc pools via HikariCP.

Statement/PreparedStatement methods are performed asynchronously.

Features

  • full asynchronously statement/preparedStatement methods
  • simple jdbc connector
  • simple configurator
  • default config available
  • instant query result via preparedStatement
  • multiple object update via preparedStatement
  • single update via statement
  • add selection PublicKeyRetrival

Implemented JDBC Frameworks

Repository

<repositories>
    <repository>
        <id>gitlab-maven</id>
        <url>https://gitlab.zyonicsoftware.com/api/v4/projects/211/packages/maven</url>
    </repository>
</repositories>

<distributionManagement>
<repository>
    <id>gitlab-maven</id>
    <url>https://gitlab.zyonicsoftware.com/api/v4/projects/211/packages/maven</url>
</repository>

<snapshotRepository>
    <id>gitlab-maven</id>
    <url>https://gitlab.zyonicsoftware.com/api/v4/projects/211/packages/maven</url>
</snapshotRepository>
</distributionManagement>

Dependency

<dependency>
    <groupId>de.mint.asyncmysqlpoolhandler</groupId>
    <artifactId>AsyncMySQLPoolHandler</artifactId>
    <version>Tag@Gitlab</version>
</dependency>

Utilization

Initialized AsyncMySQLPoolHandler Object

private final AsyncMySQLPoolHandler asyncMySQLPoolHandler=new AsyncMySQLPoolHandler(hostname,username,password,enumPoolFramework,configPoolFramework);
private final AsyncMySQLPoolHandler asyncMySQLPoolHandler=new AsyncMySQLPoolHandler(hostname,port,username,password,enumPoolFramework,configPoolFramework);
private final AsyncMySQLPoolHandler asyncMySQLPoolHandler=new AsyncMySQLPoolHandler(hostname,username,password,database,enumPoolFramework,configPoolFramework);
private final AsyncMySQLPoolHandler asyncMySQLPoolHandler=new AsyncMySQLPoolHandler(hostname,port,username,password,database,enumPoolFramework,configPoolFramework);

ConfigPoolFramework

private final ConfigPoolFramework configPoolFramework=ConfigBuilder.getConfigBuilder().build(); // returns a default configuration
// You do not have to change all values, the remaining values are filled with default values.

OpenPool:

private void openPool(){
        if(this.asyncMySQLPoolHandler.openPool()){
        //successful
        }else{
        //failed
        }
        }

ClosePool:

private void closePool(){
        if(this.asyncMySQLPoolHandler.closePool()){
        //successful
        }else{
        //failed
        }
        }

ExecuteQuery:

private void testQuery(){
        this.asyncMySQLPoolHandler.executeQueryAsync("SELECT * FROM `"+"yourTable"+"`;").whenComplete((cachedRowSet,throwable)->{
        try{
final Collection<String> collection=new ArrayList<>();
        while(cachedRowSet.next()){
        collection.add(cachedRowSet.getString(1));
        }
        // now you can work with the cachedRowSet
        cachedRowSet.close();
        }catch(final SQLException exception){
        exception.printStackTrace();
        }
        });
        }

ReturnExecuteQuery:

ExecuteQueryAsync:
public int testQueryResult(final String value){
        try{
final CachedRowSet resultSet=this.asyncMySQLPoolHandler.executeQueryAsync("SELECT `yourColumn` FROM `"+"yourTable"+"` WHERE `yourValue`= '"+this.asyncMySQLPoolHandler.removeSQLInjectionPossibility(value)+"';").join();
        if(resultSet.last()){
final int test=resultSet.getInt("yourColumn");
        resultSet.close();
        // return your async request result
        return test;
        }else{
        resultSet.close();
        // return a custom result if your request has failed
        return-1;
        }
        }catch(final SQLException exception){
        exception.printStackTrace();
        }
        return-1;
        }
ExecuteQueryInstantLastResultAsync:
public int test(final String value){
final Integer result=(Integer)this.asyncMySQLPoolHandler.executeQueryInstantLastResultAsync("SELECT `yourColumn` FROM `"+"yourTable"+"` WHERE `yourValue`= '"+this.asyncMySQLPoolHandler.removeSQLInjectionPossibility(value)+"';","yourColumn").join();
        return result!=null?result:-1;
        }
ExecuteQueryInstantFirstResultAsync:
public int test(final String value){
final Integer result=(Integer)this.asyncMySQLPoolHandler.executeQueryInstantFirstResultAsync("SELECT `yourColumn` FROM `"+"yourTable"+"` WHERE `yourValue`= '"+this.asyncMySQLPoolHandler.removeSQLInjectionPossibility(value)+"';","yourColumn").join();
        return result!=null?result:-1;
        }
ExecuteQueryInstantLastResultAsBooleanAsync:
public boolean test(final String value){
        return this.asyncMySQLPoolHandler.executeQueryInstantLastResultAsBooleanAsync("SELECT `yourColumn` FROM `"+"yourTable"+"` WHERE `yourValue`= '"+this.asyncMySQLPoolHandler.removeSQLInjectionPossibility(value)+"';","yourColumn").join();
        }
ExecuteQueryInstantFirstResultAsBooleanAsync:
public boolean test(final String value){
        return this.asyncMySQLPoolHandler.executeQueryInstantFirstResultAsBooleanAsync("SELECT `yourColumn` FROM `"+"yourTable"+"` WHERE `yourValue`= '"+this.asyncMySQLPoolHandler.removeSQLInjectionPossibility(value)+"';","yourColumn").join();
        }
ExecuteQueryInstantNextResultAsync:
public boolean test(final String value){
        return this.asyncMySQLPoolHandler.executeQueryInstantNextResultAsync("SELECT * FROM `"+"yourTable"+"` WHERE `yourValue`= '"+this.asyncMySQLPoolHandler.removeSQLInjectionPossibility(value)+"';").join();
        }

ExecuteUpdate with Statement:

  private void testUpdate(final String yourValue){
        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:

  private void testUpdate(final String...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
        });
        }

asyncmysqlpoolhandler's People

Contributors

azraanimating avatar hallo1142 avatar mintui9976 avatar spark61 avatar

Stargazers

 avatar  avatar  avatar

asyncmysqlpoolhandler's Issues

SLF4J error message

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 implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Mai 10, 2021 11:46:03 VORM. de.mint.asyncmysqlpoolhandler.mainservice.AsyncMySQLPoolHandler createMessage
INFORMATION: The pool was successfully created! [HIKARICP]
Mai 10, 2021 11:46:03 VORM. de.mint.asyncmysqlpoolhandler.mainservice.AsyncMySQLPoolHandler createMessage
INFORMATION: The pool was successfully closed! [HIKARICP]

The code producing the error above:

package util;

import de.mint.asyncmysqlpoolhandler.configservice.ConfigBuilder;
import de.mint.asyncmysqlpoolhandler.enumservice.EnumPoolFramework;
import de.mint.asyncmysqlpoolhandler.mainservice.AsyncMySQLPoolHandler;
import java.util.concurrent.CompletableFuture;
import org.intellij.lang.annotations.Language;

public class DatabaseUtil {

  private static final AsyncMySQLPoolHandler asyncMySQLPoolHandler =
      new AsyncMySQLPoolHandler(
          "localhost",
          3306,
          "root",
          "",
          "test",
          EnumPoolFramework.HIKARICP,
          ConfigBuilder.getConfigBuilder().build());

  public static void connect() {
    DatabaseUtil.asyncMySQLPoolHandler.openPool();
  }

  public static CompletableFuture<Void> update(@Language("MySQL") final String sql) {
    return DatabaseUtil.asyncMySQLPoolHandler.executeUpdateAsync(sql);
  }

  public static void disconnect() {
    DatabaseUtil.asyncMySQLPoolHandler.closePool();
  }

  public static void setup() {
    DatabaseUtil.update("CREATE TABLE IF NOT EXISTS `temp` (`first` INT, `second` VARCHAR(16));")
        .join(); // so the connection only closes if the query got executed
  }

  public static void main(final String[] args) {
    DatabaseUtil.connect();
    DatabaseUtil.setup();
    DatabaseUtil.disconnect();
  }
}

SQL Injections

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.

:)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.