Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- /**
- * This class represents a wrapper of the database data
- * and provides handle-functions that make any interaction
- * with the database easier.
- */
- public class Database {
- private String host, user, database;
- private int port;
- private Connection connection;
- private Statement statement;
- /**
- * Constructs a new {@link Database} object.
- *
- * @param hostname The hostname. May be an IP, a domain or simply 'localhost'.
- * @param port The port (default: 3306).
- * @param username The login for the database.
- * @param password The password for the database
- * - will <b>NOT</b> be stored as a field and will <b>NOT</b>
- * be accessible through getters or even reflection.
- * @param database The name of the database.
- * @throws SQLException Thrown when a connection may not be established.
- * @throws ClassNotFoundException Thrown if the {@code com.mysql.jdbc.Driver class}
- * cannot be found.
- */
- public Database(String hostname, int port, String username, String password, String database) throws SQLException, ClassNotFoundException {
- host = hostname;
- this.port = port;
- user = username;
- this.database = database;
- openConnection(password);
- statement = connection.createStatement();
- }
- /**
- * Returns the host. May be an IP, a domain or simply 'localhost'.
- * @return The address to the database's server.
- */
- public String getHost() {
- return host;
- }
- /**
- * Returns the login (username). Required for authentication purposes.
- * @return The username.
- */
- public String getUser() {
- return user;
- }
- /**
- * Returns the database's name.
- * @return The database.
- */
- public String getDatabase() {
- return database;
- }
- /**
- * Returns the {@link java.sql.Connection object}.
- * Used to retrieve the {@link java.sql.Statement object}
- * in order to execute queries and updates.
- *
- * @return The {@link java.sql.Connection}.
- */
- public Connection getConnection() {
- return connection;
- }
- /**
- * Returns the {@link java.sql.Statement object}.
- * @return The {@link java.sql.Statement}.
- */
- public Statement getStatement() {
- return statement;
- }
- //////////////////////
- // FUNCTIONAL STUFF //
- //////////////////////
- /**
- * Opens the connection for the first time.
- * <br>
- * If the connection is already open, nothing will happen.
- *
- * @param password The database password.
- * Passed as a function parameter for security purposes.
- * @throws SQLException Thrown when a connection cannot be established.
- * @throws ClassNotFoundException Thrown when the {@link com.mysql.jdbc.Driver class} is not found.
- */
- protected void openConnection(String password) throws SQLException, ClassNotFoundException {
- if (connection != null && !connection.isClosed()) {
- return;
- }
- synchronized (this) {
- if (connection != null && !connection.isClosed()) {
- return;
- }
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + this.port + "/" + this.database, this.user, password);
- }
- }
- /**
- * Closes the current connection gracefully.
- * Should <b>always</b> be called when the plugin
- * is disabled to avoid memory leaks unclean throttling.
- * <br>
- * When the connection is not open, nothing will happen.
- *
- * @throws SQLException Thrown when there is no stable
- * connection with the database's server and commands
- * cannot be executed successfully.
- */
- public void closeConnection() throws SQLException {
- if (connection != null && !connection.isClosed()) {
- connection.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement