Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- */
- package com.mortalpvp.core.global.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;
- /**
- * @author Stijn van Nieulande
- * @date 16 okt. 2017
- * @project MPCore
- */
- public class MySQLConnection_BACKUP {
- private MySQLConnectionDetails conn_details = null;
- private Connection c = null;
- /**
- * Create a new instance of the MySQLConnection
- *
- * @param conn_details
- * The MySQLConnectionDetails
- */
- public MySQLConnection_BACKUP(MySQLConnectionDetails conn_details) {
- this.conn_details = conn_details;
- }
- /**
- * Create a new instance of the MySQLConnection
- */
- public MySQLConnection_BACKUP() {
- String className = new Exception().getStackTrace()[1].getClassName();
- if (className.startsWith("me.Stijn.MPCore")) {
- this.conn_details = new MySQLConnectionDetails();
- } else {
- System.err.println("Connection is not made by the Core :/ Please use your own connection details which you can get from the Lead Developer!");
- }
- }
- /**
- * Open the connection
- */
- public Connection open() {
- if (c != null) {
- return c;
- } else {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- this.c = DriverManager.getConnection(String.format("jdbc:mysql://%s:%s/%s?autoReconnect=true&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&connectionCollation=utf8mb4_unicode_ci",
- conn_details.getHost(), conn_details.getPort(), conn_details.getDatabase()), conn_details.getUser(), conn_details.getPassword());
- return this.c;
- } catch (SQLException e) {
- System.out.println(String.format("jdbc:mysql://%s:%s/%s?autoReconnect=true&useUnicode=true&characterEncoding=utf8", conn_details.getHost(), conn_details.getPort(), conn_details.getDatabase()));
- System.out.println("Could not connect to MySQL server! because: " + e.getMessage());
- } catch (ClassNotFoundException e) {
- System.out.println("JDBC Driver not found!");
- }
- }
- return null;
- }
- /**
- * Get the MySQLConnectionDetails
- *
- * @return The MySQLConnectionDetails
- */
- public MySQLConnectionDetails getMySQLConnectionDetails() {
- return this.conn_details;
- }
- /**
- * Get the connection
- *
- * @return The connection or null if not opened
- */
- public Connection getConn() {
- return this.c;
- }
- /**
- * Close the connection
- */
- public void close() {
- if (this.c != null) {
- try {
- c.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- c = null;
- }
- /*
- * =========================================================================
- * Do Query / Update SYNC
- * =========================================================================
- */
- /**
- * Execute a query
- *
- * @param sql
- * The query string
- * @param callback
- * The callback
- */
- public void doQuery(String sql, MySQLConnectionBeforeCloseCallback callback) {
- this.open();
- ResultSet res = null;
- try {
- Statement s = this.c.createStatement();
- res = s.executeQuery(sql);
- callback.executeBeforeClose(res);
- res.close();
- s.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- try {
- c.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- /**
- * Execute a query
- *
- * @param prepared_statement
- * The prepared statement
- * @param callback
- * The callback
- */
- public void doQuery(PreparedStatement prepared_statement, MySQLConnectionBeforeCloseCallback callback) {
- this.open();
- ResultSet res = null;
- try {
- res = prepared_statement.executeQuery();
- callback.executeBeforeClose(res);
- res.close();
- prepared_statement.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- try {
- c.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- public interface MySQLConnectionBeforeCloseCallback {
- void executeBeforeClose(ResultSet result);
- }
- /**
- * Execute a update
- *
- * @param sql
- * The sql
- * @return Count or -1 if failed
- */
- public int doUpdate(String sql) {
- this.open();
- int res = -1;
- try {
- Statement s = c.createStatement();
- res = s.executeUpdate(sql);
- s.close();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- try {
- c.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- return res;
- }
- /**
- * Execute a query
- *
- * @param prepared_statement
- * The prepared statement
- * @return Count or -1 if failed
- */
- public int doUpdate(PreparedStatement prepared_statement) {
- this.open();
- int res = -1;
- try {
- res = prepared_statement.executeUpdate();
- prepared_statement.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- try {
- c.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement