package me.tabr.mysqldbconnection;
/* auto config update
* code improovements
*
* */
import org.bukkit.Server;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.Plugin;
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.List;
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())
{
// log.info("[1]");
ArrayList<String> tmp = new ArrayList<String>();
// log.info("[2]");
for (int i = 1;i<this.maxColIndex;i++)
{
// log.info("[3]");
try
{
// log.info("[4]");
tmp.add(res.getString(i));
// log.info("adding...");
}
catch (SQLException e)
{
// log.info("BREAKING!!");
//It's ok
break;
}
}
// log.info("[4]");
data.add(tmp);
}
return data;
}
catch (SQLException e)
{
log.info("[mdbc] SQL Exception: "+e);
}
log.info("[returning]");
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", this.DefaultPingTimeout);
config.set("maxColIndex", this.DefaultMaxColIndex);
this.saveConfig();
log.info("[mdbc] New config created");
}
public void onDisable()
{
//are there any plugins that depend on me?
List<String> depend;
for (Plugin plugin : getServer().getPluginManager().getPlugins()) {
if (!plugin.isEnabled()) continue;
depend = (List<String>) plugin.getDescription().getDepend();
if (depend != null && depend.contains(this.getName())) {
getServer().getPluginManager().disablePlugin(plugin);
}
}
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");//обезопасить!
if (maxColIndex == 0)//maybe config parameter not set
{
config.set("maxColIndex", this.DefaultMaxColIndex);
this.saveConfig();
}
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);
}
}