Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. public class Database {
  2.  
  3. private static HikariDataSource hikari;
  4.  
  5. public void connect() {
  6.  
  7. HikariConfig config = new HikariConfig();
  8.  
  9. config.setJdbcUrl("jdbc:mysql://" + Login.address + ":" + Login.port + "/" + Login.database + "?useSSL=false");
  10. config.setUsername(Login.username);
  11. config.setPassword(Login.password);
  12. config.setMaximumPoolSize(Login.poolSize);
  13.  
  14. config.addDataSourceProperty("characterEncoding", "utf8");
  15. config.addDataSourceProperty("useUnicode", "true");
  16.  
  17. hikari = new HikariDataSource(config);
  18.  
  19. setupTables();
  20. }
  21.  
  22. private void setupTables() {
  23. String query = "CREATE TABLE IF NOT EXISTS securitysuite (uuid char(36) NOT NULL, name TEXT NOT NULL, password TEXT NOT NULL, ip INT NOT NULL, chances INT NOT NULL, online BOOLEAN NOT NULL DEFAULT false, locked BOOLEAN NOT NULL DEFAULT false)";
  24. updateSQL(query);
  25. }
  26.  
  27. private boolean checkConnection() throws SQLException {
  28. return hikari != null && !hikari.isClosed();
  29. }
  30.  
  31. public void updateSQL(final String query) {
  32. ProxyServer.getInstance().getScheduler().runAsync(Login.getInstance(), new Runnable() {
  33. public void run() {
  34. try {
  35. if (!checkConnection())
  36. connect();
  37.  
  38. hikari.getConnection().createStatement().executeUpdate(query);
  39. } catch (SQLException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. });
  44. }
  45.  
  46. public static HikariDataSource getHikari(){
  47. return hikari;
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement