Coder Social home page Coder Social logo

huskysql's Introduction

HuskySQL

This is a ConnectionPool for Java and MySQL, I created this because I wanted to see if I could make my own ConnectionPool project other than using others.

Features

  • Easy instantiation, There are many ways to instantiate the HuskyPool with the HuskyPoolFactory
public static final ConnectionPool CONNECTION_POOL = HuskyPoolFactory.newPool(10, new HuskyProperties("un", "pw", "db", "ip"), yourExecutorService);
  • Has DatabaseCalls's, abstract classes that are used to interact with the database. This class is very similar to the native Runnable and Callable class in Java. These classes are especially useful because they automatically close/return the resources used back to ConnectionPool even without being called, so it is impossible to forget to close a Connection:
DatabaseCall nameRetriver = new PreparedStatementRetriever<String>(pool.getConnection(), "SELECT vc_name FROM t_player WHERE vc_ip = ? LIMIT 1") {
    @Override
    public String retrieve() throws SQLException {
        String theName = "";
        this.ps.setString(1, "anIp");
        this.rs = ps.executeQuery();
        if(rs.first()) {
            theName = rs.getString(1);
        } else {
            theName = "Doesn't exist";
        }
        return theName;
    }
};
String name = pool.getDatabaseCallExecutor().submit(nameRetriver, true);
  • Using the DatabaseCallExecutor the user can choose whether to run their tasks asynchronously
  • Allows users to call DatabaseCalls within database calls, allowing users to get data, and update if need be, Expanding on the first example:
DatabaseCall nameRetriver = new PreparedStatementRetriever<String>(pool.getConnection(), "SELECT vc_name FROM t_player WHERE vc_ip = ? LIMIT 1") {
    @Override
    public String retrieve() throws SQLException {
        String theName = "";
        this.ps.setString(1, "anIp");
        this.rs = this.ps.executeQuery();
        if(this.rs.first()) {
            theName = this.rs.getString(1);
        } else {
            DatabaseCall update = new PreparedStatementUpdater(pool.getConnection(), "INSERT INTO t_player(`vc_name`, `vc_ip`, `vc_uuid`) VALUES (?,?,?);") {
                @Override
                public void update() throws SQLException {
                    this.ps.setString(1, "aName");
                    this.ps.setString(2, "anIp");
                    this.ps.setString(3, "aUuid");
                    this.ps.execute();
                }
            };
            //Keep synchronous with this thread
            pool.getDatabaseCallExecutor().submit(update, false);
            //Call this again, synchronous with this thread
            theName = pool.getDatabaseCallExecutor().submit(this, false);
        }
        return theName;
    }
};
//Calls the whole task asynchronously
String name = pool.getDatabaseCallExecutor().submit(nameRetriver, true);

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.