Java Utililty Methods SQL PreparedStatement

List of utility methods to do SQL PreparedStatement

Description

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

Method

voidcloseStatement(PreparedStatement ps)
Utility method to close statement properly.
try {
    if (ps != null) {
        ps.close();
} catch (SQLException e) {
    throw new IllegalArgumentException("An error occur when closing statement", e);
voidcloseStatement(PreparedStatement theStatement)
close Statement
if (theStatement != null) {
    theStatement.close();
PreparedStatementcreateInsertPrepareStatement(Connection conn, String tableName, String[] dataColumnNames)
create Insert Prepare Statement
PreparedStatement preparedStatement = null;
try {
    StringBuilder insertQueryBuf = new StringBuilder();
    insertQueryBuf.append(insertSqlTpl[0]);
    insertQueryBuf.append(tableName);
    insertQueryBuf.append("(PKEYIDX");
    for (int idx = 0; idx < dataColumnNames.length; idx++) {
        insertQueryBuf.append(",");
...
PreparedStatementcreatePreparedStatement(Connection conn, String sql, String[] args)
create Prepared Statement
PreparedStatement stmt = conn.prepareStatement(sql);
int n = args.length;
for (int i = 0; i < n; i++)
    stmt.setString(i + 1, args[i]);
return stmt;
voidcreateProjectInfo(PreparedStatement ps, long projectId, long projectInfoTypeId, String value, long modUserId)
Create project info.
int index = 1;
ps.setLong(index++, projectId);
ps.setLong(index++, projectInfoTypeId);
ps.setString(index++, value);
ps.setString(index++, String.valueOf(modUserId));
ps.setString(index++, String.valueOf(modUserId));
ps.executeUpdate();
voiddeleteOldData(Connection connection, PreparedStatement stmt, String funcId)
delete Old Data
stmt = connection.prepareStatement(DELETE_PARAM_SQL);
stmt.setString(1, funcId);
stmt.executeUpdate();
stmt = connection.prepareStatement(DELETE_PARAM_CRITERIA);
stmt.setString(1, funcId);
stmt.executeUpdate();
stmt = connection.prepareStatement(DELETE_PARAM);
stmt.setString(1, funcId);
...
voidfillArgs(Object[][] args, PreparedStatement pstmt, int i)
fill Args
if (args == null || args.length == 0)
    return;
if (args[i] == null || args[i].length == 0)
    return;
for (int j = 0; j < args[i].length; ++j)
    pstmt.setObject(j + 1, args[i][j]);
voidfillInClause(PreparedStatement ps, int startIndex, Collection clauses, int sqlType)
fill In Clause
fillInClause(ps, startIndex, clauses.size(), clauses.iterator(), sqlType);
voidfillParameters(PreparedStatement stmt, List params)
Fills a given prepared statement with parameters from a list of objects.

int i = 1;
for (Object param : params) {
    if (param instanceof String) {
        stmt.setString(i, (String) param);
    } else if (param instanceof Integer) {
        stmt.setInt(i, ((Integer) param).intValue());
    } else if (param instanceof Long) {
        stmt.setLong(i, ((Long) param).longValue());
...
voidfillPreparedStatementParams(PreparedStatement ps, Object... obj)
Function is the same as it's name.
for (int i = 0; i < obj.length; i++) {
    Object target = obj[i];
    if (target == null) {
        ps.setNull(i + 1, Types.JAVA_OBJECT);
    } else {
        ps.setObject(i + 1, target.getClass() == Character.class ? String.valueOf(target) : target);