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

PreparedStatementfillStatement(PreparedStatement ps, Object... params)
Fill a raw PreparedStatement to make it executable.
try {
    for (int i = 0; i < params.length; i++) {
        ps.setObject(i + 1, params[i]);
} catch (SQLException e) {
    e.printStackTrace();
return ps;
...
voidfillStatement(PreparedStatement stmt, Object[] params)
Fill the PreparedStatement replacement parameters with the given objects.
if (params == null) {
    return;
for (int i = 0; i < params.length; i++) {
    if (params[i] != null) {
        stmt.setObject(i + 1, params[i]);
    } else {
        stmt.setNull(i + 1, Types.VARCHAR);
...
StringgetFileContent(final File file)
Fully reads the given file content and returns it (to be used be small files only).
FileInputStream fis = null;
String content = null;
try {
    fis = new FileInputStream(file);
    final byte arrBytes[] = new byte[fis.available()];
    fis.read(arrBytes);
    content = new String(arrBytes);
} finally {
...
ObjectgetGeneratedKey(PreparedStatement ps)
get Generated Key
Object key = null;
ResultSet rs = null;
try {
    rs = ps.getGeneratedKeys();
    if (rs.next()) {
        key = rs.getObject(1);
} catch (SQLException e) {
...
intgetGenerateKey(PreparedStatement stmt)
get Generate Key
int key = 0;
ResultSet rs = stmt.getGeneratedKeys();
if (rs.next()) {
    key = rs.getInt(1);
return key;
longgetIdentity(PreparedStatement stat)
Returns the value for IDENTITY column returned by the last execution of the given statement.
ResultSet rs = stat.getGeneratedKeys();
rs.next();
return rs.getBigDecimal(1).longValue();
IntegergetLastId(PreparedStatement s)
get Last Id
if (s == null)
    return null;
ResultSet rs = s.getGeneratedKeys();
if (!rs.next())
    return null;
return rs.getInt(1);
PreparedStatementgetLimitedBatchSizePreparedStatement(PreparedStatement pstmt, int maxBatchSize)
This method wraps the PreparedStatement given as argument into a delegate object that calls the executeBatch method every maxBatchSizeth time a batch is added to the statement with the addBatch method.
return null; 
PreparedStatementgetNewPreparedStatement(String format)
get New Prepared Statement
try {
    if (connection == null)
        connection = getNewConnection();
    PreparedStatement s = connection.prepareStatement(format);
    return s;
} catch (SQLException e) {
return null;
...
PreparedStatementgetPreparedStatement(Connection conn, String sql)
get Prepared Statement
try {
    return conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
} catch (SQLException e) {
    e.printStackTrace();
return null;