public boolean init() { database = getNewDatabase(); database.open(); if (!database.checkConnection()) { return false; } if (!database.createTable( "CREATE TABLE IF NOT EXISTS " + accounts + "(" + pname + " varchar(64), " + pbalance + " double);")) { return false; } if (supportsModification) { database.query("ALTER TABLE " + accounts + " MODIFY " + pname + " varchar(64)"); } return true; }
protected void saveAccount(String name, double money) { if (accountExists(name)) { String sql = "UPDATE " + accounts + " SET " + pbalance + "=? WHERE " + pname + "=?"; try { PreparedStatement prest = database.prepare(sql); prest.setDouble(1, money); prest.setString(2, name); prest.executeUpdate(); prest.close(); } catch (SQLException e) { } } else { String sql = "INSERT INTO " + accounts + " (" + pname + ", " + pbalance + ") VALUES (?, ?)"; try { PreparedStatement prest = database.prepare(sql); prest.setString(1, name); prest.setDouble(2, money); prest.executeUpdate(); prest.close(); } catch (SQLException e) { } } }
public double loadAccountMoney(String name) { String sql = "SELECT * FROM " + accounts + " WHERE " + pname + "=?"; double money = -1; try { PreparedStatement prest = database.prepare(sql); prest.setString(1, name); ResultSet set = prest.executeQuery(); while (set.next()) { money = set.getDouble(pbalance); } prest.close(); } catch (SQLException e) { e.printStackTrace(); return -1; } return money; }
public void clean() { String sql = "SELECT * FROM " + accounts + " WHERE " + pbalance + "=?"; try { PreparedStatement prest = database.prepare(sql); prest.setDouble(1, plugin.getAPI().getDefaultHoldings()); ResultSet set = prest.executeQuery(); boolean executeQuery = false; String deleteQuery = "DELETE FROM " + accounts + " WHERE " + pname + " IN ("; while (set.next()) { String name = set.getString(pname); if (plugin.getServer().getPlayerExact(name) != null) { continue; } executeQuery = true; deleteQuery += "'" + name + "', "; } prest.close(); deleteQuery = deleteQuery.substring(0, deleteQuery.length() - 2) + ")"; if (executeQuery) { database.query(deleteQuery); } } catch (SQLException e) { } }
public void removeAccount(String name) { String sql = "DELETE FROM " + accounts + " WHERE " + pname + "=?"; try { PreparedStatement prest = database.prepare(sql); prest.setString(1, name); prest.execute(); prest.close(); } catch (SQLException e) { } }
public List<Account> getTopAccounts() { String sql = "SELECT " + pname + " FROM " + accounts + " ORDER BY " + pbalance + " DESC limit 5"; List<Account> topAccounts = new ArrayList<Account>(); ResultSet set = database.query(sql); try { while (set.next()) { topAccounts.add(getAccount(set.getString(pname))); } } catch (SQLException e) { } return topAccounts; }
public void close() { database.close(); }