DataBank.java 1.8 KB

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