Advertisement
Guest User

Untitled

a guest
Jun 29th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. package eu.skywen.wencoreproxy.database.model;
  2.  
  3. import eu.skywen.wencoreproxy.WencoreProxy;
  4. import eu.skywen.wencoreproxy.log.model.LogType;
  5.  
  6. import java.sql.*;
  7. import java.util.concurrent.TimeUnit;
  8.  
  9. public class MySQL {
  10.  
  11. private Connection connection;
  12. private String driver;
  13. private String connectionString;
  14. private WencoreProxy plugin;
  15.  
  16. public MySQL(WencoreProxy plugin, String hostname, int port, String database, String username, String password) {
  17. this.plugin = plugin;
  18. driver = "com.mysql.jdbc.Driver";
  19. connectionString = String.format("jdbc:mysql://%s:%d/%s?user=%s&autoReconnect=true&useSSL=false&password=%s&useUnicode=true&characterEncoding=UTF-8", hostname, port, database, username, password);
  20. }
  21.  
  22. public Connection createConnection() {
  23. try {
  24. Class.forName(driver);
  25. connection = DriverManager.getConnection(connectionString);
  26. } catch (SQLException e) {
  27. plugin.getLogController().log("Database §b| §fCould not connect to the database! Error: %s", LogType.ERROR, e.getMessage());
  28. } catch (ClassNotFoundException e) {
  29. plugin.getLogController().log("Database §b| §fDriver %s not found!", LogType.ERROR, driver);
  30. } catch (Exception e) {
  31. System.out.println(e.getMessage());
  32. }
  33.  
  34. return connection;
  35. }
  36.  
  37. private Connection getConnection() {
  38. if (connection != null) return connection;
  39. return createConnection();
  40. }
  41.  
  42. public boolean isConnected() {
  43. return connection != null;
  44. }
  45.  
  46. public void close() {
  47. try {
  48. if (getConnection() != null) {
  49. getConnection().close();
  50. }
  51. } catch (SQLException e) {
  52. plugin.getLogController().log("Database §b| §fCould not connect to the database! Error: %s", LogType.ERROR, e.getMessage());
  53. }
  54. connection = null;
  55. }
  56.  
  57. public Result query(String query) {
  58. return preparedQuery(query, true, null);
  59. }
  60.  
  61. public Result query(String query, boolean retry) {
  62. return preparedQuery(query, retry, null);
  63. }
  64.  
  65. public Result preparedQuery(String query, Object... args) {
  66. return preparedQuery(query, true, args);
  67. }
  68.  
  69. public Result preparedQuery(String query, boolean retry, Object... args) {
  70. try {
  71. PreparedStatement statement = null;
  72.  
  73. try {
  74. statement = connection.prepareStatement(query);
  75.  
  76. if (args != null) {
  77. for (int i = 0; i < args.length; i++) {
  78. Object obj = args[i];
  79. if (obj != null) {
  80. statement.setObject(i + 1, obj);
  81. }
  82. }
  83. }
  84.  
  85. if (statement.execute()) {
  86. return new Result(statement, statement.getResultSet());
  87. }
  88.  
  89. } catch (SQLException e) {
  90. String message = e.getMessage();
  91. plugin.getLogController().log("Database §b| §fError in executing query: %s", LogType.WARNING, message);
  92.  
  93. if (retry && message.contains("_BUSY")) {
  94. plugin.getLogController().log("Database §b| §fRetrying to execute query: %s", LogType.INFO, query);
  95. plugin.getProxy().getScheduler().schedule(plugin, () -> preparedQuery(query, true, args), 1, TimeUnit.SECONDS);
  96. }
  97. }
  98.  
  99. if (statement != null) {
  100. statement.close();
  101. }
  102.  
  103. } catch (SQLException e) {
  104. plugin.getLogController().log("Database §b| §fError in executing query: %s", LogType.WARNING, e.getMessage());
  105. }
  106.  
  107. return null;
  108. }
  109.  
  110.  
  111. public class Result {
  112. private ResultSet resultSet;
  113. private Statement statement;
  114.  
  115. public Result(Statement statement, ResultSet resultSet) {
  116. this.statement = statement;
  117. this.resultSet = resultSet;
  118. }
  119.  
  120. public ResultSet getResultSet() {
  121. return this.resultSet;
  122. }
  123.  
  124. public void close() {
  125. try {
  126. this.statement.close();
  127. this.resultSet.close();
  128. } catch (SQLException e) {
  129. }
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement