DataBank.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package me.km.databank;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import me.km.api.Module;
  7. import net.minecraft.util.text.TextFormatting;
  8. public class DataBank extends Module
  9. {
  10. private final static DummyStatement DUMMY_STATEMENT = new DummyStatement();
  11. private Connection c;
  12. private final String user;
  13. private final String password;
  14. public DataBank(String mname, String prefix, TextFormatting color, String user, String password)
  15. {
  16. super(mname, prefix, color);
  17. this.user = user;
  18. this.password = password;
  19. }
  20. public boolean openDataBankConnection()
  21. {
  22. c = null;
  23. try
  24. {
  25. // The newInstance() call is a work around for some broken Java implementations
  26. Class.forName("com.mysql.jdbc.Driver").newInstance();
  27. c = DriverManager.getConnection("jdbc:mysql://localhost/minecraft?autoReconnect=true", user, password);
  28. this.sendToConsole("Die Verbindung wurde erfolgreich aufgebaut.");
  29. return true;
  30. }
  31. catch(ClassNotFoundException | IllegalAccessException | InstantiationException | SQLException ex)
  32. {
  33. this.sendWarningToConsole(ex.toString());
  34. return false;
  35. }
  36. }
  37. public void closeDataBankConnection()
  38. {
  39. try
  40. {
  41. c.close();
  42. this.sendToConsole("Die Verbindung wurde erfolgreich unterbrochen.");
  43. }
  44. catch (SQLException ex)
  45. {
  46. this.sendWarningToConsole(ex.getMessage());
  47. }
  48. catch (NullPointerException ex)
  49. {
  50. }
  51. }
  52. public Statement createStatement() throws SQLException
  53. {
  54. if(c == null)
  55. {
  56. return DUMMY_STATEMENT;
  57. }
  58. return c.createStatement();
  59. }
  60. }