Advertisement
Guest User

Untitled

a guest
May 2nd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. package ru.lisenochek.perks.sql;
  2.  
  3. import org.bukkit.Bukkit;
  4. import ru.lisenochek.perks.ConfigClass;
  5. import ru.lisenochek.perks.Logger;
  6. import ru.lisenochek.perks.Main;
  7.  
  8. import java.sql.*;
  9.  
  10. public class SQL {
  11.  
  12. public static Connection connection;
  13. public static Statement statement;
  14. public static ResultSet resultSet;
  15.  
  16. private static String url = "jdbc:mysql://" + ConfigClass.sqlConfig.get("ip") + ":" + ConfigClass.sqlConfig.get("port") + "/" + ConfigClass.sqlConfig.get("databaseName");
  17. private static String user = (String) ConfigClass.sqlConfig.get("user");
  18. private static String password = (String) ConfigClass.sqlConfig.get("password");
  19.  
  20. public synchronized static void closeConnection() throws SQLException {
  21. if (connection == null) return;
  22. if (!connection.isClosed()) connection.close();
  23. }
  24.  
  25. public synchronized static void openConnection() throws ClassNotFoundException, SQLException {
  26.  
  27. if (ConfigClass.sqlMode.equals("sqlite")) {
  28. if (!Main.plugin.getDataFolder().mkdirs()) Main.plugin.getDataFolder().mkdirs();
  29. Class.forName("org.sqlite.JDBC");
  30. connection = DriverManager.getConnection("jdbc:sqlite:plugins/" + Main.plugin.getName() + "/Database.db");
  31. }
  32.  
  33. if (ConfigClass.sqlMode.equals("mysql")) {
  34. Class.forName("com.mysql.jdbc.Driver");
  35. connection = DriverManager.getConnection(url, user, password);
  36. }
  37.  
  38. statement = connection.createStatement();
  39. }
  40.  
  41. public synchronized static boolean executeQuery(String query) {
  42.  
  43. if (query == null || query.equals("")) {
  44. Logger.error("Невозможно выполнить запрос! Он равен null или пуст!");
  45. return false;
  46. }
  47.  
  48. try {
  49. statement.execute(query);
  50. statement.close();
  51. closeConnection();
  52. openConnection();
  53. return true;
  54. } catch (SQLException e) {
  55. Logger.error("Что-то пошло не так и при запросе появилось исключение:");
  56. Logger.error(e.getMessage());
  57. return false;
  58. } catch (ClassNotFoundException e) {
  59. e.printStackTrace();
  60. return false;
  61. }
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement