Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. package ru.enervate.lobby;
  2.  
  3. import java.sql.*;
  4. import java.util.concurrent.*;
  5.  
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.plugin.Plugin;
  8.  
  9. @SuppressWarnings("ALL")
  10. public class SQLUtil {
  11.  
  12. private static SQLUtil util;
  13.  
  14. private Connection connection;
  15. private Plugin plugin;
  16. private String dbLocation;
  17.  
  18. private String connect;
  19.  
  20. private String host;
  21. private String port;
  22. private String database;
  23. private String user;
  24. private String pass;
  25.  
  26. private ExecutorService service = Executors.newFixedThreadPool(1);
  27.  
  28. public SQLUtil(Plugin main) {
  29. util = this;
  30. plugin = main;
  31.  
  32. host = plugin.getConfig().getString("mysql.host");
  33. port = plugin.getConfig().getString("mysql.port");
  34. database = plugin.getConfig().getString("mysql.database");
  35. user = plugin.getConfig().getString("mysql.user");
  36. pass = plugin.getConfig().getString("mysql.pass");
  37. connect = "jdbc:mysql://" + host + ":" + port + "/" + database + "?useUnicode=true&" + "user=" + user + "&password=" + pass;
  38. }
  39.  
  40. public static SQLUtil getInstance() {
  41. return util;
  42. }
  43.  
  44. public synchronized void openConnection() {
  45. try {
  46. Class.forName("com.mysql.jdbc.Driver").newInstance();
  47. connection = DriverManager.getConnection(connect);
  48. } catch(Exception e) {
  49. Bukkit.getLogger().info("####################################################");
  50. Bukkit.getLogger().info(" ");
  51. Bukkit.getLogger().info("[SQLUtil]: Check your config! Plugin can not connect to the database!");
  52. Bukkit.getLogger().info(" ");
  53. Bukkit.getLogger().info("####################################################");
  54. }
  55. }
  56.  
  57. public void closeConnection() {
  58. try {
  59. connection.close();
  60. connection = null;
  61. plugin = null;
  62. dbLocation = null;
  63. connect = null;
  64. host = null;
  65. port = null;
  66. database = null;
  67. user = null;
  68. pass = null;
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. }
  72. }
  73.  
  74. public synchronized void execute(final String query, final Object... values) {
  75. new SQLThread(query, values).start();
  76. }
  77.  
  78. public synchronized ResultSet executeQuery(final String query, final Object... values) {
  79. final Future<ResultSet> f = service.submit(new SQLTask(query, values));
  80.  
  81. /*while(!f.isDone()) {
  82. // nothing... ugh
  83. }*/
  84.  
  85. try {
  86. return f.get();
  87. } catch (Exception e1) {
  88. return null;
  89. }
  90. }
  91.  
  92. private class StartThread extends Thread {
  93. public void run() {
  94. openConnection();
  95. }
  96. }
  97.  
  98. private class SQLThread extends Thread {
  99.  
  100. private String query;
  101. private Object[] values;
  102.  
  103. public SQLThread(final String query, final Object... values) {
  104. this.query = query;
  105. this.values = values;
  106. }
  107.  
  108. public void run() {
  109. try {
  110. if(connection == null || connection.isClosed()) {
  111. openConnection();
  112. }
  113.  
  114. PreparedStatement ps = connection.prepareStatement(query);
  115.  
  116. for (int i = 0; i < values.length; i++) {
  117. ps.setObject(i + 1, values[i]);
  118. }
  119.  
  120. ps.executeUpdate();
  121. ps.close();
  122. } catch (Exception e) {
  123. System.out.println("SQLUtil Exception!");
  124. System.out.println(" ");
  125. e.printStackTrace();
  126. }
  127. }
  128.  
  129. }
  130.  
  131. private class SQLTask implements Callable<ResultSet> {
  132. private String query;
  133. private Object[] values;
  134.  
  135. public SQLTask(final String query, final Object... values) {
  136. this.query = query;
  137. this.values = values;
  138. }
  139.  
  140. @Override
  141. public ResultSet call() {
  142. try {
  143. if (connection == null || connection.isClosed()) {
  144. openConnection();
  145. }
  146.  
  147. PreparedStatement ps = connection.prepareStatement(query);
  148.  
  149. for (int i = 0; i < values.length; i++) {
  150. ps.setObject(i + 1, values[i]);
  151. }
  152.  
  153. return ps.executeQuery();
  154. } catch (Exception e1) {
  155. System.out.println("SQLUtil Exception!");
  156. System.out.println(" ");
  157. e1.printStackTrace();
  158. return null;
  159. }
  160. }
  161. }
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement