Untitled

mail@pastecode.io avatar
unknown
java
3 years ago
2.2 kB
3
Indexable
Never
package me.caiorocha.udemy.adapter;

import com.dieselpoint.norm.Database;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.ConsoleCommandSender;

import java.io.FileNotFoundException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;
import java.util.Properties;

public final class Connection {
    private static Database database;

    private Connection() {}

    public static Database getDatabase() {
        if (Objects.isNull(database)) setup();
        return database;
    }

    private static void setup() {
        ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();

        try {
            final Properties properties = Configuration.getInstance().getProperties("database");
            final String HOSTNAME = properties.getProperty("database.hostname");
            final String PORT = properties.getProperty("database.port");
            final String DATABASE = properties.getProperty("database.name");
            final String USERNAME = properties.getProperty("database.username");
            final String PASSWORD = properties.getProperty("database.password");

            database = new Database();
            database.setMaxPoolSize(10);
            database.setJdbcUrl("jdbc:mysql://localhost:3306/udemy?useSSL=false");
            database.setUser("root");
            database.setPassword("");

            console.sendMessage(ChatColor.GREEN + "[DATABASE] Banco de dados configurado.");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static boolean containsTable(String tableName){
        try {
            ResultSet set = database.getConnection().getMetaData().getTables(null, null, tableName, null);
            while(set.next()){
                String table = set.getString("TABLE_NAME");
                if(table != null && table.equals(tableName)){
                    return true;
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }
}