Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eu.skywen.wencoreproxy.database.model;
- import eu.skywen.wencoreproxy.WencoreProxy;
- import eu.skywen.wencoreproxy.log.model.LogType;
- import java.sql.*;
- import java.util.concurrent.TimeUnit;
- public class MySQL {
- private Connection connection;
- private String driver;
- private String connectionString;
- private WencoreProxy plugin;
- public MySQL(WencoreProxy plugin, String hostname, int port, String database, String username, String password) {
- this.plugin = plugin;
- driver = "com.mysql.jdbc.Driver";
- connectionString = String.format("jdbc:mysql://%s:%d/%s?user=%s&autoReconnect=true&useSSL=false&password=%s&useUnicode=true&characterEncoding=UTF-8", hostname, port, database, username, password);
- }
- public Connection createConnection() {
- try {
- Class.forName(driver);
- connection = DriverManager.getConnection(connectionString);
- } catch (SQLException e) {
- plugin.getLogController().log("Database §b| §fCould not connect to the database! Error: %s", LogType.ERROR, e.getMessage());
- } catch (ClassNotFoundException e) {
- plugin.getLogController().log("Database §b| §fDriver %s not found!", LogType.ERROR, driver);
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- return connection;
- }
- private Connection getConnection() {
- if (connection != null) return connection;
- return createConnection();
- }
- public boolean isConnected() {
- return connection != null;
- }
- public void close() {
- try {
- if (getConnection() != null) {
- getConnection().close();
- }
- } catch (SQLException e) {
- plugin.getLogController().log("Database §b| §fCould not connect to the database! Error: %s", LogType.ERROR, e.getMessage());
- }
- connection = null;
- }
- public Result query(String query) {
- return preparedQuery(query, true, null);
- }
- public Result query(String query, boolean retry) {
- return preparedQuery(query, retry, null);
- }
- public Result preparedQuery(String query, Object... args) {
- return preparedQuery(query, true, args);
- }
- public Result preparedQuery(String query, boolean retry, Object... args) {
- try {
- PreparedStatement statement = null;
- try {
- statement = connection.prepareStatement(query);
- if (args != null) {
- for (int i = 0; i < args.length; i++) {
- Object obj = args[i];
- if (obj != null) {
- statement.setObject(i + 1, obj);
- }
- }
- }
- if (statement.execute()) {
- return new Result(statement, statement.getResultSet());
- }
- } catch (SQLException e) {
- String message = e.getMessage();
- plugin.getLogController().log("Database §b| §fError in executing query: %s", LogType.WARNING, message);
- if (retry && message.contains("_BUSY")) {
- plugin.getLogController().log("Database §b| §fRetrying to execute query: %s", LogType.INFO, query);
- plugin.getProxy().getScheduler().schedule(plugin, () -> preparedQuery(query, true, args), 1, TimeUnit.SECONDS);
- }
- }
- if (statement != null) {
- statement.close();
- }
- } catch (SQLException e) {
- plugin.getLogController().log("Database §b| §fError in executing query: %s", LogType.WARNING, e.getMessage());
- }
- return null;
- }
- public class Result {
- private ResultSet resultSet;
- private Statement statement;
- public Result(Statement statement, ResultSet resultSet) {
- this.statement = statement;
- this.resultSet = resultSet;
- }
- public ResultSet getResultSet() {
- return this.resultSet;
- }
- public void close() {
- try {
- this.statement.close();
- this.resultSet.close();
- } catch (SQLException e) {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement