Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. public class SqlManager {
  2.  
  3. private HikariDataSource hikariDataSource;
  4.  
  5. public SqlManager() {
  6. try {
  7. HikariDataSource config = new HikariDataSource();
  8. config.setDataSourceClassName("org.mariadb.jdbc.MariaDbDataSource");
  9. config.addDataSourceProperty("serverName", Config.SQL$HOST.getString());
  10. config.addDataSourceProperty("port", Config.SQL$PORT.getInt());
  11. config.addDataSourceProperty("databaseName", Config.SQL$DATABSE_NAME.getString());
  12. config.setMaximumPoolSize(20);
  13. config.setIdleTimeout(0);
  14. config.setUsername(Config.SQL$USERNAME.getString());
  15. config.setPassword(Config.SQL$PASSWORD.getString());
  16.  
  17.  
  18. this.hikariDataSource = new HikariDataSource(config);
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22.  
  23. boolean success = true;
  24. long start = System.currentTimeMillis();
  25. try (Connection c = getConnection()) {
  26. try (Statement s = c.createStatement()) {
  27. s.execute("/* ping */ SELECT 1");
  28. }
  29. } catch (Exception e) {
  30. success = false;
  31. }
  32. long duration = System.currentTimeMillis() - start;
  33.  
  34. if (success) {
  35. String userTable = "CREATE TABLE IF NOT EXISTS users (" +
  36. "uuid VARCHAR(60) PRIMARY KEY," +
  37. "exp_points INT" +
  38. ");";
  39.  
  40. String dailyMissionTable = "CREATE TABLE IF NOT EXISTS daily_challenges (" +
  41. "id INTEGER AUTO_INCREMENT PRIMARY KEY," +
  42. "users_uuid VARCHAR(60) NOT NULL," +
  43. "challenge_id INTEGER NOT NULL," +
  44. "FOREIGN KEY(users_uuid) REFERENCES users(uuid));";
  45.  
  46. String weeklyMissionTable = "CREATE TABLE IF NOT EXISTS weekly_challenges (" +
  47. "id INTEGER AUTO_INCREMENT PRIMARY KEY," +
  48. "users_uuid VARCHAR(60) NOT NULL," +
  49. "challenge_id INTEGER NOT NULL," +
  50. "FOREIGN KEY(users_uuid) REFERENCES users(uuid));";
  51.  
  52. Console.log("Connected to sql, " + duration + "ms");
  53. try (Connection c = getConnection();
  54. Statement s = c.createStatement()) {
  55. s.execute(userTable);
  56. s.execute(dailyMissionTable);
  57. s.execute(weeklyMissionTable);
  58.  
  59. Console.log("Created 'users'");
  60. Console.log("Created 'daily_challenges'");
  61. Console.log("Created 'weekly_challenges'");
  62. } catch (Exception e) {
  63. e.printStackTrace();
  64. }
  65. } else {
  66. Console.log("Sql not connected");
  67. }
  68. }
  69.  
  70. public Connection getConnection() throws SQLException {
  71. return this.hikariDataSource.getConnection();
  72. }
  73.  
  74. public void shutdown() {
  75. this.hikariDataSource.close();
  76. }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement