Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.luist.survivalplus.database;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import org.bukkit.plugin.Plugin;
- public class MySQL {
- private final String user;
- private final String database;
- private final String password;
- private final String port;
- private final String hostname;
- private Connection connection;
- private Plugin plugin;
- public MySQL(Plugin plugin) {
- this.hostname = plugin.getConfig().getString("database.host");
- this.port = plugin.getConfig().getString("database.port");
- this.database = plugin.getConfig().getString("database.name");
- this.user = plugin.getConfig().getString("database.username");
- this.password = plugin.getConfig().getString("database.password");
- this.connection = null;
- this.plugin = plugin;
- openConnection();
- plugin.getLogger().info("MySQL > Fixing database timezone");
- updateSQL("SET time_zone = '+00:00';");
- }
- public Connection openConnection() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- this.connection = DriverManager.getConnection(
- "jdbc:mysql://" + this.hostname + ":" + this.port + "/" + this.database, this.user, this.password);
- } catch (ClassNotFoundException e) {
- plugin.getLogger().severe(e.getMessage());
- return null;
- } catch (SQLException e) {
- plugin.getLogger().severe(e.getMessage());
- return null;
- }
- return this.connection;
- }
- public boolean checkConnection() {
- return this.connection != null;
- }
- public Connection getConnection() {
- return this.connection;
- }
- public void closeConnection() {
- if (this.connection != null) {
- try {
- this.connection.close();
- } catch (SQLException e) {
- plugin.getLogger().warning("Error closing the MySQL Connection!");
- e.printStackTrace();
- }
- }
- }
- public ResultSet querySQL(String query) {
- Connection c = null;
- Statement s = null;
- ResultSet ret = null;
- if (checkConnection()) {
- c = getConnection();
- } else {
- c = openConnection();
- }
- try {
- s = c.createStatement();
- } catch (SQLException e1) {
- plugin.getLogger().warning(e1.getMessage());
- }
- try {
- ret = s.executeQuery(query);
- } catch (SQLException e) {
- plugin.getLogger().warning(e.getMessage());
- }
- closeConnection();
- return ret;
- }
- public void updateSQL(String update) {
- Connection c = null;
- Statement s = null;
- if (checkConnection()) {
- c = getConnection();
- } else {
- c = openConnection();
- }
- try {
- s = c.createStatement();
- s.executeUpdate(update);
- } catch (SQLException e1) {
- plugin.getLogger().warning(e1.getMessage());
- }
- closeConnection();
- }
- public ResultSet select(String statement, String... args) throws SQLException {
- PreparedStatement mySQLstatement = this.connection.prepareStatement(statement);
- for (int i = 0; i < args.length; i++) {
- mySQLstatement.setString(i + 1, args[i]);
- }
- ResultSet result = mySQLstatement.executeQuery();
- return result;
- }
- public void update(String statement, String... args) throws SQLException {
- PreparedStatement mySQLstatement = this.connection.prepareStatement(statement);
- for (int i = 0; i < args.length; i++) {
- mySQLstatement.setString(i + 1, args[i]);
- }
- mySQLstatement.executeUpdate();
- mySQLstatement.close();
- }
- public void insert(String statement, String... args) throws SQLException {
- PreparedStatement mySQLstatement = this.connection.prepareStatement(statement);
- for (int i = 0; i < args.length; i++) {
- mySQLstatement.setString(i + 1, args[i]);
- }
- mySQLstatement.execute();
- mySQLstatement.close();
- }
- }
Add Comment
Please, Sign In to add comment