Advertisement
Guest User

Untitled

a guest
Dec 25th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. public class MySQL {
  2.  
  3. private Connection connection;
  4. private PreparedStatement preparedStatement;
  5. private ResultSet resultSet;
  6. private String host;
  7. private int port;
  8. private String database;
  9. private String user;
  10. private String password;
  11.  
  12. public MySQL(String host, int port, String database, String user, String password) {
  13. this.host = host;
  14. this.port = port;
  15. this.database = database;
  16. this.user = user;
  17. this.password = password;
  18. }
  19.  
  20. public void connect() {
  21. try {
  22. this.connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?autoReconnect=true", this.user, this.password);
  23. System.out.println("[MySQL successfully connected...]");
  24. } catch (SQLException ex) {
  25. System.out.println("[MySQL successfully disconnected...]");
  26. }
  27. }
  28.  
  29. public Connection getConnection() {
  30. return this.connection;
  31. }
  32.  
  33. public boolean isConnected() {
  34. if (this.connection != null) {
  35. return true;
  36. }
  37. return false;
  38. }
  39.  
  40. public void disconnect() {
  41. if (this.connection != null) {
  42. try {
  43. this.connection.close();
  44. } catch (SQLException ex) {
  45. }
  46. }
  47. }
  48.  
  49. public void update(String update) {
  50. try {
  51. this.preparedStatement = this.connection.prepareStatement(update);
  52. this.preparedStatement.executeUpdate(update);
  53. } catch (SQLException ex) {
  54. }
  55. }
  56.  
  57. public ResultSet select(String query) {
  58. try {
  59. this.preparedStatement = this.connection.prepareStatement(query);
  60. this.resultSet = this.preparedStatement.executeQuery();
  61. } catch (SQLException ex) {
  62. ex.printStackTrace();
  63. }
  64. return resultSet;
  65. }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement