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(Connection conn, String sql, Object... params) throws SQLException 

Source Link

Document

Execute an SQL INSERT, UPDATE, or DELETE query.

Usage

From source file:dbutils.ExampleJDBC.java

/**
 * ?/* w ww.  j  av a  2s  .c o m*/
 */
public static void insertAndUpdateData() throws SQLException {
    Connection conn = getConnection();
    QueryRunner qr = new QueryRunner();
    try {
        //??insert?
        Object[] insertParams = { "John Doe", "", 12, 3 };
        int inserts = qr.update(conn, "INSERT INTO test_student(name,gender,age,team_id) VALUES (?,?,?,?)",
                insertParams);
        System.out.println("inserted " + inserts + " data");

        Object[] updateParams = { "John Doe Update", "John Doe" };
        int updates = qr.update(conn, "UPDATE test_student SET name=? WHERE name=?", updateParams);
        System.out.println("updated " + updates + " data");
    } catch (SQLException e) {
        e.printStackTrace();
        conn.rollback();
    } finally {
        DbUtils.close(conn);
    }
}

From source file:com.pinterest.deployservice.db.DatabaseUtil.java

public static void transactionalUpdate(BasicDataSource dataSource, List<UpdateStatement> updateStatements)
        throws Exception {
    QueryRunner queryRunner = new QueryRunner();
    Connection connection = dataSource.getConnection();
    boolean autoStatus = connection.getAutoCommit();
    connection.setAutoCommit(false);/*from   w  w w .j a v a2  s  .c  om*/
    try {
        for (UpdateStatement updateStatement : updateStatements) {
            queryRunner.update(connection, updateStatement.getStatement(), updateStatement.getValueArray());
        }
        connection.commit();
    } catch (SQLException e) {
        connection.rollback();
        throw e;
    } finally {
        connection.setAutoCommit(autoStatus);
        DbUtils.closeQuietly(connection);
    }
}

From source file:jp.co.golorp.emarf.model.Models.java

/**
 * ???//from   w  w  w . j  a va  2 s  .c o  m
 *
 * @param sql
 *            ?sql
 * @param params
 *            sql?
 * @return ?
 */
private static int regist(final String sql, final Object... params) {

    Date sysDate = DateUtil.getDate();

    for (int i = 0; i < params.length; i++) {
        Object param = params[i];
        if (StringUtil.isNotBlank(param) && param.toString().equals("@{sysDate}")) {
            params[i] = new Timestamp(sysDate.getTime());
        }
    }

    statementLog(getRawSql(sql, params));

    QueryRunner runner = new QueryRunner();
    Connection cn = Connections.get();
    try {
        return runner.update(cn, sql, params);
    } catch (SQLException e) {
        throw new SystemError(e);
    }
}

From source file:cn.itcast.bbs.dao.TypeDao.java

public void deleteTypeById(int typeId) throws SQLException {
    QueryRunner runner = new QueryRunner();
    String sql = "delete from type where id = ?;";
    runner.update(JdbcUtil.getConnection(), sql, typeId);
}

From source file:cn.itcast.bbs.dao.ReplyDao.java

public void deleteReplyByTopicId(int topicId) throws SQLException {
    QueryRunner runner = new QueryRunner();
    String sql = "delete from reply where topic_id = ?;";
    runner.update(JdbcUtil.getConnection(), sql, topicId);
}

From source file:net.orpiske.ssps.common.db.AbstractDao.java

/**
 * Runs an UPDATE/INSERT/DELETE statement
 * @param query The query (statement) to run
 * @param args The arguments to the query
 * @return The number of affected rows/*from  w ww .j a  v  a 2s . c o  m*/
 * @throws SQLException If unable to perform the operation
 */
protected int runUpdate(String query, Object... args) throws SQLException {
    Connection conn = databaseManager.getConnection();

    QueryRunner run = new QueryRunner();

    return run.update(conn, query, args);
}

From source file:cn.itcast.bbs.dao.TopicDao.java

public void deleteTopicByTypeId(int typeId) throws SQLException {
    QueryRunner runner = new QueryRunner();
    String sql = "delete from topic where type_id = ?;";
    runner.update(JdbcUtil.getConnection(), sql, typeId);
}

From source file:com.ouc.cpss.dao.BaseDao.java

/**
 * ??//w w w .  ja va  2 s  . co m
 *
 * @param conn 
 * @param sql sql?
 * @param params ?
 * @return
 * @throws java.sql.SQLException
 */
public boolean update(Connection conn, String sql, Object[] params) throws SQLException {
    boolean flag = false;
    QueryRunner qRunner = new QueryRunner();
    int i = qRunner.update(conn, sql, params);
    if (i > 0) {
        flag = true;
    }
    return flag;
}

From source file:com.ouc.cpss.dao.BaseDao.java

/**
 * sql?,?,,//  w  w w. j  a  v  a  2s. co  m
 *
 * @param sql
 * @return
 */
public boolean update(String sql, Object[] params) {
    Connection conn = null;
    boolean flag = false;
    try {
        conn = getConnection();
        QueryRunner qRunner = new QueryRunner();
        int i = qRunner.update(conn, sql, params);
        if (i > 0) {
            flag = true;
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DbUtils.closeQuietly(conn);
    }
    return flag;
}

From source file:com.demo.admin.dao.impl.AdminDriverDaoImpl.java

@Override
public int updateActivedById(Long id) {
    QueryRunner queryRunner = dbHelper.getRunner();
    try {// w w w .  jav  a 2  s .  c o  m
        int rows = queryRunner.update(dbHelper.getConnection(), "update demo_driver set actived=1 where id=?",
                id);
        return rows;
    } catch (SQLException e) {
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        logger.error("{}??{},{}", methodName, id);
        throw new RuntimeException(e);
    }
}