Java SQL ResultSet Read getValue4Type(ResultSet rs, String column, Class typeClass)

Here you can find the source of getValue4Type(ResultSet rs, String column, Class typeClass)

Description

get Value Type

License

Apache License

Declaration

public static Object getValue4Type(ResultSet rs, String column, Class<?> typeClass) throws SQLException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Date;

public class Main {

    public static Object getValue4Type(ResultSet rs, String column, Class<?> typeClass) throws SQLException {

        if (Collection.class.isAssignableFrom(typeClass)) {
            return null;
        }//from ww  w  .j  a v a 2  s.co m

        try {
            rs.findColumn(column);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        if (typeClass.equals(Integer.class) || typeClass.equals(Integer.TYPE)) {
            return rs.getInt(column);
        }

        if (typeClass.equals(Long.class) || typeClass.equals(Long.TYPE)) {
            return rs.getLong(column);
        }

        if (typeClass.equals(Boolean.class) || typeClass.equals(Boolean.TYPE)) {
            return rs.getBoolean(column);
        }

        if (typeClass.equals(Float.class) || typeClass.equals(Float.TYPE)) {
            return rs.getFloat(column);
        }

        if (typeClass.equals(Double.class) || typeClass.equals(Double.TYPE)) {
            return rs.getDouble(column);
        }

        if (typeClass.equals(Byte.class) || typeClass.equals(Byte.TYPE)) {
            return rs.getByte(column);
        }

        if (typeClass.equals(String.class)) {
            return rs.getString(column);
        }

        if (Date.class.isAssignableFrom(typeClass)) {
            return rs.getTimestamp(column);
        }

        if (java.sql.Date.class.isAssignableFrom(typeClass)) {
            return rs.getDate(column);
        }

        return rs.getObject(column);
    }
}

Related

  1. getUUIDFromResultSet(ResultSet rset, String name)
  2. getValue(int type, ResultSet resultSet, int columnIndex)
  3. getValue(ResultSet result, String strField)
  4. getValue(ResultSet resultSet, String columnName, E defaultValue)
  5. getValue(ResultSet rs, String sField)
  6. getValueByObjectType(ResultSetMetaData metaData, ResultSet rs, int index)
  7. getValueFromResultSet(int index, final ResultSet resultSet, final Class type)
  8. readBitBoolean(ResultSet result, int index)
  9. readBytes(ResultSet rs, int pos)