Java Utililty Methods SQL ResultSet Read

List of utility methods to do SQL ResultSet Read

Description

The list of methods to do SQL ResultSet Read are organized into topic(s).

Method

EgetValue(ResultSet resultSet, String columnName, E defaultValue)
get Value
if (checkColumn(resultSet, columnName)) {
    E value = (E) resultSet.getObject(columnName);
    if (resultSet.wasNull())
        return defaultValue;
    return cast(value, defaultValue);
return defaultValue;
StringgetValue(ResultSet rs, String sField)
get Value
String sRet = rs.getString(sField);
sRet = (sRet == null) ? "" : sRet;
return sRet;
ObjectgetValue4Type(ResultSet rs, String column, Class typeClass)
get Value Type
if (Collection.class.isAssignableFrom(typeClass)) {
    return null;
try {
    rs.findColumn(column);
} catch (Exception e) {
    e.printStackTrace();
    return null;
...
ObjectgetValueByObjectType(ResultSetMetaData metaData, ResultSet rs, int index)
get Value By Object Type
int columnIndex = index + 1;
Object return_obj = rs.getObject(columnIndex);
if (return_obj != null) {
    int type = metaData.getColumnType(columnIndex);
    switch (type) {
    case Types.BIT:
        return_obj = rs.getByte(columnIndex);
        break;
...
TgetValueFromResultSet(int index, final ResultSet resultSet, final Class type)
get Value From Result Set
try {
    if (java.sql.Date.class.isAssignableFrom(type)) {
        final long time = resultSet.getLong(index);
        return (T) new java.sql.Date(time);
    } else if (Time.class.isAssignableFrom(type)) {
        final long time = resultSet.getLong(index);
        return (T) new java.sql.Time(time);
    } else if (Timestamp.class.isAssignableFrom(type)) {
...
BooleanreadBitBoolean(ResultSet result, int index)
Read a Boolean (encoded in a bit or tinyint or string field or whatever) from the results set.
Object o = result.getObject(index);
if (o == null)
    return null;
Boolean rv = null;
if (o instanceof Boolean) {
    rv = (Boolean) o;
} else if (o instanceof Integer) {
    rv = Boolean.valueOf(((Integer) o).intValue() == 1);
...
byte[]readBytes(ResultSet rs, int pos)
read Bytes
InputStream is = rs.getBinaryStream(pos);
if (is == null) {
    return null;
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
byte[] buf = new byte[1024];
while (true) {
    try {
...
DatereadDate(ResultSet result, int index)
Read a long from the result set, and convert to a null (if 0) or a Date.
long time = result.getLong(index);
if (time == 0)
    return null;
return new Date(time);
MapreadMap(final ResultSet rs)
read Map
final Map<String, Object> values = new LinkedHashMap<>();
final ResultSetMetaData metaData = rs.getMetaData();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
    final String name = metaData.getColumnName(i);
    final Object value = rs.getObject(i);
    values.put(name, value);
return values;
...
ObjectreadObject(java.sql.ResultSet resultSet, int index)
Given a ResultSet and an index into the columns of that ResultSet, read binary data from the column which represents a serialized object, and re-create the object.
ObjectInputStream objIn = new ObjectInputStream(resultSet.getBinaryStream(index));
Object obj = objIn.readObject();
objIn.close();
return obj;