Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. package de.rizegames.bungeesystem.bungeeDB;
  2.  
  3. import de.rizegames.bungeesystem.Core;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9.  
  10. public class MySQL
  11. {
  12. private final Core main;
  13. private final String host;
  14. private final String database;
  15. private final String user;
  16. private final String password;
  17. private int port;
  18. private Connection conn;
  19.  
  20. public MySQL(Core main, String host, int port, String user, String password, String database)
  21. {
  22. this.port = 3306;
  23. this.main = main;
  24. this.host = host;
  25. this.port = port;
  26. this.user = user;
  27. this.password = password;
  28. this.database = database;
  29. }
  30.  
  31. public void openConnection()
  32. {
  33. try
  34. {
  35. if (!isConnectionOpen()) {
  36. this.conn = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?autoReconnect=true", this.user, this.password);
  37. }
  38. }
  39. catch (SQLException e)
  40. {
  41. sendException(e);
  42. }
  43. }
  44.  
  45. public void closeConnection()
  46. {
  47. try
  48. {
  49. if (isConnectionOpen())
  50. {
  51. this.conn.close();
  52. this.conn = null;
  53. }
  54. }
  55. catch (SQLException e)
  56. {
  57. sendException(e);
  58. }
  59. }
  60.  
  61. private boolean isConnectionOpen()
  62. {
  63. try
  64. {
  65. return (this.conn != null) && (this.conn.isValid(3));
  66. }
  67. catch (SQLException e)
  68. {
  69. sendException(e);
  70. }
  71. return false;
  72. }
  73.  
  74. public Connection getConnection()
  75. {
  76. return this.conn;
  77. }
  78.  
  79. public boolean executeUpdate(String syntax)
  80. {
  81. try
  82. {
  83. PreparedStatement statement = this.conn.prepareStatement(syntax);
  84. statement.executeUpdate();
  85. statement.close();
  86. return true;
  87. }
  88. catch (SQLException e)
  89. {
  90. sendException(e);
  91. }
  92. return false;
  93. }
  94.  
  95. public ResultSet getResult(String syntax)
  96. {
  97. try
  98. {
  99. PreparedStatement statement = this.conn.prepareStatement(syntax);
  100. return statement.executeQuery();
  101. }
  102. catch (SQLException e)
  103. {
  104. sendException(e);
  105. }
  106. return null;
  107. }
  108.  
  109. public boolean existResult(String table, String where, Object value)
  110. {
  111. ResultSet rs = getResult("SELECT * FROM " + table + " WHERE " + where + "='" + value + "'");
  112. try
  113. {
  114. if (rs.next()) {
  115. return true;
  116. }
  117. rs.close();
  118. }
  119. catch (SQLException e)
  120. {
  121. sendException(e);
  122. }
  123. return false;
  124. }
  125.  
  126. private void sendException(Exception e)
  127. {
  128. this.main.sendConsole("�cFolgender Fehler ist aufgereten:");
  129. this.main.sendConsole("�c" + e.getMessage());
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement