Java SQL ResultSet Read getValue(ResultSet resultSet, String columnName, E defaultValue)

Here you can find the source of getValue(ResultSet resultSet, String columnName, E defaultValue)

Description

get Value

License

Apache License

Parameter

Parameter Description
E a parameter
resultSet ResultSet
columnName String
defaultValue E

Exception

Parameter Description
SQLException an exception

Return

E

Declaration

public static <E> E getValue(ResultSet resultSet, String columnName, E defaultValue) throws SQLException 

Method Source Code


//package com.java2s;
/*//w  ww  .j  av  a  2 s  . com
 * SpiderDB - Lightweight Database Schema Crawler
 * Copyright (c) 2010, Avdhesh yadav.
 * Contact: avdhesh.yadav@gmail.com
 * 
 * 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.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class Main {
    /**
     * 
     * @param <E>
     * @param resultSet ResultSet
     * @param columnName String
     * @param defaultValue  E
     * 
     * @return E
     * 
     * @throws SQLException
     */
    public static <E> E getValue(ResultSet resultSet, String columnName, E defaultValue) throws SQLException {
        if (checkColumn(resultSet, columnName)) {
            E value = (E) resultSet.getObject(columnName);

            if (resultSet.wasNull())
                return defaultValue;

            return cast(value, defaultValue);
        }
        return defaultValue;
    }

    /**
     * 
     * @param resultSet ResultSet
     * @param columnName String
     * 
     * @return boolean
     * 
     * @throws SQLException
     */
    public static boolean checkColumn(ResultSet resultSet, String columnName) throws SQLException {
        ResultSetMetaData rsMetaData = resultSet.getMetaData();
        for (int i = 0; i < rsMetaData.getColumnCount(); i++) {
            String col = rsMetaData.getColumnLabel(i + 1);
            if (col != null && col.equalsIgnoreCase(columnName))
                return true;
        }
        return false;
    }

    /**
     * 
     * @param <E>
     * @param value E
     * @param defaultValue E 
     * 
     * @return E
     */
    public static <E> E cast(E value, E targetType) {
        try {
            if (!Class.forName(value.getClass().getName()).isInstance(targetType)) {
                if (value instanceof Long && targetType instanceof Integer)
                    return (E) new Integer(Integer.parseInt(Long.toString((Long) value)));
                else if (value instanceof String && targetType instanceof Integer)
                    return (E) new Integer(Integer.parseInt(value.toString()));
                else if (value instanceof String && targetType instanceof Short)
                    return (E) new Short(Short.parseShort((value.toString())));
                else if (value instanceof Integer && targetType instanceof Long)
                    return (E) new Long((Integer) value);
                else if ((value instanceof Long || value instanceof Integer || value instanceof Short)
                        && targetType instanceof String)
                    return (E) String.valueOf(value);
                else if ((value instanceof BigDecimal) && targetType instanceof Integer)
                    return (E) (new Integer(((BigDecimal) value).intValue()));
            }
        } catch (ClassNotFoundException e) {
        }

        return value;
    }
}

Related

  1. getURI(ResultSet resultSet, String columnLabel)
  2. getURI(ResultSet rs, int col)
  3. getUUIDFromResultSet(ResultSet rset, String name)
  4. getValue(int type, ResultSet resultSet, int columnIndex)
  5. getValue(ResultSet result, String strField)
  6. getValue(ResultSet rs, String sField)
  7. getValue4Type(ResultSet rs, String column, Class typeClass)
  8. getValueByObjectType(ResultSetMetaData metaData, ResultSet rs, int index)
  9. getValueFromResultSet(int index, final ResultSet resultSet, final Class type)