DataBank.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package me.km.databank;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.HashMap;
  8. import me.km.module.Module;
  9. import me.km.scheduler.SnuviScheduler;
  10. import net.minecraft.util.text.TextFormatting;
  11. public class DataBank extends Module
  12. {
  13. private Connection c = null;
  14. private final String user;
  15. private final String password;
  16. private final HashMap<String, PreparedStatement> statements = new HashMap<>();
  17. public DataBank(String prefix, TextFormatting color, String user, String password)
  18. {
  19. super(prefix, color);
  20. this.user = user;
  21. this.password = password;
  22. }
  23. public boolean openDataBankConnection()
  24. {
  25. try
  26. {
  27. // The newInstance() call is a work around for some broken Java implementations
  28. Class.forName("com.mysql.jdbc.Driver").newInstance();
  29. c = DriverManager.getConnection("jdbc:mysql://localhost/minecraft?useSSL=false", user, password);
  30. this.sendToConsole("Connection to datebank etablished.");
  31. return true;
  32. }
  33. catch(ClassNotFoundException | IllegalAccessException | InstantiationException | SQLException ex)
  34. {
  35. this.sendWarningToConsole(ex.toString());
  36. return false;
  37. }
  38. }
  39. public void closeDataBankConnection()
  40. {
  41. try
  42. {
  43. if(c != null)
  44. {
  45. c.close();
  46. statements.values().forEach(stmt ->
  47. {
  48. try
  49. {
  50. stmt.close();
  51. }
  52. catch(SQLException ex)
  53. {
  54. }
  55. });
  56. }
  57. this.sendToConsole("The connection was closed.");
  58. }
  59. catch (SQLException ex)
  60. {
  61. this.sendWarningToConsole(ex.getMessage());
  62. }
  63. }
  64. public boolean isDummyDatabank()
  65. {
  66. return c == null;
  67. }
  68. public PreparedStatement prepareStatement(String query)
  69. {
  70. try
  71. {
  72. PreparedStatement ps = c.prepareStatement(query);
  73. PreparedStatement old = statements.put(query, ps);
  74. if(old != null)
  75. {
  76. old.close();
  77. }
  78. }
  79. catch(SQLException ex)
  80. {
  81. ex.printStackTrace();
  82. }
  83. return null;
  84. }
  85. public PreparedStatement prepareUnsafeStatement(String query) throws SQLException
  86. {
  87. return c.prepareStatement(query);
  88. }
  89. public boolean execute(String query)
  90. {
  91. if(c == null)
  92. {
  93. return false;
  94. }
  95. try(java.sql.Statement stmt = c.createStatement())
  96. {
  97. stmt.executeUpdate(query);
  98. return true;
  99. }
  100. catch(SQLException ex)
  101. {
  102. ex.printStackTrace();
  103. return false;
  104. }
  105. }
  106. public void startReconnecting(SnuviScheduler scheduler)
  107. {
  108. if(c == null)
  109. {
  110. return;
  111. }
  112. scheduler.scheduleRepeatingTask(() ->
  113. {
  114. try(java.sql.Statement stmt = c.createStatement())
  115. {
  116. // pointless query just for reconnecting
  117. stmt.executeUpdate("SHOW TABLES IN minecraft");
  118. }
  119. catch(SQLException ex)
  120. {
  121. sendToConsole("reconnect was done");
  122. }
  123. }, 100, 12000); // doing this every 10 minutes
  124. }
  125. }