Java Utililty Methods SQL Update

List of utility methods to do SQL Update

Description

The list of methods to do SQL Update are organized into topic(s).

Method

voidexecuteUpdate(final Connection conn, final String query)
execute Update
conn.setAutoCommit(false);
final Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
st.executeUpdate(query);
voidexecuteUpdate(final Connection conn, final String sqlStatmeent)
Executes a jdbc update statement and disposes of the resources
PreparedStatement ps = null;
try {
    ps = conn.prepareStatement(sqlStatmeent);
    ps.executeUpdate();
} finally {
    close(new Object[] { ps });
intexecuteUpdate(List paramList, Connection conn, PreparedStatement pstmt)
Don't forget We need to use this way to close pstmt and connection in persistence layer.
int returnCode = -100;
try {
    Iterator<String> paramIter = paramList.iterator();
    int i = 1;
    while (paramIter.hasNext()) {
        String eachParam = (String) paramIter.next();
        pstmt.setString(i, eachParam);
        i++;
...
booleanexecuteUpdate(PreparedStatement preparedStatement, List paramList)
Generic update function.
try {
    int i = 1;
    for (Object param : paramList) {
        if (param instanceof String) {
            preparedStatement.setString(i, String.valueOf(param));
        i++;
    preparedStatement.executeUpdate();
} catch (SQLException e) {
    e.printStackTrace();
    return false;
return true;
intexecuteUpdate(PreparedStatement ps)
execute Update
assert locked : "Database isn't locked";
int i = ps.executeUpdate();
return i;
intexecuteUpdate(String parameterizedSQL, List values, Connection conn)
execute Update
PreparedStatement pstmt = null;
try {
    pstmt = prepareStatement(parameterizedSQL, values, conn);
    return pstmt.executeUpdate();
} finally {
    try {
        if (pstmt != null)
            pstmt.close();
...
intexecuteUpdate(String queryStatement, Connection database)
execute Update
return database.createStatement().executeUpdate(queryStatement);
intExecuteUpdate(String sql, List paras)
Execute Update
int iRet = 0;
try {
    GetConnection();
    PreparedStatement stm = dbConnection.prepareStatement(sql);
    if (stm != null) {
        for (int i = 0; i < paras.size(); i++) {
            stm.setString(i + 1, paras.get(0));
        iRet = stm.executeUpdate(sql);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    EndConnection();
return iRet;
intexecuteUpdateNoCommit(Connection c, String query)
executeUpdateNoCommit
int rowsUpdated = -1;
Statement st = c.createStatement();
try {
    rowsUpdated = st.executeUpdate(query);
} catch (SQLException ee) {
    throw ee;
} finally {
    try {
...
intexecuteUpdatePreparedStm(Connection conn, String preparedStm, Object... sqlParameters)
execute prepared sql statment
int rs = 0;
PreparedStatement prepStm = null;
if (conn != null) {
    prepStm = conn.prepareStatement(preparedStm);
    if (sqlParameters != null) {
        for (int i = 1; i <= sqlParameters.length; i++) {
            prepStm.setObject(i, sqlParameters[i - 1]);
    rs = prepStm.executeUpdate();
return rs;