Advertisement
Lisenochek

Untitled

Aug 27th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. package ru.lisenochek.mcrust.sql;
  2.  
  3. import ru.lisenochek.mcrust.Main;
  4. import ru.lisenochek.mcrust.utils.Logger;
  5.  
  6. import java.sql.*;
  7.  
  8. public class SQL {
  9.  
  10. private static SQL sql;
  11.  
  12. private Connection connection;
  13. private ResultSet resultSet;
  14.  
  15. private SQL() {
  16. sql = this;
  17. }
  18.  
  19. public static SQL getSQL() {
  20. return sql == null ? new SQL() : sql;
  21. }
  22.  
  23. public void connect() throws SQLException, ClassNotFoundException {
  24. Class.forName("org.h2.Driver");
  25. connection = DriverManager.getConnection("jdbc:h2://" + Main.plugin.getDataFolder().getAbsolutePath() + "/Database;mode=MySQL", "sa", "");
  26. }
  27.  
  28. public void close() throws SQLException {
  29. if (connection != null) connection.close();
  30. }
  31.  
  32. public void execute(String query) {
  33.  
  34. if (query == null || query.equals("")) {
  35. Logger.getLogger("Невозможно выполнить запрос! Он равен null или пуст!").error();
  36. return;
  37. }
  38.  
  39. try {
  40. Statement statement = connection.createStatement();
  41. statement.execute(query);
  42. statement.close();
  43. } catch (SQLException e) {
  44. Logger.getLogger("Что-то пошло не так и при запросе появилось исключение:").error();
  45. e.printStackTrace();
  46. }
  47. }
  48.  
  49. public ResultSet executeQuery(String query) {
  50.  
  51. if (query == null || query.equals("")) {
  52. Logger.getLogger("Невозможно выполнить запрос! Он равен null или пуст!").error();
  53. return null;
  54. }
  55.  
  56. try {
  57. resultSet = connection.createStatement().executeQuery(query);
  58. } catch (SQLException e) {
  59. Logger.getLogger("Что-то пошло не так и при запросе появилось исключение:").error();
  60. e.printStackTrace();
  61. }
  62.  
  63. return resultSet;
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement