Created
January 18, 2013 02:47
-
-
Save blha303/4561979 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.rayherring; | |
public class OnlinePlayersSQLConfig | |
{ | |
OnlinePlayersSQL plugin; | |
public OnlinePlayersSQLConfig(OnlinePlayersSQL plugin) | |
{ | |
this.plugin = plugin; | |
} | |
public void loadConfiguration() { | |
String mySQLServer = "MySQLServer"; | |
String mySQLPort = "MySQLPort"; | |
String mySQLUsername = "MySQLUsername"; | |
String mySQLPassword = "MySQLPassword"; | |
String mySQLDatabase = "MySQLDatabase"; | |
String mySQLTable = "MySQLTable"; | |
this.plugin.getConfig().addDefault(mySQLServer, "localhost"); | |
this.plugin.getConfig().addDefault(mySQLPort, "3306"); | |
this.plugin.getConfig().addDefault(mySQLUsername, "root"); | |
this.plugin.getConfig().addDefault(mySQLPassword, ""); | |
this.plugin.getConfig().addDefault(mySQLDatabase, "db"); | |
this.plugin.getConfig().addDefault(mySQLTable, "online_players"); | |
this.plugin.getConfig().addDefault("showDebug", Boolean.valueOf(false)); | |
this.plugin.getConfig().addDefault("op_only_resync", Boolean.valueOf(false)); | |
this.plugin.getConfig().options().copyDefaults(true); | |
this.plugin.saveConfig(); | |
} | |
public boolean opOnlyResync() { | |
return this.plugin.getConfig().getBoolean("op_only_resync"); | |
} | |
public String getMySQLServer() { | |
return this.plugin.getConfig().getString("MySQLServer"); | |
} | |
public String getMySQLPort() { | |
return this.plugin.getConfig().getString("MySQLPort"); | |
} | |
public String getMySQLUsername() { | |
return this.plugin.getConfig().getString("MySQLUsername"); | |
} | |
public String getMySQLPassword() { | |
return this.plugin.getConfig().getString("MySQLPassword"); | |
} | |
public String getMySQLDatabase() { | |
return this.plugin.getConfig().getString("MySQLDatabase"); | |
} | |
public String getMySQLTable() { | |
return this.plugin.getConfig().getString("MySQLTable"); | |
} | |
public boolean isShowDebug() { | |
return this.plugin.getConfig().getBoolean("showDebug"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.rayherring; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.util.logging.Logger; | |
public class OnlinePlayersSQLLib | |
{ | |
Logger log = Logger.getLogger("Minecraft"); | |
String url; | |
Connection conn = null; | |
PreparedStatement myQuery = null; | |
OnlinePlayersSQL plugin; | |
public OnlinePlayersSQLLib(OnlinePlayersSQL plugin) | |
{ | |
this.plugin = plugin; | |
this.url = ("jdbc:mysql://" + plugin.opConfig.getMySQLServer() + ":" + plugin.opConfig.getMySQLPort() + "/" + plugin.opConfig.getMySQLDatabase()); | |
} | |
public Connection SQLConnect() throws SQLException { | |
Connection conn = DriverManager.getConnection(this.url, this.plugin.opConfig.getMySQLUsername(), this.plugin.opConfig.getMySQLPassword()); | |
return conn; | |
} | |
public void SQLDisconnect() throws SQLException { | |
this.myQuery.close(); | |
this.conn.close(); | |
} | |
public void updateTableSchema() throws SQLException { | |
this.log.info("Updating Schema information for table."); | |
if (!columnExists(this.plugin.opConfig.getMySQLDatabase(), this.plugin.opConfig.getMySQLTable(), "online")) { | |
this.log.info("Creating additional 'online' column for table."); | |
runUpdateQuery("ALTER TABLE " + this.plugin.opConfig.getMySQLTable() + " ADD COLUMN online boolean default false;"); | |
} | |
if (!columnExists(this.plugin.opConfig.getMySQLDatabase(), this.plugin.opConfig.getMySQLTable(), "last_logout")) { | |
this.log.info("Creating additional 'last_logout' column for table."); | |
runUpdateQuery("ALTER TABLE " + this.plugin.opConfig.getMySQLTable() + " ADD COLUMN last_logout int;"); | |
} | |
if (!columnExists(this.plugin.opConfig.getMySQLDatabase(), this.plugin.opConfig.getMySQLTable(), "first_login")) { | |
this.log.info("Creating additional 'first_login' column for table."); | |
runUpdateQuery("ALTER TABLE " + this.plugin.opConfig.getMySQLTable() + " ADD COLUMN first_login int;"); | |
} | |
} | |
public void runUpdateQuery(String query) { | |
try { | |
this.conn = SQLConnect(); | |
this.myQuery = this.conn.prepareStatement(query); | |
this.myQuery.executeUpdate(); | |
SQLDisconnect(); | |
} | |
catch (SQLException e1) { | |
e1.printStackTrace(); | |
} | |
} | |
public ResultSet runSearchQuery(String query) { | |
ResultSet result = null; | |
try | |
{ | |
this.conn = SQLConnect(); | |
this.myQuery = this.conn.prepareStatement(query); | |
result = this.myQuery.executeQuery(); | |
} catch (SQLException el) { | |
el.printStackTrace(); | |
} | |
return result; | |
} | |
public void createSqlTable() throws SQLException | |
{ | |
runUpdateQuery("CREATE TABLE " + this.plugin.opConfig.getMySQLTable() + | |
"(player varchar(255) not null, " + | |
"previous_world varchar(255), " + | |
"current_world varchar(255), " + | |
"ip_address varchar(16), " + | |
"logon_time int(11), " + | |
"permission_group varchar(255), " + | |
"online boolean default false, " + | |
"last_logout int(11), " + | |
"first_login int(11))"); | |
} | |
public boolean tableExists(String db, String tbl) { | |
ResultSet result = null; | |
Boolean recordExists = Boolean.valueOf(false); | |
String query = "SELECT * FROM Information_Schema.TABLES WHERE Information_Schema.TABLES.TABLE_NAME = '" + | |
tbl + "' " + | |
"AND Information_Schema.TABLES.TABLE_SCHEMA = '" + db + "'"; | |
result = runSearchQuery(query); | |
try | |
{ | |
recordExists = Boolean.valueOf(result.isBeforeFirst()); | |
SQLDisconnect(); | |
return recordExists.booleanValue(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
return false; | |
} | |
public boolean columnExists(String db, String tbl, String column) { | |
ResultSet result = null; | |
Boolean recordExists = Boolean.valueOf(false); | |
String query = "SELECT * FROM Information_Schema.COLUMNS WHERE Information_Schema.COLUMNS.COLUMN_NAME = '" + | |
column + "' " + | |
"AND Information_Schema.COLUMNS.TABLE_NAME = '" + tbl + "' " + | |
"AND Information_Schema.COLUMNS.TABLE_SCHEMA = '" + db + "'"; | |
result = runSearchQuery(query); | |
try | |
{ | |
this.log.info("Result of column " + column + " check: " + result.isBeforeFirst()); | |
recordExists = Boolean.valueOf(result.isBeforeFirst()); | |
SQLDisconnect(); | |
return recordExists.booleanValue(); | |
} | |
catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment