mdbc v.0.2
Details
-
Filenamemdbc.jar
-
Uploaded by
-
UploadedMar 3, 2012
-
Size5.63 KB
-
Downloads486
-
MD5d63088f15a2eb1b2c2bab25516baaa38
Supported Bukkit Versions
- CB 1.1-R6
Changelog
- added ArrayList<ArrayList<String>> executeQuery and int executeUpdate methods,so u don't need include sql features. Don't fogret update config file!!!
code:
package me.tabr.mysqldbconnection; import org.bukkit.Server; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; import java.sql.Connection; //import java.sql.DatabaseMetaData; import java.sql.DriverManager; //import java.sql.ResultSet; import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; //import java.util.List; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; public class MDBCMain extends JavaPlugin { private static final Logger log = Logger.getLogger("Minecraft"); private static String dbHost = null; private String dbPort = null; private String dbUser = null; private String dbPassword = null; private String dbDatabase = null; private FileConfiguration config = null; private Connection connection = null; private Statement ownStmt = null; private final int DefaultPingTimeout= 300; private final int DefaultMaxColIndex= 32; private int PingTimeout = DefaultPingTimeout; private int maxColIndex = DefaultMaxColIndex; public int executeUpdate(String SQL) { int res = 0; try { res = this.ownStmt.executeUpdate(SQL); } catch (SQLException e) { log.info("[mdbc] SQL Exception: "+e); } return res; } public ArrayList<ArrayList<String>> executeQuery (String SQL) { ResultSet res = null; ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>(); try { res = this.ownStmt.executeQuery(SQL); // int size = res.getFetchSize(); while (res.next()) { ArrayList<String> tmp = new ArrayList<String>(); for (int i = 1;i<this.maxColIndex;i++) { try { tmp.add(res.getString(i)); // log.info("adding..."); } catch (SQLException e) { // log.info("BREAKING!!"); //It's ok break; } } data.add(tmp); } return data; } catch (SQLException e) { log.info("[mdbc] SQL Exception: "+e); } return data; } public void Test() { log.log(Level.INFO,"[mdbc] test successfull"); } public boolean tableExists(String table) { ResultSet res = null; boolean bool; try { DatabaseMetaData data = this.connection.getMetaData(); res = data.getTables(null, null, table, null); bool = res.next(); return bool; } catch (SQLException e) { log.log(Level.SEVERE, "[mdbc]: Could not load table " + table + " for: mysql: " + e); return false; } finally { try { if (res != null) { res.close(); } } catch (Exception e) { log.log(Level.SEVERE, "[mdbc]: Could not close connection to mysql database: " + e); return false; } } } public Statement getStmt() { try { return connection.createStatement(); } catch (SQLException e) { log.log(Level.INFO,"[mdbc] connection create exception: "+e); return null; } } private void createSampleConfig() { config.set("host", "localhost"); config.set("port", "3306"); config.set("database", "database"); config.set("user", "user"); config.set("password", "password"); config.set("PingTimeout", 300); config.set("maxColIndex", 32); this.saveConfig(); log.info("[mdbc] New config created"); } public void onDisable() { this.disconnect(); } public void onEnable() { log.info("[mdbc] starting..."); this.config = this.getConfig(); if (!(this.config.contains("host"))) { this.createSampleConfig(); } dbHost = this.config.get("host").toString(); dbPort = this.config.get("port").toString(); dbDatabase = this.config.get("database").toString(); dbUser = this.config.get("user").toString(); dbPassword = this.config.get("password").toString(); PingTimeout = this.config.getInt("PingTimeout");//обезопасить! maxColIndex = this.config.getInt("maxColIndex");//обезопасить! try { this.connect(); ownStmt = this.getStmt(); } catch (ClassNotFoundException e) { log.log(Level.INFO,"[mdbc] ClassNotFoundException"); getServer().getPluginManager().disablePlugin(this); return; } catch (SQLException e) { log.log(Level.INFO,"[mdbc] SQLException"); getServer().getPluginManager().disablePlugin(this); return; } pingDBConnection(); } private void pingDBConnection() { Server BukkitServer = this.getServer(); BukkitServer.getScheduler().scheduleSyncDelayedTask(this, new Runnable() { public void run() { log.info("ping db connection..."); try { // ownStmt.p ownStmt.executeUpdate("SHOW TABLES");//hmm.... // ownStmt.executeUpdate("SELECT 1");//hmm.... } catch (SQLException e) { log.info("[mcdb] Ping SQLException :"+e); } pingDBConnection(); } },20*PingTimeout);//5 min } private void disconnect() { try { this.ownStmt.close(); } catch (Exception e) { log.log(Level.INFO,"[mdbc] mysql statement connection close error!"+e); } try { this.connection.close(); } catch (Exception e) { log.log(Level.INFO,"[mdbc] mysql connection close error!"+e); } } private void connect() throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); String str = "jdbc:mysql://"+dbHost+":"+dbPort+"/"+dbDatabase+"?user="+dbUser+"&password="+dbPassword; this.connection = DriverManager.getConnection(str); } }