Advertisement
Guest User

Untitled

a guest
Mar 15th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. package ru.shapex.ghostytops.sql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.plugin.Plugin;
  9.  
  10. public class SQLUtil {
  11.  
  12. public Connection connection = null;
  13. private final Plugin plugin;
  14. private final String dbLocation;
  15.  
  16. public SQLUtil(Plugin plugin, String dbLocation) {
  17. this.plugin = plugin;
  18. this.dbLocation = dbLocation;
  19. }
  20.  
  21. public void openConnection() {
  22. try {
  23. String e = this.plugin.getConfig().getString("mysql.type");
  24. if (e.equalsIgnoreCase("sqlite")) {
  25. Class.forName("org.sqlite.JDBC").newInstance();
  26. this.connection = DriverManager.getConnection("jdbc:sqlite://" + this.plugin.getDataFolder().getAbsolutePath() + "/" + this.dbLocation);
  27. } else if (e.equalsIgnoreCase("mysql")) {
  28. Class.forName("com.mysql.jdbc.Driver").newInstance();
  29. this.connection = DriverManager.getConnection("jdbc:mysql://" + this.plugin.getConfig().getString("mysql.host") + ":" + this.plugin.getConfig().getString("mysql.port") + "/" + this.plugin.getConfig().getString("mysql.database") + "?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&" + "user=" + this.plugin.getConfig().getString("mysql.user") + "&password=" + this.plugin.getConfig().getString("mysql.pass"));
  30. }
  31. } catch (Exception var2) { Bukkit.getLogger().info("Произошла ошибка при подключении к БД."); }
  32. }
  33.  
  34. public void execute(final String query, final Object... values) {
  35. new Thread(new Runnable() {
  36. public void run() {
  37. try {
  38. if ((SQLUtil.this.connection == null) || (SQLUtil.this.connection.isClosed())) { SQLUtil.this.openConnection(); }
  39. PreparedStatement var2 = SQLUtil.this.connection.prepareStatement(query);
  40. for (int i = 0; i < values.length; i++) { var2.setObject(i + 1, values[i]); }
  41. var2.executeUpdate();
  42. } catch (Exception var3) { var3.printStackTrace(); }}}).start();
  43. }
  44.  
  45. public ResultSet executeQuery(String query, Object... values) {
  46. ResultSetThread rst = new ResultSetThread(query, values);
  47. rst.run();
  48. return rst.res;
  49. }
  50.  
  51. public class ResultSetThread extends Thread {
  52. ResultSet res;
  53. String query;
  54. Object[] values;
  55.  
  56. public ResultSetThread(String query, Object... values) {
  57. this.query = query;
  58. this.values = values;
  59. }
  60.  
  61. public void run() {
  62. try {
  63. if ((SQLUtil.this.connection == null) || (SQLUtil.this.connection.isClosed())) { SQLUtil.this.openConnection(); }
  64. PreparedStatement var3 = SQLUtil.this.connection.prepareStatement(this.query);
  65. for (int i = 0; i < this.values.length; i++) {
  66. var3.setObject(i + 1, this.values[i]);
  67. } this.res = var3.executeQuery();
  68. } catch (Exception var31) { var31.printStackTrace(); }
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement