Source:
private static KeyValPair<String, String> getQueryBounds(long lower, long upper) throws SQLException { Connection dbConnectionLower = null; Connection dbConnectionUpper = null; PreparedStatement psLowerBound = null; PreparedStatement psUpperBound = null; StringBuilder lowerBound = new StringBuilder(); StringBuilder upperBound = new StringBuilder(); KeyValPair<String, String> boundedQUeryPair = null; try { dbConnectionLower = getDBConnection(); dbConnectionUpper = getDBConnection(); lowerBound.append("SELECT " + KEY_COLUMN + " FROM " + TABLE_NAME); upperBound.append("SELECT " + KEY_COLUMN + " FROM " + TABLE_NAME); if (WHERE_CLAUSE != null) { lowerBound.append(" WHERE " + WHERE_CLAUSE); upperBound.append(" WHERE " + WHERE_CLAUSE); } lowerBound.append(" LIMIT " + (0 + lower) + ",1"); upperBound.append(" LIMIT " + (upper - 1) + ",1"); psLowerBound = dbConnectionLower.prepareStatement(lowerBound.toString()); psUpperBound = dbConnectionUpper.prepareStatement(upperBound.toString()); ResultSet rsLower = psLowerBound.executeQuery(); ResultSet rsUpper = psUpperBound.executeQuery(); String lowerVal = null; String upperVal = null; while (rsLower.next()) { lowerVal = rsLower.getString(KEY_COLUMN); } while (rsUpper.next()) { upperVal = rsUpper.getString(KEY_COLUMN); } boundedQUeryPair = new KeyValPair<String, String>(lowerVal, upperVal); } catch (SQLException e) { LOG.error("Exception in getting bounds for queries"); } finally { if (psLowerBound != null) { psLowerBound.close(); } if (dbConnectionLower != null) { dbConnectionLower.close(); } if (psUpperBound != null) { psUpperBound.close(); } if (dbConnectionUpper != null) { dbConnectionUpper.close(); } } return boundedQUeryPair; }

Target:
private static KeyValPair<String, String> getQueryBounds(long lower, long upper) throws SQLException { Connection dbConnection = null; PreparedStatement psLowerBound = null; PreparedStatement psUpperBound = null; StringBuilder lowerBound = new StringBuilder(); StringBuilder upperBound = new StringBuilder(); KeyValPair<String, String> boundedQUeryPair = null; try { dbConnection = getDBConnection(); lowerBound.append("SELECT " + KEY_COLUMN + " FROM " + TABLE_NAME); upperBound.append("SELECT " + KEY_COLUMN + " FROM " + TABLE_NAME); if (WHERE_CLAUSE != null) { lowerBound.append(" WHERE " + WHERE_CLAUSE); upperBound.append(" WHERE " + WHERE_CLAUSE); } lowerBound.append(" LIMIT " + (0 + lower) + ",1"); upperBound.append(" LIMIT " + (upper - 1) + ",1"); psLowerBound = dbConnection.prepareStatement(lowerBound.toString()); psUpperBound = dbConnection.prepareStatement(upperBound.toString()); ResultSet rsLower = psLowerBound.executeQuery(); ResultSet rsUpper = psUpperBound.executeQuery(); String lowerVal = null; String upperVal = null; while (rsLower.next()) { lowerVal = rsLower.getString(KEY_COLUMN); } while (rsUpper.next()) { upperVal = rsUpper.getString(KEY_COLUMN); } boundedQUeryPair = new KeyValPair<String, String>(lowerVal, upperVal); } catch (SQLException e) { LOG.error("Exception in getting bounds for queries"); throw new RuntimeException(e); } finally { if (psLowerBound != null) { psLowerBound.close(); } if (psUpperBound != null) { psUpperBound.close(); } if (dbConnection != null) { dbConnection.close(); } } return boundedQUeryPair; }