Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. package de.proxycloud.bungeesystem.mysql;
  2.  
  3. import java.io.PrintStream;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9.  
  10. public class DatabaseManager
  11. {
  12. private Connection connection;
  13. private final String hostname;
  14. private final String database;
  15. private final String username;
  16. private final String password;
  17. private final String url;
  18.  
  19. public DatabaseManager(String hostname, String database, String username, String password)
  20. {
  21. this.hostname = hostname;
  22. this.database = database;
  23. this.username = username;
  24. this.password = password;
  25. this.url = ("jdbc:mysql://" + this.hostname + ":3306/" + this.database);
  26. }
  27.  
  28. public void connect()
  29. {
  30. if (!isConnected()) {
  31. try
  32. {
  33. this.connection = DriverManager.getConnection(this.url, this.username, this.password);
  34. System.out.println("[MySQL] The Connection to MySQL was sucsefulled created.");
  35. }
  36. catch (SQLException e)
  37. {
  38. System.out.println("[MySQL] Failed to connect to MySQL.");
  39. }
  40. }
  41. }
  42.  
  43. public void disconnect()
  44. {
  45. if (isConnected()) {
  46. try
  47. {
  48. this.connection.close();
  49. this.connection = null;
  50. System.out.println("[MySQL] The Connection from MySQL is disconnected");
  51. }
  52. catch (SQLException e)
  53. {
  54. System.out.println("[MySQL] Failed to disconnect from MySQL.");
  55. }
  56. }
  57. }
  58.  
  59. public void update(String qry)
  60. {
  61. try
  62. {
  63. PreparedStatement preparedStatement = this.connection.prepareStatement(qry);
  64. preparedStatement.executeUpdate(qry);
  65. }
  66. catch (SQLException e)
  67. {
  68. e.printStackTrace();
  69. }
  70. }
  71.  
  72. public ResultSet query(String qry)
  73. {
  74. try
  75. {
  76. PreparedStatement preparedStatement = this.connection.prepareStatement(qry);
  77. return preparedStatement.executeQuery(qry);
  78. }
  79. catch (SQLException e)
  80. {
  81. e.printStackTrace();
  82. }
  83. return null;
  84. }
  85.  
  86. public boolean isConnected()
  87. {
  88. return this.connection != null;
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement