Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.kopara.api.bungee.mysql;
- /**
- * Created by Wojtek on 09.02.2017.
- */
- import net.md_5.bungee.BungeeCord;
- import pl.kopara.api.bungee.BungeePlugin;
- import pl.kopara.api.bungee.utils.Logger;
- import java.sql.*;
- import java.util.concurrent.Executor;
- import java.util.concurrent.Executors;
- import java.util.concurrent.TimeUnit;
- public class BungeeMySQL {
- private final String host;
- private final String user;
- private final String pass;
- private final String name;
- private final int port;
- private final String prefix;
- private Connection conn;
- private Executor executor;
- public BungeeMySQL(final String host, final int port, final String user, final String pass, final String name, final String prefix) {
- super();
- this.executor = Executors.newFixedThreadPool(6);
- this.host = host;
- this.port = port;
- this.user = user;
- this.pass = pass;
- this.name = name;
- this.prefix = prefix;
- BungeeCord.getInstance().getScheduler().schedule(BungeePlugin.getPlugins(), new Runnable() {
- @Override
- public void run() {
- BungeePlugin.getMySQL().execute("SELECT CURTIME()");
- }
- }, 15, TimeUnit.MINUTES);
- }
- public boolean connect(){
- final long time = System.currentTimeMillis();
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Logger.debug("Try connect with MySQL");
- this.conn = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.name, this.user, this.pass);
- Logger.debug("Connected to the MySQL with ping: " + (System.currentTimeMillis() - time) + "ms!");
- return true;
- }
- catch (ClassNotFoundException e) {
- Logger.debug("JDBC driver not found Error: " + e.getMessage());
- e.printStackTrace();
- }
- catch (SQLException e2) {
- Logger.debug("Can not connect to a MySQL server! Error: " + e2.getMessage());
- e2.printStackTrace();
- BungeeCord.getInstance().stop();
- }
- return false;
- }
- public void INSERT(String table, String values, String q) {
- executeUpdate("INSERT INTO `{P}" + table + "` (`id`," + values +") VALUES (NULL, '" + q + "');");
- }
- public void DELETE(String table, String where, String name) {
- executeUpdate("DELETE FROM `{P}" +table + "` WHERE `" + where + "` ='" + name + "';");
- }
- public void DELETE(String table, String where1, String name1, String where2, String name2) {
- executeUpdate("DELETE FROM `{P}" +table + "` WHERE `" + where1 + "` ='" + name1 + "' AND `" + where2 + "` ='" + name2 + "';");
- }
- public void UPDATE(String table, String values, String where, String name) {
- executeUpdate("UPDATE `{P}" + table + "` SET " + values + " WHERE `" + where + "` ='" + name +"';");
- }
- public void CREATETABLE(String table, String values) {
- executeUpdate("CREATE TABLE IF NOT EXISTS `{P}" + table + "` (`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, " + values + ");");
- }
- public ResultSet SELECT(final String table, final String where, final String name) {
- return query("SELECT * FROM `{P}" + table + "` WHERE `" + where + "` ='" + name + "';");
- }
- public ResultSet SELECT(final String table) {
- return query("SELECT * FROM `{P}" + table + "`;");
- }
- public void executeUpdate(String q) {
- this.executor.execute(() -> update(q));
- }
- private void update(String query){
- try {
- if (!this.isConnected()) {
- Logger.debug("CONNECTION CLOSE OPEN!");
- connect();
- }
- final Statement st = this.conn.createStatement();
- st.executeUpdate(query.replace("{P}", this.prefix));
- st.close();
- } catch (SQLException e) {
- Logger.debug(e.getMessage());
- e.printStackTrace();
- }
- }
- private ResultSet query(String query){
- try {
- if (!this.isConnected()) {
- Logger.debug("CONNECTION CLOSE OPEN!");
- connect();
- }
- Statement st = this.conn.createStatement();
- return st.executeQuery(query.replace("{P}", this.prefix));
- } catch (SQLException e) {
- Logger.debug(e.getMessage());
- e.printStackTrace();
- return null;
- }
- }
- public Connection getConnecrion() {
- return this.conn;
- }
- public boolean isConnected() {
- try {
- return !this.conn.isClosed() || this.conn == null;
- }
- catch (SQLException e) {
- e.printStackTrace();
- return false;
- }
- }
- private void execute(String query){
- if(this.conn == null) connect();
- try {
- Statement st = this.conn.createStatement();
- st.execute(query);
- st.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void disconnect() {
- if (this.conn != null) {
- try {
- this.conn.close();
- }
- catch (SQLException e) {
- Logger.debug("Can not close the connection to the MySQL Error: " + e.getMessage());
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement