Advertisement
Guest User

Untitled

a guest
Aug 6th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;
  5.  
  6. /**
  7. * This class represents a wrapper of the database data
  8. * and provides handle-functions that make any interaction
  9. * with the database easier.
  10. */
  11. public class Database {
  12.  
  13. private String host, user, database;
  14. private int port;
  15. private Connection connection;
  16. private Statement statement;
  17.  
  18. /**
  19. * Constructs a new {@link Database} object.
  20. *
  21. * @param hostname The hostname. May be an IP, a domain or simply 'localhost'.
  22. * @param port The port (default: 3306).
  23. * @param username The login for the database.
  24. * @param password The password for the database
  25. * - will <b>NOT</b> be stored as a field and will <b>NOT</b>
  26. * be accessible through getters or even reflection.
  27. * @param database The name of the database.
  28. * @throws SQLException Thrown when a connection may not be established.
  29. * @throws ClassNotFoundException Thrown if the {@code com.mysql.jdbc.Driver class}
  30. * cannot be found.
  31. */
  32. public Database(String hostname, int port, String username, String password, String database) throws SQLException, ClassNotFoundException {
  33. host = hostname;
  34. this.port = port;
  35. user = username;
  36. this.database = database;
  37. openConnection(password);
  38. statement = connection.createStatement();
  39. }
  40.  
  41. /**
  42. * Returns the host. May be an IP, a domain or simply 'localhost'.
  43. * @return The address to the database's server.
  44. */
  45. public String getHost() {
  46. return host;
  47. }
  48.  
  49. /**
  50. * Returns the login (username). Required for authentication purposes.
  51. * @return The username.
  52. */
  53. public String getUser() {
  54. return user;
  55. }
  56.  
  57. /**
  58. * Returns the database's name.
  59. * @return The database.
  60. */
  61. public String getDatabase() {
  62. return database;
  63. }
  64.  
  65. /**
  66. * Returns the {@link java.sql.Connection object}.
  67. * Used to retrieve the {@link java.sql.Statement object}
  68. * in order to execute queries and updates.
  69. *
  70. * @return The {@link java.sql.Connection}.
  71. */
  72. public Connection getConnection() {
  73. return connection;
  74. }
  75.  
  76. /**
  77. * Returns the {@link java.sql.Statement object}.
  78. * @return The {@link java.sql.Statement}.
  79. */
  80. public Statement getStatement() {
  81. return statement;
  82. }
  83.  
  84.  
  85. //////////////////////
  86. // FUNCTIONAL STUFF //
  87. //////////////////////
  88.  
  89. /**
  90. * Opens the connection for the first time.
  91. * <br>
  92. * If the connection is already open, nothing will happen.
  93. *
  94. * @param password The database password.
  95. * Passed as a function parameter for security purposes.
  96. * @throws SQLException Thrown when a connection cannot be established.
  97. * @throws ClassNotFoundException Thrown when the {@link com.mysql.jdbc.Driver class} is not found.
  98. */
  99. protected void openConnection(String password) throws SQLException, ClassNotFoundException {
  100. if (connection != null && !connection.isClosed()) {
  101. return;
  102. }
  103.  
  104. synchronized (this) {
  105. if (connection != null && !connection.isClosed()) {
  106. return;
  107. }
  108. Class.forName("com.mysql.jdbc.Driver");
  109. connection = DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + this.port + "/" + this.database, this.user, password);
  110. }
  111. }
  112.  
  113. /**
  114. * Closes the current connection gracefully.
  115. * Should <b>always</b> be called when the plugin
  116. * is disabled to avoid memory leaks unclean throttling.
  117. * <br>
  118. * When the connection is not open, nothing will happen.
  119. *
  120. * @throws SQLException Thrown when there is no stable
  121. * connection with the database's server and commands
  122. * cannot be executed successfully.
  123. */
  124. public void closeConnection() throws SQLException {
  125. if (connection != null && !connection.isClosed()) {
  126. connection.close();
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement