Example usage for org.apache.commons.dbutils QueryRunner update

List of usage examples for org.apache.commons.dbutils QueryRunner update

Introduction

In this page you can find the example usage for org.apache.commons.dbutils QueryRunner update.

Prototype

public int update(String sql, Object... params) throws SQLException 

Source Link

Document

Executes the given INSERT, UPDATE, or DELETE SQL statement.

Usage

From source file:jongo.jdbc.JDBCExecutor.java

/**
 * Executes the given {@link jongo.sql.Delete} object
 * @param delete a {@link jongo.sql.Delete} instance
 * @return number of records deleted/*from www .ja  va2s. com*/
 * @throws SQLException from the QueryRunner
 * @see org.apache.commons.dbutils.QueryRunner
 */
public static int delete(final Delete delete) throws SQLException {
    l.debug(delete.toString());
    DatabaseConfiguration dbconf = conf.getDatabaseConfiguration(delete.getTable().getDatabase());
    QueryRunner run = JDBCConnectionFactory.getQueryRunner(dbconf);
    Dialect dialect = DialectFactory.getDialect(dbconf);

    try {
        int deleted = run.update(dialect.toStatementString(delete), JongoUtils.parseValue(delete.getId()));
        l.debug("Deleted " + deleted + " records.");
        return deleted;
    } catch (SQLException ex) {
        l.debug(ex.getMessage());
        throw ex;
    }
}

From source file:jongo.jdbc.JDBCExecutor.java

/**
 * Executes the given {@link jongo.sql.Update} object
 * @param update a {@link jongo.sql.Update} instance
 * @return a List of {@link jongo.rest.xstream.Row} with the modified records
 * @throws SQLException from the QueryRunner
 * @see org.apache.commons.dbutils.QueryRunner
 *///  ww  w .  j  a  v  a  2 s.  c o m
public static List<Row> update(final Update update) throws SQLException {
    l.debug(update.toString());

    DatabaseConfiguration dbconf = conf.getDatabaseConfiguration(update.getTable().getDatabase());
    QueryRunner run = JDBCConnectionFactory.getQueryRunner(dbconf);
    Dialect dialect = DialectFactory.getDialect(dbconf);

    List<Row> results = new ArrayList<Row>();
    try {
        int ret = run.update(dialect.toStatementString(update), JongoUtils.parseValues(update.getParameters()));
        if (ret != 0) {
            results = get(update.getSelect(), false);
        }
    } catch (SQLException ex) {
        l.error(ex.getMessage());
        throw ex;
    }
    l.debug("Updated " + results.size() + " records.");
    return results;
}

From source file:azkaban.project.JdbcProjectLoaderTest.java

private static void clearDB() {
    if (!testDBExists) {
        return;//from   w  w w  . j  a  v a2  s.  com
    }

    DataSource dataSource = DataSourceUtils.getMySQLDataSource(host, port, database, user, password,
            numConnections);
    Connection connection = null;
    try {
        connection = dataSource.getConnection();
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    QueryRunner runner = new QueryRunner();
    try {
        runner.update(connection, "DELETE FROM projects");

    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_events");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_permissions");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_files");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_flows");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_properties");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    DbUtils.closeQuietly(connection);
}

From source file:jongo.demo.Demo.java

private static void update(QueryRunner run, String stmt, Object... args) {
    if (args.length == 0) {
        //            System.out.println(stmt); // uncomment to print SQL
    } else {/* ww  w .j  a  v  a 2s  . c om*/
        String k = stmt;
        for (Object o : args) {
            String p = "";
            if (o instanceof java.lang.Number)
                p = String.valueOf(o);
            else
                p = "'" + String.valueOf(o) + "'";
            k = k.replaceFirst("\\?", p);
        }
        //            System.out.println(k); // uncomment to print SQL
    }
    try {
        run.update(stmt, args);
    } catch (SQLException ex) {
        l.error("Failed to update database", ex);
    }
}

From source file:com.example.data.StoreData.java

public void deleteOrder(long orderId) throws SQLException {
    QueryRunner run = new QueryRunner(H2DB.getDataSource());
    run.update("delete from order where id=?", orderId);
}

From source file:com.example.data.UserData.java

public void removeUser(String username) throws SQLException {
    QueryRunner run = new QueryRunner(H2DB.getDataSource());
    run.update("delete from user where user_name=?", username);
}

From source file:com.rhino.data.db.TickerDao.java

public int deleteTicker(String equity) throws SQLException {
    String sql = "delete from ticker where equity=?";
    QueryRunner run = new QueryRunner(DataSourceFactory.getDataSource());
    return run.update(sql, equity);
}

From source file:com.rhino.data.db.TickerDao.java

public int insertTicker(String equity, Ticker ticker, String grade, String exchange) throws SQLException {
    String sql = "insert into ticker (equity,openPrice,closePrice,highPrice,lowPrice,adjustedClose,volume,date,grade,exchange) values(?,?,?,?,?,?,?,?,?,?)";
    String date = Util.getDate(ticker.getDate());
    Object[] params = new Object[] { equity, ticker.getOpenPrice(), ticker.getClosePrice(),
            ticker.getHighPrice(), ticker.getLowPrice(), ticker.getAdjustedClose(), ticker.getVolume(), date,
            grade, exchange };//  www . ja v  a 2  s. co  m
    QueryRunner run = new QueryRunner(DataSourceFactory.getDataSource());
    int updates = run.update(sql, params);
    run.update("delete from ticker where volume=0");
    return updates;
}

From source file:ch.vorburger.mariadb4j.MariaDB4jSampleTutorialTest.java

@Test
public void testEmbeddedMariaDB4j() throws Exception {
    DB db = DB.newEmbeddedDB(3308);//from   w  w w .  j a  v  a 2s .c o  m
    db.start();

    Connection conn = null;
    try {
        conn = db.getConnection();
        QueryRunner qr = new QueryRunner();

        // Should be able to create a new table
        qr.update(conn, "CREATE TABLE hello(world VARCHAR(100))");

        // Should be able to insert into a table
        qr.update(conn, "INSERT INTO hello VALUES ('Hello, world')");

        // Should be able to select from a table
        List<String> results = qr.query(conn, "SELECT * FROM hello", new ColumnListHandler<String>());
        Assert.assertEquals(1, results.size());
        Assert.assertEquals("Hello, world", results.get(0));

        // Should be able to source a SQL file
        db.source("ch/vorburger/mariadb4j/testSourceFile.sql");
        results = qr.query(conn, "SELECT * FROM hello", new ColumnListHandler<String>());
        Assert.assertEquals(3, results.size());
        Assert.assertEquals("Hello, world", results.get(0));
        Assert.assertEquals("Bonjour, monde", results.get(1));
        Assert.assertEquals("Hola, mundo", results.get(2));
    } finally {
        DbUtils.closeQuietly(conn);
    }
}

From source file:com.fluke.database.dataservice.EODDao.java

public int deleteEODTicker(String equity) throws SQLException {
    String sql = "delete from EOD where equity=?";
    QueryRunner run = new QueryRunner(DatabaseProperty.getDataSource());
    return run.update(sql, equity);
}