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

PreparedStatementgetPreparedStatement(Connection conn, String sql, List sqlParams, boolean getGeneratedKeys)
get Prepared Statement
PreparedStatement pstmt = getGeneratedKeys ? conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)
        : conn.prepareStatement(sql);
int pos = 1;
for (Object param : sqlParams) {
    if (param instanceof Integer) {
        pstmt.setInt(pos++, ((Integer) param).intValue());
    } else {
        pstmt.setString(pos++, (String) param);
...
PreparedStatementgetPreparedStatement(String sql)
Get prepared statement
try {
    Connection conn = getConnection();
    PreparedStatement pstmt = conn.prepareStatement(sql);
    statConns.put(pstmt, conn);
    return pstmt;
} catch (Exception e) {
    return null;
intgetPriorID(Connection conn, PreparedStatement stmt)
get Prior ID
int back = -1;
if (uri.startsWith("jdbc:hsqldb")) {
    PreparedStatement ident = conn.prepareStatement("CALL IDENTITY()");
    ResultSet res = ident.executeQuery();
    if (res.next()) {
        back = res.getInt(1);
    res.close();
...
intgetScalarValue(PreparedStatement ps)

Get a scalar value from a PreparedStatement.

ResultSet rs = ps.executeQuery();
rs.next();
try {
    return rs.getInt(1);
} finally {
    close(rs);
    close(ps);
voidimportCsvDataInTable(int offsetIndex, Connection connection, PreparedStatement preparedStatement, String[] columnMapping, List> data, Map> notInsertedData)
import Csv Data In Table
int csvLineIndex = offsetIndex;
for (List<String> csvLine : data) {
    csvLineIndex++;
    if (csvLine.size() != columnMapping.length) {
        notInsertedData.put(csvLineIndex, csvLine);
    } else {
        int parameterIndex = 1;
        for (int csvValueIndex = 0; csvValueIndex < csvLine.size(); csvValueIndex++) {
...
ArrayintToArray(final int[] arr)

Converts an array of integers to an Array object that can be used in a PreparedStatement for Postgresql to set an int4[] column.

return intToArray(arr, "int4");
booleanisClosed(PreparedStatement ps, boolean defaultValue)
is Closed
try {
    return ps.isClosed();
} catch (Throwable t) {
    return defaultValue;
ListloadColumnTableNameList(PreparedStatement stmt)
Return table name list which is related on columns.
List<String> tableNames = new ArrayList<String>();
try {
    ResultSetMetaData rsMetaData = stmt.getMetaData();
    if (rsMetaData != null) {
        for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
            tableNames.add(rsMetaData.getTableName(i));
} catch (Exception ignored) {
return tableNames;
voidLockRow(PreparedStatement pstmt, String tablename, boolean exclusiveMode)
Lock Row
pstmt.executeUpdate(getLockStatement(tablename, exclusiveMode));
voidlogPreparedStatement(PreparedStatement p)
log Prepared Statement
System.out.println("[QUERY] : " + p.toString().split(": ")[1]);