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

PreparedStatementprepareStatement(String parameterizedSQL, List values, Connection conn)
prepare Statement
PreparedStatement pstmt = conn.prepareStatement(parameterizedSQL);
populateStatement(pstmt, values);
return pstmt;
PreparedStatementprepareStatement(String parameterizedSQL, List values, Connection conn)
prepare Statement
PreparedStatement pstmt = conn.prepareStatement(parameterizedSQL);
for (int i = 0; values != null && i < values.size(); i++) {
    try {
        String val = (String) values.get(i);
        if (val != null && val.equals("NULL"))
            pstmt.setNull(i + 1, Types.NULL);
        else
            pstmt.setObject(i + 1, values.get(i));
...
PreparedStatementprepareStatement(String query)
Prepares an SQL query statement to the database using the active database connection.
return connection.prepareStatement(query);
PreparedStatementprepareStatementForwardReadOnly(Connection conn, String name, String sql)
prepare statements for this connection
PreparedStatement ps = null;
try {
    ps = prepareForwardReadOnly(conn, sql);
} finally {
    if (ps == null) {
        System.err.println("Warning: couldn't initialize " + name + " from " + sql);
return ps;
voidputArgsToStatement(PreparedStatement stmt, List args)
put Args To Statement
if (args != null) {
    for (int i = 0; i < args.size(); i++) {
        stmt.setObject(i + 1, args.get(i));
ResultSetquery(List paramList, Connection conn, PreparedStatement pstmt)
Don't forget: After execute query, we need to close resultset, prepared statement and connection in persistence layer
ResultSet result = null;
try {
    Iterator<String> paramIter = paramList.iterator();
    int i = 1;
    while (paramIter.hasNext()) {
        String eachParam = paramIter.next();
        pstmt.setString(i, eachParam);
        i++;
...
longrunInsertLong(PreparedStatement s)
runInsertLong is a database insert helper function, it runs an insert and returns a Long value based on the result of the query - Statements must return a value to be used with this function
long rval = -1;
try {
    ResultSet rs = s.executeQuery();
    if (rs.next()) {
        rval = rs.getLong(1);
        System.out.println("SQL Insert rval:  " + rval);
    rs.close();
...
voidsaveResourceInfoRecord(long resourceId, long propertyId, String propertyValue, String userId, PreparedStatement ps)
Private helper method to save a resource info record.
int index = 1;
ps.setLong(index++, resourceId);
ps.setLong(index++, propertyId);
ps.setString(index++, propertyValue);
ps.setString(index++, userId);
ps.setString(index++, userId);
int nr = ps.executeUpdate();
if (nr != 1) {
...
voidsetByte(PreparedStatement statement, int index, Byte value)
Sets a SQL parameter of type byte using a prepared SQL statement, an index and a value.
if (statement != null) {
    if (value == null) {
        statement.setNull(index, Types.TINYINT);
    } else {
        statement.setByte(index, value.byteValue());
voidsetBytes(PreparedStatement pstmt, int index, byte[] bytes)
set Bytes
if (bytes != null) {
    pstmt.setBytes(index, bytes);
} else {
    pstmt.setNull(index, Types.BINARY);