Advertisement
Guest User

Untitled

a guest
Nov 28th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. package net.glaucus.xxnurioxx.libs; import java.io.IOException; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; import com.mysql.jdbc.Connection; public class GMySQL { private Plugin instance; private Connection connection; private String host = "localhost"; private String database = "DB"; private String username = "user"; private String password = "123"; private int port = 3306; public GMySQL(Plugin pmain, String host, int port, String database, String username, String password){ instance = pmain; this.host = host; this.password = password; this.username = username; this.password = database; this.port = port; } public GMySQL(Plugin pmain,boolean loadByConfig){ instance = pmain; try { loadConfig(); } catch (IOException e) { TODO Auto-generated catch block e.printStackTrace(); } } Start MySQL (Only works if runned) public void start(){ Bukkit.getScheduler().runTaskAsynchronously(instance, new Runnable() { @Override public void run() { new Thread(){ public void run() { while(true){ try{ if (connection != null && !connection.isClosed()) { Thread.sleep(1000*30); continue; } connect(); Thread.sleep(1000*10); }catch(Exception er){} } }}.start(); } }); } Query Class public Statement getStatement(){ try { return connection.createStatement(); } catch (SQLException e) { System.err.println("[MySQL] "+e.getMessage()); } return null; } Connection field public Connection getConnection(){ return connection; } SIDE ONLY private void connect() { try{ if (connection != null && !connection.isClosed()) return; Class.forName("com.mysql.jdbc.Driver"); connection = (Connection) DriverManager.getConnection("jdbc:mysql:" + host + ":" + port + "/" + database, username, password); if(!connection.isClosed())System.out.println("[MySQL] "+"Connected to '"+database+"'"); }catch(Exception e){ System.err.println("[MySQL] "+e.getMessage()); } } private void loadConfig() throws IOException { NLConfig cfg = new NLConfig("mysql", instance); if (!cfg.getConfig().isSet("host") || !cfg.getConfig().isSet("port") || !cfg.getConfig().isSet("database") || !cfg.getConfig().isSet("username") || !cfg.getConfig().isSet("password")){ cfg.getConfig().set("host", host); cfg.set("database", database); cfg.set("username", username); cfg.set("password", password); cfg.set("port", port); cfg.save(); } port = cfg.getConfig().getInt("port"); host = cfg.getConfig().getString("host"); database = cfg.getConfig().getString("database"); username = cfg.getConfig().getString("username"); password = cfg.getConfig().getString("password"); } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement