Advertisement
Guest User

Untitled

a guest
May 22nd, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. public class SQLFramework {
  2.  
  3. private SQLFramework() {}
  4.  
  5. // Fields
  6. private static HikariDataSource source;
  7.  
  8.  
  9. @FunctionalInterface
  10. public interface Insert {
  11. public void insert(Connection connection, Formatter format);
  12. }
  13.  
  14.  
  15. public static Insert initialise = (connection, format) -> {
  16. try {
  17.  
  18. connection.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS custom_kits("
  19. + "uuid varchar(36), "
  20. + "guild varchar(16), "
  21. + "kills bigint, "
  22. + "deaths bigint, "
  23. + "PRIMARY KEY (uuid), "
  24. + "FOREIGN KEY (guild) REFERENCES guilds(name))");
  25.  
  26. connection.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS guilds("
  27. + "name varchar(16), "
  28. + "desc varchar(36), "
  29. + "wins bigint, "
  30. + "loses bigint, "
  31. + "PRIMARY KEY (name))");
  32.  
  33. } catch (SQLException e) {
  34. format.out("Failed to initialise main tables!", e);
  35. getServer().getPluginManager().disablePlugin(CustomKits.instance);
  36. }
  37. };
  38.  
  39.  
  40. public static void setup(Formatter format) {
  41.  
  42. FileConfiguration config = CustomKits.instance.getConfig();
  43.  
  44. source = new HikariDataSource();
  45.  
  46. source.setJdbcUrl("jdbc:mysql://localhost:3306/ck_test");
  47. source.setUsername("CustomKitsTest");
  48. source.setPassword("localtest");
  49. // source.setJdbcUrl(config.getString("database.jbdcurl", "default"));
  50. // source.setJdbcUrl(config.getString("database.user", "default"));
  51. // source.setPassword(config.getString("database.password", "default"));
  52.  
  53. }
  54.  
  55. public static void execute(Insert insert, Formatter format) {
  56. new BukkitRunnable() {
  57. @Override
  58. public void run() {
  59.  
  60. Connection connection = null;
  61.  
  62. try {
  63. connection = source.getConnection();
  64. insert.insert(connection, format);
  65. } catch (SQLException e) {
  66. format.out("Failed to establish SQL connection", e);
  67. } finally {
  68. if (connection != null) {
  69. try {
  70. connection.setAutoCommit(true);
  71. connection.close();
  72. } catch (SQLException e) {
  73. format.out("Failed to set connection autocommit to true", e);
  74. }
  75. }
  76. }
  77.  
  78. }
  79. }.runTaskAsynchronously(CustomKits.instance);
  80. }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement