Java SQL ResultSet Read getExpectedType(ResultSet resultSet, int columnIndex)

Here you can find the source of getExpectedType(ResultSet resultSet, int columnIndex)

Description

get Expected Type

License

Open Source License

Declaration

private static Class getExpectedType(ResultSet resultSet, int columnIndex) throws SQLException 

Method Source Code

//package com.java2s;
/* Copyright (C) 2000-2003 The Software Conservancy as Trustee.
 * All rights reserved./*from   ww w. ja  v a2 s .com*/
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Nothing in this notice shall be deemed to grant any rights to trademarks,
 * copyrights, patents, trade secrets or any other intellectual property of the
 * licensor or any contributor except as expressly stated herein. No patent
 * license is granted separate from the Software, for code that you delete from
 * the Software, or for combinations of the Software with other software or
 * hardware.
 */

import java.sql.*;

public class Main {
    private static Class getExpectedType(ResultSet resultSet, int columnIndex) throws SQLException {
        Class expectedType;
        ResultSetMetaData metaData = resultSet.getMetaData();
        int sqlType = metaData.getColumnType(columnIndex);
        int decimalDigits = metaData.getScale(columnIndex);
        expectedType = getClassByJdbcType(sqlType, decimalDigits);
        return expectedType;
    }

    public static Class getClassByJdbcType(int type, int decimalDigits) {
        switch (type) {
        case Types.CHAR:
        case Types.VARCHAR:
        case Types.LONGVARCHAR:
            return String.class;

        case Types.BIT:
        case Types.BOOLEAN:
            return Boolean.class;

        case Types.TINYINT:
        case Types.SMALLINT:
        case Types.INTEGER:
            return Integer.class;

        case Types.BIGINT:
            return Long.class;

        case Types.FLOAT:
        case Types.DOUBLE:
            return Double.class;

        case Types.DECIMAL:
        case Types.NUMERIC:
            if (decimalDigits == 0) {
                return Long.class;
            } else {
                return Double.class;
            }

        case Types.DATE:
            return java.sql.Date.class;

        case Types.TIME:
        case Types.TIMESTAMP:
            return java.util.Date.class;

        default:
            return String.class;
        }
    }
}

Related

  1. getEntityMap(ResultSet rs, ResultSetMetaData rsmd)
  2. getEnum(Class enumClass, ResultSet rs, String columnName)
  3. getEnum(ResultSet r, String columnName, Class enumType)
  4. getEnum(ResultSet rs, int index, Class type)
  5. getEnumFromResultSet(ResultSet rset, Enum field, T[] enumValues)
  6. getExplainPlan(ResultSet rs)
  7. getFieldNames(ResultSet rs)
  8. getFieldsPresentInResultSet(ResultSet rs)
  9. getFirstGeometryFieldIndex(ResultSet resultSet)