Advertisement
Lisenochek

Untitled

Aug 27th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. package ru.lisenochek.mcrust.sql;
  2.  
  3. import ru.lisenochek.mcrust.Config;
  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.  
  14. private SQL() {
  15. sql = this;
  16. }
  17.  
  18. public static SQL getSQL() {
  19. return sql == null ? new SQL() : sql;
  20. }
  21.  
  22. public void connect() throws ClassNotFoundException, SQLException {
  23. Class.forName("com.mysql.jdbc.Driver");
  24. connection = DriverManager.getConnection("jdbc:mariadb://" + Config.SQL_IP.getString() + ":" + Config.SQL_PORT.getString() + "/", Config.SQL_USER.getString(), Config.SQL_PASSWORD.getString());
  25. }
  26.  
  27. public void close() throws SQLException {
  28. if (connection != null) connection.close();
  29. }
  30.  
  31. public void execute(String query) {
  32.  
  33. if (query == null || query.isEmpty()) {
  34. Logger.getLogger("Невозможно выполнить запрос! Он равен null или пуст!").error();
  35. return;
  36. }
  37.  
  38. try (Statement statement = connection.prepareStatement(query)) {
  39. statement.execute(query);
  40. } catch (SQLException e) {
  41. Logger.getLogger("Что-то пошло не так и при запросе появилось исключение:").error();
  42. e.printStackTrace();
  43. }
  44. }
  45.  
  46. public ResultSet executeQuery(String query) {
  47.  
  48. if (query == null || query.isEmpty()) {
  49. Logger.getLogger("Невозможно выполнить запрос! Он равен null или пуст!").error();
  50. return null;
  51. }
  52.  
  53. ResultSet resultSet = null;
  54.  
  55. try (Statement statement = connection.prepareStatement(query)) {
  56. resultSet = statement.executeQuery(query);
  57. } catch (SQLException e) {
  58. Logger.getLogger("Что-то пошло не так и при запросе появилось исключение:").error();
  59. e.printStackTrace();
  60. }
  61.  
  62. return resultSet;
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement