Example usage for java.sql PreparedStatement executeUpdate

List of usage examples for java.sql PreparedStatement executeUpdate

Introduction

In this page you can find the example usage for java.sql PreparedStatement executeUpdate.

Prototype

int executeUpdate() throws SQLException;

Source Link

Document

Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.

Usage

From source file:mangotiger.sql.SQL.java

public static int update(final Connection connection, final String sql, final Object[] parameters)
        throws SQLException {
    PreparedStatement statement = null;
    try {//  ww  w  .  ja  va 2s  . co m
        statement = newPreparedStatement(connection, sql, parameters);
        return statement.executeUpdate();
    } catch (SQLException e) {
        log().error("unable to execute " + sql + asString(sql, parameters), e);
        throw e;
    } finally {
        close(statement);
    }
}

From source file:com.glaf.activiti.util.ExecutionUtils.java

@SuppressWarnings("unchecked")
public static void executeSqlUpdate(DelegateExecution execution, Expression sql) {
    CommandContext commandContext = Context.getCommandContext();

    ExecutionEntity executionEntity = commandContext.getExecutionEntityManager()
            .findExecutionById(execution.getId());
    String processDefinitionId = executionEntity.getProcessDefinitionId();
    ProcessDefinitionEntity processDefinitionEntity = commandContext.getProcessDefinitionEntityManager()
            .findProcessDefinitionById(processDefinitionId);
    String processName = processDefinitionEntity.getKey();

    Map<String, Object> params = new java.util.HashMap<String, Object>();

    Map<String, Object> variables = execution.getVariables();
    if (variables != null && variables.size() > 0) {
        Iterator<String> iterator = variables.keySet().iterator();
        while (iterator.hasNext()) {
            String variableName = iterator.next();
            if (params.get(variableName) == null) {
                Object value = execution.getVariable(variableName);
                params.put(variableName, value);
            }/*from   ww w . j  av a 2s  .  com*/
        }
    }

    params.put(Constants.BUSINESS_KEY, execution.getProcessBusinessKey());
    params.put("processInstanceId", execution.getProcessInstanceId());
    params.put("processDefinitionId", processDefinitionEntity.getId());
    params.put("processName", processName);
    params.put("now", new java.util.Date());

    if (sql != null) {
        String sqlx = sql.getExpressionText();

        if (sqlx.indexOf("#{tableName}") != -1) {
            String tableName = (String) execution.getVariable("tableName");
            if (StringUtils.isNotEmpty(tableName)) {
                sqlx = StringTools.replace(sqlx, "#{tableName}", tableName);
            }
        } else if (sqlx.indexOf("${tableName}") != -1) {
            String tableName = (String) execution.getVariable("tableName");
            if (StringUtils.isNotEmpty(tableName)) {
                sqlx = StringTools.replace(sqlx, "${tableName}", tableName);
            }
        }

        sqlx = StringTools.replaceIgnoreCase(sqlx, "${", "#{");

        List<Object> values = new java.util.ArrayList<Object>();
        SqlExecutor sqlExecutor = JdbcUtils.rebuildSQL(sqlx, params);
        sqlx = sqlExecutor.getSql();
        if (sqlExecutor.getParameter() != null) {
            if (sqlExecutor.getParameter() instanceof List) {
                List<Object> list = (List<Object>) sqlExecutor.getParameter();
                values.addAll(list);
            }
        }

        logger.debug(sqlx);
        logger.debug(values);

        Connection con = null;
        try {
            con = commandContext.getDbSqlSession().getSqlSession().getConnection();
            PreparedStatement psmt = con.prepareStatement(sqlx);
            JdbcUtils.fillStatement(psmt, values);
            psmt.executeUpdate();
            psmt.close();
            psmt = null;
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }

    }
}

From source file:com.sql.EmailAttachment.java

/**
 * Insert attachement into received email attachment database table.
 * // w  w  w .j a  v  a2s.co m
 * @param EmailID Integer
 * @param fileName String
 */
public static void insertEmailAttachment(int EmailID, String fileName) {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = DBConnection.connectToDB();
        String sql = "INSERT INTO EmailAttachment (" + "emailID, " + "fileName " + ") VALUES (" + "?, " + "?)";
        ps = conn.prepareStatement(sql);
        ps.setInt(1, EmailID);
        ps.setString(2, fileName);
        ps.executeUpdate();
    } catch (SQLException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
    }
}

From source file:Main.java

public static int update(Connection connection, String sql, List<Object> parameters) throws SQLException {
    int numRowsUpdated = 0;
    PreparedStatement ps = null;
    try {/*from ww  w .j a v  a 2 s.c o m*/
        ps = connection.prepareStatement(sql);

        int i = 0;
        for (Object parameter : parameters) {
            ps.setObject(++i, parameter);
        }
        numRowsUpdated = ps.executeUpdate();
    } finally {
        close(ps);
    }
    return numRowsUpdated;
}

From source file:com.dangdang.ddframe.rdb.sharding.example.transaction.Main.java

private static void updateFailure(final DataSource dataSource) throws SQLException {
    String sql1 = "UPDATE t_order SET status='UPDATE_1' WHERE user_id=10 AND order_id=1000";
    String sql2 = "UPDATE t_order SET not_existed_column=1 WHERE user_id=1 AND order_id=?";
    String sql3 = "UPDATE t_order SET status='UPDATE_2' WHERE user_id=10 AND order_id=1000";
    SoftTransactionManager transactionManager = new SoftTransactionManager(
            getSoftTransactionConfiguration(dataSource));
    transactionManager.init();/*from w w w.j a  v a2  s  . co m*/
    BEDSoftTransaction transaction = (BEDSoftTransaction) transactionManager
            .getTransaction(SoftTransactionType.BestEffortsDelivery);
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        transaction.begin(conn);
        PreparedStatement preparedStatement1 = conn.prepareStatement(sql1);
        PreparedStatement preparedStatement2 = conn.prepareStatement(sql2);
        preparedStatement2.setObject(1, 1000);
        PreparedStatement preparedStatement3 = conn.prepareStatement(sql3);
        preparedStatement1.executeUpdate();
        preparedStatement2.executeUpdate();
        preparedStatement3.executeUpdate();
    } finally {
        transaction.end();
        if (conn != null) {
            conn.close();
        }
    }
}

From source file:com.sql.Audit.java

/**
 * Removes old audits based on specific time frame.
 *//* w  w  w.  ja v a2 s  .c o  m*/
public static void removeOldAudits() {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = DBConnection.connectToDB();
        String sql = "DELETE FROM Audit WHERE " + "date < dateadd(" + Global.getAuditTimeFrame() + ", -"
                + Global.getAuditTimeAmount() + ", getdate())";
        ps = conn.prepareStatement(sql);
        ps.executeUpdate();
    } catch (SQLException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
    }
}

From source file:com.silverpeas.notation.model.RatingDAO.java

public static long deleteComponentRatings(Connection con, String componentInstanceId) throws SQLException {
    PreparedStatement prepStmt = con.prepareStatement(QUERY_DELETE_COMPONENT_RATINGS);
    try {/*from   www .j a  v a 2  s  . co m*/
        prepStmt.setString(1, componentInstanceId);
        return prepStmt.executeUpdate();
    } finally {
        DBUtil.close(prepStmt);
    }
}

From source file:io.hops.metadata.ndb.mysqlserver.MysqlServerConnector.java

public static void truncateTable(boolean transactional, String tableName, int limit)
        throws StorageException, SQLException {
    MysqlServerConnector connector = MysqlServerConnector.getInstance();
    try {//from   w  w w .  j  av a  2s  . c o m
        Connection conn = connector.obtainSession();
        if (transactional) {
            if (limit > 0) {
                PreparedStatement s = conn.prepareStatement("delete from " + tableName + " limit " + limit);
                s.executeUpdate();
            } else {
                int nbrows = 0;
                do {
                    PreparedStatement s = conn.prepareStatement("delete from " + tableName + " limit 1000");
                    nbrows = s.executeUpdate();
                } while (nbrows > 0);
            }
        } else {
            PreparedStatement s = conn.prepareStatement("truncate table " + tableName);
            s.executeUpdate();
        }
    } finally {
        connector.closeSession();
    }
}

From source file:com.sql.Activity.java

/**
 * Updates activity set to no longer awaiting timestamp for items that have 
 * been properly stamped/*from  w  w w .j av  a2 s  .c  om*/
 * 
 * @param id Integer
 */
public static void markEntryStamped(int id) {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = DBConnection.connectToDB();
        String sql = "UPDATE Activity SET awaitingTimestamp = 0 WHERE id = ?";
        ps = conn.prepareStatement(sql);
        ps.setInt(1, id);
        ps.executeUpdate();
    } catch (SQLException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
    }
}

From source file:com.example.mydtapp.JdbcInputAppTest.java

public static void insertEventsInTable(int numEvents, int offset) {
    try {//w  ww. j  a v  a  2 s  .  c  om
        Connection con = DriverManager.getConnection(URL);
        String insert = "insert into " + TABLE_NAME + " values (?,?,?)";
        PreparedStatement stmt = con.prepareStatement(insert);
        for (int i = 0; i < numEvents; i++, offset++) {
            stmt.setInt(1, offset);
            stmt.setString(2, "Account_Holder-" + offset);
            stmt.setInt(3, (offset * 1000));
            stmt.executeUpdate();
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}