Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SQLFramework {
- private SQLFramework() {}
- // Fields
- private static HikariDataSource source;
- @FunctionalInterface
- public interface Insert {
- public void insert(Connection connection, Formatter format);
- }
- public static Insert initialise = (connection, format) -> {
- try {
- connection.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS custom_kits("
- + "uuid varchar(36), "
- + "guild varchar(16), "
- + "kills bigint, "
- + "deaths bigint, "
- + "PRIMARY KEY (uuid), "
- + "FOREIGN KEY (guild) REFERENCES guilds(name))");
- connection.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS guilds("
- + "name varchar(16), "
- + "desc varchar(36), "
- + "wins bigint, "
- + "loses bigint, "
- + "PRIMARY KEY (name))");
- } catch (SQLException e) {
- format.out("Failed to initialise main tables!", e);
- getServer().getPluginManager().disablePlugin(CustomKits.instance);
- }
- };
- public static void setup(Formatter format) {
- FileConfiguration config = CustomKits.instance.getConfig();
- source = new HikariDataSource();
- source.setJdbcUrl("jdbc:mysql://localhost:3306/ck_test");
- source.setUsername("CustomKitsTest");
- source.setPassword("localtest");
- // source.setJdbcUrl(config.getString("database.jbdcurl", "default"));
- // source.setJdbcUrl(config.getString("database.user", "default"));
- // source.setPassword(config.getString("database.password", "default"));
- }
- public static void execute(Insert insert, Formatter format) {
- new BukkitRunnable() {
- @Override
- public void run() {
- Connection connection = null;
- try {
- connection = source.getConnection();
- insert.insert(connection, format);
- } catch (SQLException e) {
- format.out("Failed to establish SQL connection", e);
- } finally {
- if (connection != null) {
- try {
- connection.setAutoCommit(true);
- connection.close();
- } catch (SQLException e) {
- format.out("Failed to set connection autocommit to true", e);
- }
- }
- }
- }
- }.runTaskAsynchronously(CustomKits.instance);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement