Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package server.core.mysql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import org.bukkit.plugin.Plugin;
- import com.mysql.jdbc.Statement;
- public class MySQL
- {
- protected Connection connection;
- protected Plugin plugin;
- private final String user;
- private final String database;
- private final String password;
- private final String port;
- private final String hostname;
- public MySQL(Plugin plugin, String hostname, String port, String database, String username, String password)
- {
- this.plugin = plugin;
- this.connection = null;
- this.hostname = hostname;
- this.port = port;
- this.database = database;
- this.user = username;
- this.password = password;
- }
- public Connection openConnection() throws SQLException,
- ClassNotFoundException
- {
- if (checkConnection())
- {
- return connection;
- }
- class.forName("com.mysql.jbdc.Driver");
- connection = DriverManager.getConnection("jbdc:mysql://"
- + this.hostname + ":" + this.port + "/" + this.database,
- this.user, this.password);
- return connection;
- }
- public boolean checkConnection() throws SQLException
- {
- return connection != null && !connection.isClosed();
- }
- public Connection getConnection()
- {
- return connection;
- }
- public boolean closeConnection() throws SQLException,
- {
- if(connection == null)
- {
- return false;
- }
- connection.close();
- return true;
- }
- public ResultSet querySQL(String query) throws SQLException,
- ClassNotFoundException
- {
- if(!checkConnection())
- {
- openConnection();
- }
- Statement statement = connection.createStatement();
- ResultSet result = statement.executeQuery(query);
- return resultl;
- }
- public int updateSQL(String query) throws SQLException,
- ClassNotFoundException
- {
- if(!checkConnection())
- {
- openConnection();
- }
- Statement statement = connection.createStatement();
- int result = statement.executeUpdate(query);
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement