DataBank.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.util.HashMap;
  7. import me.hammerle.snuviscript.code.ISnuviLogger;
  8. import me.km.scheduler.SnuviScheduler;
  9. public class DataBank {
  10. private final ISnuviLogger logger;
  11. private Connection c = null;
  12. private final String user;
  13. private final String password;
  14. private final HashMap<String, PreparedStatement> statements = new HashMap<>();
  15. public DataBank(ISnuviLogger logger, String user, String password) {
  16. this.logger = logger;
  17. this.user = user;
  18. this.password = password;
  19. }
  20. public boolean openDataBankConnection() {
  21. try {
  22. // The newInstance() call is a work around for some broken Java implementations
  23. Class.forName("com.mysql.jdbc.Driver").getDeclaredConstructor().newInstance();
  24. c = DriverManager.getConnection("jdbc:mysql://localhost/minecraft?useSSL=false", user,
  25. password);
  26. logger.print("connection to databank etablished");
  27. return true;
  28. } catch(Exception ex) {
  29. logger.print(ex);
  30. return false;
  31. }
  32. }
  33. public void closeDataBankConnection() {
  34. try {
  35. if(c != null) {
  36. c.close();
  37. statements.values().forEach(stmt -> {
  38. try {
  39. stmt.close();
  40. } catch(SQLException ex) {
  41. }
  42. });
  43. }
  44. logger.print("connection to databank was closed");
  45. } catch(SQLException ex) {
  46. logger.print(ex);
  47. }
  48. }
  49. public boolean isDummyDatabank() {
  50. return c == null;
  51. }
  52. public PreparedStatement prepareStatement(String query) {
  53. try {
  54. PreparedStatement ps = c.prepareStatement(query);
  55. PreparedStatement old = statements.put(query, ps);
  56. if(old != null) {
  57. old.close();
  58. }
  59. return ps;
  60. } catch(SQLException ex) {
  61. ex.printStackTrace();
  62. }
  63. return null;
  64. }
  65. public PreparedStatement prepareUnsafeStatement(String query) throws SQLException {
  66. return c.prepareStatement(query);
  67. }
  68. public boolean execute(String query) {
  69. if(c == null) {
  70. return false;
  71. }
  72. try(java.sql.Statement stmt = c.createStatement()) {
  73. stmt.executeUpdate(query);
  74. return true;
  75. } catch(SQLException ex) {
  76. ex.printStackTrace();
  77. return false;
  78. }
  79. }
  80. public void startReconnecting(SnuviScheduler scheduler) {
  81. if(c == null) {
  82. return;
  83. }
  84. scheduler.scheduleRepeatingTask("startReconnecting", () -> {
  85. try(java.sql.Statement stmt = c.createStatement()) {
  86. // pointless query just for reconnecting
  87. stmt.executeUpdate("SHOW TABLES IN minecraft");
  88. } catch(SQLException ex) {
  89. logger.print("reconnect was done");
  90. }
  91. }, 100, 12000); // doing this every 10 minutes
  92. }
  93. }