Search in sources :

Example 41 with SQLException

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;
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement)

Example 42 with SQLException

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;
}
Also used : SQLException(java.sql.SQLException) Arguments(com.iCo6.IO.mini.Arguments) Connection(java.sql.Connection) QueryRunner(com.iCo6.util.org.apache.commons.dbutils.QueryRunner)

Example 43 with SQLException

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);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) QueryRunner(com.iCo6.util.org.apache.commons.dbutils.QueryRunner)

Example 44 with SQLException

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);
    }
}
Also used : Player(org.bukkit.entity.Player) SQLException(java.sql.SQLException) Connection(java.sql.Connection) QueryRunner(com.iCo6.util.org.apache.commons.dbutils.QueryRunner)

Example 45 with SQLException

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;
}
Also used : Player(org.bukkit.entity.Player) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) QueryRunner(com.iCo6.util.org.apache.commons.dbutils.QueryRunner)

Aggregations

SQLException (java.sql.SQLException)17708 PreparedStatement (java.sql.PreparedStatement)7646 ResultSet (java.sql.ResultSet)6472 Connection (java.sql.Connection)6226 Statement (java.sql.Statement)3089 ArrayList (java.util.ArrayList)2495 Test (org.junit.Test)1525 IOException (java.io.IOException)1220 List (java.util.List)760 HashMap (java.util.HashMap)633 CallableStatement (java.sql.CallableStatement)506 Map (java.util.Map)471 Properties (java.util.Properties)426 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)387 Timestamp (java.sql.Timestamp)363 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)328 DatabaseMetaData (java.sql.DatabaseMetaData)326 ResultSetMetaData (java.sql.ResultSetMetaData)301 DataSource (javax.sql.DataSource)295 File (java.io.File)280