use of java.sql.SQLException in project Core by iConomy.
the class QueryRunner method update.
/**
* Execute an SQL INSERT, UPDATE, or DELETE query.
*
* @param conn The connection to use to run the query.
* @param sql The SQL to execute.
* @param params The query replacement parameters.
* @return The number of rows updated.
* @throws SQLException if a database access error occurs
*/
public int update(Connection conn, String sql, Object... params) throws SQLException {
PreparedStatement stmt = null;
int rows = 0;
try {
stmt = this.prepareStatement(conn, sql);
this.fillStatement(stmt, params);
rows = stmt.executeUpdate();
} catch (SQLException e) {
this.rethrow(e, sql, params);
} finally {
close(stmt);
}
return rows;
}
use of java.sql.SQLException in project Core by iConomy.
the class MoneyComparator method createAccount.
static boolean createAccount(String name, Double balance, Integer status) {
Boolean created = false;
if (useMiniDB() || useInventoryDB() || useOrbDB()) {
if (hasAccount(name))
return false;
if (useOrbDB())
if (iConomy.Server.getPlayer(name) != null)
return false;
if (useInventoryDB())
if (inventory.dataExists(name))
return false;
Arguments Row = new Arguments(name);
Row.setValue("balance", balance);
Row.setValue("status", status);
database.addIndex(Row.getKey(), Row);
database.update();
return true;
}
try {
QueryRunner run = new QueryRunner();
Connection c = iConomy.Database.getConnection();
try {
String t = Constants.Nodes.DatabaseTable.toString();
Integer amount = run.update(c, "INSERT INTO " + t + "(username, balance, status) values (?, ?, ?)", name.toLowerCase(), balance, status);
if (amount > 0)
created = true;
} catch (SQLException ex) {
System.out.println("[iConomy] Error issueing SQL query: " + ex);
} finally {
DbUtils.close(c);
}
} catch (SQLException ex) {
System.out.println("[iConomy] Database Error: " + ex);
}
return false;
}
use of java.sql.SQLException in project Core by iConomy.
the class MoneyComparator method setStatus.
static void setStatus(String name, int status) {
if (!hasAccount(name))
return;
if (useMiniDB()) {
database.setArgument(name, "status", status);
database.update();
return;
}
if (useInventoryDB() || useOrbDB()) {
if (database.hasIndex(name)) {
database.setArgument(name, "status", status);
database.update();
}
return;
}
try {
QueryRunner run = new QueryRunner();
Connection c = iConomy.Database.getConnection();
try {
String t = Constants.Nodes.DatabaseTable.toString();
int update = run.update(c, "UPDATE " + t + " SET status=? WHERE username=?", status, name.toLowerCase());
} catch (SQLException ex) {
System.out.println("[iConomy] Error issueing SQL query: " + ex);
} finally {
DbUtils.close(c);
}
} catch (SQLException ex) {
System.out.println("[iConomy] Database Error: " + ex);
}
}
use of java.sql.SQLException in project Core by iConomy.
the class MoneyComparator method setBalance.
static void setBalance(String name, double balance) {
double original = 0.0, gain = 0.0, loss = 0.0;
if (Constants.Nodes.Logging.getBoolean()) {
original = getBalance(name);
gain = balance - original;
loss = original - balance;
}
if (!hasAccount(name)) {
createAccount(name, balance, 0);
if (Constants.Nodes.Logging.getBoolean()) {
if (gain < 0.0)
gain = 0.0;
if (loss < 0.0)
loss = 0.0;
Transactions.insert(new Transaction("setBalance", "System", name).setFromBalance(original).setToBalance(balance).setGain(gain).setLoss(loss).setSet(balance));
}
return;
}
if (useMiniDB() || useInventoryDB() || useOrbDB()) {
if (useInventoryDB())
if (inventory.dataExists(name)) {
inventory.setBalance(name, balance);
return;
}
if (useOrbDB()) {
Player gainer = iConomy.Server.getPlayer(name);
if (gainer != null)
if (balance < gainer.getTotalExperience()) {
int amount = (int) (gainer.getTotalExperience() - balance);
for (int i = 0; i < amount; i++) {
if (gainer.getExp() > 0)
gainer.setExp(gainer.getExp() - 1);
else if (gainer.getTotalExperience() > 0)
gainer.setTotalExperience(gainer.getTotalExperience() - 1);
else
break;
}
} else {
int amount = (int) (balance - gainer.getTotalExperience());
for (int i = 0; i < amount; i++) gainer.setExp(gainer.getExp() + 1);
}
return;
}
if (database.hasIndex(name)) {
database.setArgument(name, "balance", balance);
database.update();
}
return;
}
try {
QueryRunner run = new QueryRunner();
Connection c = iConomy.Database.getConnection();
try {
String t = Constants.Nodes.DatabaseTable.toString();
int update = run.update(c, "UPDATE " + t + " SET balance=? WHERE username=?", balance, name.toLowerCase());
} catch (SQLException ex) {
System.out.println("[iConomy] Error issueing SQL query: " + ex);
} finally {
DbUtils.close(c);
}
} catch (SQLException ex) {
System.out.println("[iConomy] Database Error: " + ex);
}
}
use of java.sql.SQLException in project Core by iConomy.
the class MoneyComparator method accountList.
static List<String> accountList() {
List<String> accounts = new ArrayList<String>();
if (useMiniDB() || useInventoryDB() || useOrbDB()) {
if (useInventoryDB())
accounts.addAll(inventory.getAllPlayers());
if (useOrbDB())
for (Player p : iConomy.Server.getOnlinePlayers()) accounts.add(p.getName());
accounts.addAll(database.getIndices().keySet());
return accounts;
}
try {
QueryRunner run = new QueryRunner();
Connection c = iConomy.Database.getConnection();
try {
String t = Constants.Nodes.DatabaseTable.toString();
accounts = run.query(c, "SELECT username FROM " + t, returnList);
} catch (SQLException ex) {
System.out.println("[iConomy] Error issueing SQL query: " + ex);
} finally {
DbUtils.close(c);
}
} catch (SQLException ex) {
System.out.println("[iConomy] Database Error: " + ex);
}
return accounts;
}
Aggregations