[UTIL] ⭐ASync Database Util!⭐ 2.0

Easily create your databases. (ASync supported)

  1. jarnoboy404
    Native Minecraft Version:
    1.16
    Tested Minecraft Versions:
    • 1.7
    • 1.8
    • 1.9
    • 1.10
    • 1.11
    • 1.12
    • 1.13
    • 1.14
    • 1.15
    • 1.16
    Source Code:
    https://github.com/jarnoboy404/Databases
    Installation
    Maven:

    Code (XML):
    <repositories>
      <repository>
          <id>jitpack.io</id>
          <url>https://jitpack.io</url>
      </repository>
    </repositories>

    <dependencies>
      <dependency>
          <groupId>com.github.jarnoboy404</groupId>
          <artifactId>Databases</artifactId>
          <version>2.0</version>
      </dependency>
    </dependencies>
    Gradle:

    Code (gradle (Unknown Language)):
    allprojects {
      repositories {
        ...
        maven { url 'https://jitpack.io' }
      }
    }

    dependencies {
            implementation 'com.github.jarnoboy404:Databases:2.0'
    }
    Usage preparation
    1: Create your databaseConnection

    Code (Java):
    private DatabaseConnection databaseConnection;
    @Override
    public void onEnable() {
        try {
            databaseConnection = new DatabaseConnection("localhost", 3306, "MyDatabase", "root", "password", 4);
        }catch (HikariPool.PoolInitializationException e) {
            Bukkit.getConsoleSender().sendMessage("Failed to connect to localhost database");
            Bukkit.getPluginManager().disablePlugin(this);
            return;
        }
    }
    2: Create your database class

    Code (Java):
    public class TestDatabase extends DatabaseConnection {
        public TestDatabase(DatabaseConnection databaseConnection) {
            super(databaseConnection);
        }
    }
    3: Lets make your database class accessible

    Code (Java):
    public static TestDatabase testDatabase;
    private DatabaseConnection databaseConnection;
    @Override
    public void onEnable() {
        try {
            databaseConnection = new DatabaseConnection("localhost", 3306, "MyDatabase", "root", "password", 4);
        }catch (HikariPool.PoolInitializationException e) {
            Bukkit.getConsoleSender().sendMessage("Failed to connect to localhost database");
            Bukkit.getPluginManager().disablePlugin(this);
            return;
        }
        testDatabase = new TestDatabase(databaseConnection);
    }
    Usage Examples
    NOTE: All your methods with database query's needs to be in your database class (in my case "TestDatabase")
    Select:

    Code (Java):
    public String getUser(UUID uuid) {
        QueryParam queryParam = new QueryParam();
        queryParam.addQueryParam("uuid", uuid);
        DatabaseResult result = executeQuery("SELECT * FROM `playerdata`" + queryParam.getWhereQuery());
        String user = null;
        if(result.getResult()) user = result.getString("user");
        result.endResult();
        return user;
    }
    Select async method 1:

    Code (Java):
    public CompletableFuture<String> getUser(UUID uuid) {
        return CompletableFuture.supplyAsync(() -> {
            QueryParam queryParam = new QueryParam();
            queryParam.addQueryParam("uuid", uuid);
       
            DatabaseResult result = executeQuery("SELECT * FROM `playerdata`" + queryParam.getWhereQuery());
            String user = null;
            if(result.getResult()) user = result.getString("user");
            result.endResult();
            return user;
        });
    }
    Select async method 2

    Code (Java):
    public CompletableFuture<DatabaseResult> getUser(UUID uuid) {
        QueryParam queryParam = new QueryParam();
        queryParam.addQueryParam("uuid", uuid);
        return executeQueryAsync("SELECT * FROM `playerdata`" + queryParam.getWhereQuery());
    }
    Update:

    Code (Java):
    public void updateUser(UUID uuid) {
        QueryParam updateQuery = new QueryParam();
        updateQuery.addQueryParam("uuid", uuid);
        QueryParam whereQuery = new QueryParam();
        whereQuery.addQueryParam("uuid", uuid);
        executeUpdate(updateQuery.getUpdateQuery("playerdata", whereQuery));
    }
    Update async:

    Code (Java):
    public CompletableFuture<Void> updateUser(UUID uuid) {
        QueryParam updateQuery = new QueryParam();
        updateQuery.addQueryParam("uuid", uuid);
        QueryParam whereQuery = new QueryParam();
        whereQuery.addQueryParam("uuid", uuid);
        return executeUpdateAsync(updateQuery.getUpdateQuery("playerdata", whereQuery));
    }
    Insert:

    Code (Java):
    public void createUser(UUID uuid, String user) {
        QueryParam queryParam = new QueryParam();
        queryParam.addQueryParam("uuid", uuid);
        queryParam.addQueryParam("user", user);
        executeUpdate(queryParam.getInsertQuery("playerdata"));
    }
    Insert async:

    Code (Java):
    public CompletableFuture<Void> createUser(UUID uuid, String user) {
        QueryParam queryParam = new QueryParam();
        queryParam.addQueryParam("uuid", uuid);
        queryParam.addQueryParam("user", user);
        return executeUpdateAsync(queryParam.getInsertQuery("playerdata"));
    }
    Good luck!