Guest User

Untitled

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