Java SQL ResultSet Read getValueFromResultSet(int index, final ResultSet resultSet, final Class type)

Here you can find the source of getValueFromResultSet(int index, final ResultSet resultSet, final Class type)

Description

get Value From Result Set

License

Apache License

Declaration

@SuppressWarnings("unchecked")
    public static <T> T getValueFromResultSet(int index,
            final ResultSet resultSet, final Class<T> type) 

Method Source Code

//package com.java2s;
/**//from w w w .ja  va2s.  co  m
 * Copyright 2012-2015 Niall Gallagher
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.math.BigDecimal;
import java.sql.*;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T getValueFromResultSet(int index,
            final ResultSet resultSet, final Class<T> type) {

        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)) {
                final long time = resultSet.getLong(index);
                return (T) new java.sql.Timestamp(time);

            } else if (Date.class.isAssignableFrom(type)) {
                final long time = resultSet.getLong(index);
                return (T) new Date(time);

            } else if (Long.class.isAssignableFrom(type)) {
                return (T) Long.valueOf(resultSet.getLong(index));

            } else if (Integer.class.isAssignableFrom(type)) {
                return (T) Integer.valueOf(resultSet.getInt(index));

            } else if (Short.class.isAssignableFrom(type)) {
                return (T) Short.valueOf(resultSet.getShort(index));

            } else if (Float.class.isAssignableFrom(type)) {
                return (T) Float.valueOf(resultSet.getFloat(index));

            } else if (Double.class.isAssignableFrom(type)) {
                return (T) Double.valueOf(resultSet.getDouble(index));

            } else if (Boolean.class.isAssignableFrom(type)) {
                return (T) Boolean.valueOf(resultSet.getBoolean(index));

            } else if (BigDecimal.class.isAssignableFrom(type)) {
                return (T) resultSet.getBigDecimal(index);

            } else if (CharSequence.class.isAssignableFrom(type)) {
                return (T) resultSet.getString(index);

            } else if (byte[].class.isAssignableFrom(type)) {
                return (T) resultSet.getBytes(index);

            } else {
                throw new IllegalStateException("Type " + type
                        + " not supported.");
            }
        } catch (Exception e) {
            throw new IllegalStateException(
                    "Unable to read the value from the resultSet. Index:"
                            + index + ", type: " + type, e);
        }
    }
}

Related

  1. getValue(ResultSet result, String strField)
  2. getValue(ResultSet resultSet, String columnName, E defaultValue)
  3. getValue(ResultSet rs, String sField)
  4. getValue4Type(ResultSet rs, String column, Class typeClass)
  5. getValueByObjectType(ResultSetMetaData metaData, ResultSet rs, int index)
  6. readBitBoolean(ResultSet result, int index)
  7. readBytes(ResultSet rs, int pos)
  8. readDate(ResultSet result, int index)
  9. readMap(final ResultSet rs)