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.DriverManager;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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 int PingTimeout = DefaultPingTimeout;
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);
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");//обезопасить!
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.executeUpdate("SELECT 1");//hmm....
}
catch (SQLException e)
{
log.info("[mdbc] Ping SQLException :"+e);
}
pingDBConnection();
}
},20*PingTimeout);
}
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);
}
}
Comments