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(Connection conn, String query, List params)
prepare Statement
PreparedStatement cstmt = conn.prepareStatement(query);
constrainQueryParams(conn, params);
setPreparedStatementParams(cstmt, params);
return cstmt;
PreparedStatementprepareStatement(Connection con, String sql, Object... params)
prepare Statement
PreparedStatement ps = null;
try {
    ps = con.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
    for (int i = 0; i < params.length; i++) {
        ps.setObject(i + 1, params[i]);
} catch (SQLException e) {
    e.printStackTrace();
...
PreparedStatementprepareStatement(Connection conn, Map psMap, String key, String sql)
prepare Statement
Object[] obj = (Object[]) psMap.get(key);
if (obj == null) {
    obj = new Object[] { null, null };
    psMap.put(key, obj);
String lastSql = (String) obj[0];
PreparedStatement ps = (PreparedStatement) obj[1];
if (!sql.equals(lastSql)) {
...
PreparedStatementprepareStatement(Connection conn, String name, String sql)
prepare statements for this connection.
PreparedStatement ps = null;
try {
    ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
} finally {
    if (ps == null) {
        System.err.println("Warning: couldn't initialize " + name + " from " + sql);
return ps;
PreparedStatementprepareStatement(Connection conn, String sql)
prepare Statement
if (uri.startsWith("jdbc:hsqldb") || uri.startsWith("jdbc:postgres")) {
    return conn.prepareStatement(sql);
return conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
PreparedStatementprepareStatement(Connection conn, String sql, boolean isCallable)
prepare Statement
if (!isCallable)
    return conn.prepareStatement(sql);
else
    return conn.prepareCall(sql);
PreparedStatementprepareStatement(Connection conn, String sql, Object... values)
prepare Statement
PreparedStatement stmt = conn.prepareStatement(sql);
setPreparedStatementValues(stmt, values);
return stmt;
PreparedStatementprepareStatement(Connection connection, String line)
prepare Statement
try {
    return connection.prepareStatement(line);
} catch (SQLException e) {
    return null;
PreparedStatementprepareStatement(Connection connection, String sql, boolean returnKeys, Object... values)
prepare Statement
PreparedStatement statement = connection.prepareStatement(sql,
        returnKeys ? Statement.RETURN_GENERATED_KEYS : Statement.NO_GENERATED_KEYS);
setValues(statement, values);
return statement;
PreparedStatementprepareStatement(Connection connection, String sql, Object... values)
Returns a PreparedStatement of the given connection, set with the given SQL query and the given parameter values.
PreparedStatement statement = connection.prepareStatement(sql);
setValues(statement, values);
return statement;