public boolean initialize() { PreparedStatement ps = null; // ResultSet rs = null; table = plugin.getConfig().getString("mysql-table", "player-tracker"); try { conn = getSQLConnection(); DatabaseMetaData dbm = conn.getMetaData(); // Table create if not it exists if (!dbm.getTables(null, null, table, null).next()) { getLogger().log(Level.INFO, "[P-Tracker] Creating table " + table + "."); ps = conn.prepareStatement( "CREATE TABLE `" + table + "` (" + "`id` mediumint( 9 ) NOT NULL AUTO_INCREMENT ," + "`accountname` varchar( 32 ) NOT NULL default '0'," + "`ip` varchar( 15 ) NOT NULL default '0'," + "`time` timestamp NOT NULL default CURRENT_TIMESTAMP ," + "PRIMARY KEY ( `id` )" + ") ENGINE = MYISAM DEFAULT CHARSET = latin1;"); ps.execute(); if (!dbm.getTables(null, null, table, null).next()) throw new SQLException("Table " + table + " not found; tired to create and failed"); } } catch (SQLException ex) { PlayerTracker.log.log(Level.SEVERE, "[P-Tracker] Couldn't execute MySQL statement: ", ex); return false; } return true; }
public Connection getSQLConnection(String mysqlDatabase) { FileConfiguration Config = plugin.getConfig(); String mysqlUser = Config.getString("mysql-user", "root"); String mysqlPassword = Config.getString("mysql-password", "root"); try { return DriverManager.getConnection( mysqlDatabase + "?autoReconnect=true&user="******"&password="******"[P-Tracker]: Unable to create mySQL connection.", ex); } return null; }
public Connection getSQLConnection() { String mysqlDatabase = plugin.getConfig().getString("mysql-database", "jdbc:mysql://localhost:3306/minecraft"); return getSQLConnection(mysqlDatabase); }