Search in sources :

Example 1 with ResultSetHandler

use of com.iCo6.util.org.apache.commons.dbutils.ResultSetHandler in project Core by iConomy.

the class iConomy method onConversion.

public boolean onConversion() {
    if (!Constants.Nodes.Convert.getBoolean())
        return false;
    Thrun.init(new Runnable() {

        public void run() {
            String from = Constants.Nodes.ConvertFrom.toString();
            String table = Constants.Nodes.ConvertTable.toString();
            String username = Constants.Nodes.ConvertUsername.toString();
            String password = Constants.Nodes.ConvertPassword.toString();
            String url = Constants.Nodes.ConvertURL.toString();
            if (!Common.matches(from, "h2", "h2db", "h2sql", "mysql", "mysqldb"))
                return;
            String driver = "", dsn = "";
            if (Common.matches(from, "sqlite", "h2", "h2sql", "h2db")) {
                driver = "org.h2.Driver";
                dsn = "jdbc:h2:" + directory + File.separator + table + ";AUTO_RECONNECT=TRUE";
                username = "sa";
                password = "sa";
            } else if (Common.matches(from, "mysql", "mysqldb")) {
                driver = "com.mysql.jdbc.Driver";
                dsn = url + "/" + table;
            }
            if (!DbUtils.loadDriver(driver)) {
                System.out.println("Please make sure the " + from + " driver library jar exists.");
                return;
            }
            Connection old = null;
            try {
                old = (username.isEmpty() && password.isEmpty()) ? DriverManager.getConnection(url) : DriverManager.getConnection(url, username, password);
            } catch (SQLException ex) {
                System.out.println(ex);
                return;
            }
            QueryRunner run = new QueryRunner();
            try {
                try {
                    run.query(old, "SELECT * FROM " + table, new ResultSetHandler() {

                        public Object handle(ResultSet rs) throws SQLException {
                            Account current = null;
                            Boolean next = rs.next();
                            if (next)
                                if (iConomy.Accounts.exists(rs.getString("username")))
                                    current = iConomy.Accounts.get(rs.getString("username"));
                                else
                                    iConomy.Accounts.create(rs.getString("username"), rs.getDouble("balance"));
                            if (current != null)
                                current.getHoldings().setBalance(rs.getDouble("balance"));
                            if (next)
                                if (iConomy.Accounts.exists(rs.getString("username")))
                                    if (rs.getBoolean("hidden"))
                                        iConomy.Accounts.get(rs.getString("username")).setStatus(1);
                            return true;
                        }
                    });
                } catch (SQLException ex) {
                    System.out.println("[iConomy] Error issueing SQL query: " + ex);
                } finally {
                    DbUtils.close(old);
                }
            } catch (SQLException ex) {
                System.out.println("[iConomy] Database Error: " + ex);
            }
            System.out.println("[iConomy] Conversion complete. Please update your configuration, change convert to false!");
        }
    });
    return false;
}
Also used : Account(com.iCo6.system.Account) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ResultSetHandler(com.iCo6.util.org.apache.commons.dbutils.ResultSetHandler) QueryRunner(com.iCo6.util.org.apache.commons.dbutils.QueryRunner)

Aggregations

Account (com.iCo6.system.Account)1 QueryRunner (com.iCo6.util.org.apache.commons.dbutils.QueryRunner)1 ResultSetHandler (com.iCo6.util.org.apache.commons.dbutils.ResultSetHandler)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1