Advertisement
Guest User

Untitled

a guest
May 6th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. public String host;
  2. public String port;
  3. public String database;
  4. public String username;
  5. public String password;
  6. private static Connection con;
  7.  
  8. public MySQL(String host, String port, String database, String username, String password) {
  9. this.host = host;
  10. this.port = port;
  11. this.database = database;
  12. this.username = username;
  13. this.password = password;
  14. }
  15.  
  16. public boolean isConnected() {
  17. return con != null;
  18. }
  19.  
  20.  
  21. public void connect() {
  22. if(!isConnected()) {
  23. try {
  24. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
  25. System.out.println("Die Connection wurde hergestellt!");
  26. } catch(SQLException e) {
  27. System.out.println("Die Connection konnte nicht hergestellt werden!");
  28. System.out.println("Der Fehler ist:");
  29. System.out.println("" + e.getMessage());
  30. }
  31. }
  32. }
  33.  
  34. public void disconnect() {
  35. if(isConnected()) {
  36. try {
  37. con.close();
  38. System.out.println("Die Connection wurde aufgehoben!");
  39. } catch(SQLException e) {
  40. System.out.println("Die Connection konnte nicht aufgehoben werden!");
  41. System.out.println("Der Fehler ist:");
  42. System.out.println("" + e.getMessage());
  43. }
  44. }
  45. }
  46.  
  47. public void update(String qry) {
  48. if(isConnected()) {
  49. try {
  50. Statement stm = con.createStatement();
  51. stm.executeUpdate(qry);
  52. } catch(SQLException e) {
  53. System.out.println("Das Update konnte nicht ausgeführt werden!");
  54. System.out.println("Der Fehler ist:");
  55. System.out.println("" + e.getMessage());
  56. }
  57. }
  58. }
  59.  
  60. public static Connection getConnection(){
  61. return con;
  62. }
  63.  
  64. public ResultSet result(String qry) {
  65. ResultSet rs = null;
  66.  
  67. update("create table if not exists BedWars(Name varchar(100), UUID varchar(100), Kills int, Deaths int, Wins int, brokenbeds int, Gespielt int)");
  68.  
  69. if(isConnected()) {
  70. try {
  71. Statement stm = con.createStatement();
  72. rs = stm.executeQuery(qry);
  73. } catch(SQLException e) {
  74. System.out.println("Der ResultSet konnte nicht ausgeführt werden!");
  75. System.out.println("Der Fehler ist:");
  76. System.out.println("" + e.getMessage());
  77. }
  78. }
  79. return rs;
  80. }
  81.  
  82. public void reconnect() {
  83. Timer timer = new Timer();
  84. timer.schedule(new TimerTask() {
  85.  
  86. @Override
  87. public void run() {
  88. try {
  89. con.close();
  90. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
  91. System.out.println("[Permissions] Reconnected!");
  92. } catch (SQLException e) {
  93. e.printStackTrace();
  94. }
  95. }
  96. }, 0, 10000*60*10);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement