Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. package ru.nestway.mysql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. public abstract class Database
  9. {
  10. protected Connection connection;
  11.  
  12. protected Database()
  13. {
  14. this.connection = null;
  15. }
  16.  
  17. public abstract Connection openConnection()
  18. throws SQLException, ClassNotFoundException;
  19.  
  20. public boolean checkConnection()
  21. throws SQLException
  22. {
  23. return (this.connection != null) && (!this.connection.isClosed());
  24. }
  25.  
  26. public Connection getConnection()
  27. {
  28. return this.connection;
  29. }
  30.  
  31. public boolean closeConnection()
  32. throws SQLException
  33. {
  34. if (this.connection == null) {
  35. return false;
  36. }
  37. this.connection.close();
  38. return true;
  39. }
  40.  
  41. public ResultSet querySQL(String query)
  42. throws SQLException, ClassNotFoundException
  43. {
  44. if (!checkConnection()) {
  45. openConnection();
  46. }
  47. Statement statement = this.connection.createStatement();
  48.  
  49. ResultSet result = statement.executeQuery(query);
  50.  
  51. return result;
  52. }
  53.  
  54. public int updateSQL(String query)
  55. throws SQLException, ClassNotFoundException
  56. {
  57. if (!checkConnection()) {
  58. openConnection();
  59. }
  60. Statement statement = this.connection.createStatement();
  61.  
  62. int result = statement.executeUpdate(query);
  63.  
  64. return result;
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement