Java SQL Table Column getColumnType(ResultSet resultSet, int i, Map typeCache)

Here you can find the source of getColumnType(ResultSet resultSet, int i, Map typeCache)

Description

Gets type of column from result-set.

License

Apache License

Parameter

Parameter Description
resultSet result-set
i column index
typeCache for caching types

Return

type according to

Declaration

public static int getColumnType(ResultSet resultSet, int i, Map<Integer, Integer> typeCache)
        throws SQLException 

Method Source Code


//package com.java2s;
/*//w  w w.j a va 2 s. com
 * Copyright 2007 - 2012 the original author or authors.
 *
 * 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.sql.ResultSet;
import java.sql.SQLException;

import java.sql.Types;

import java.util.Map;

public class Main {
    /**
     * Gets type of column from result-set.
     * 
     * @param resultSet result-set
     * @param i column index
     * @param typeCache for caching types
     * @return type according to {@link Types}
     */
    public static int getColumnType(ResultSet resultSet, int i, Map<Integer, Integer> typeCache)
            throws SQLException {
        Integer type = typeCache.get(i);
        if (type == null) {
            try {
                type = resultSet.getMetaData().getColumnType(i);
            } catch (Exception e) {
                type = Types.OTHER;
            }
            typeCache.put(i, type);
        }
        return type;
    }

    /**
     * Gets type of column from result-set.
     * 
     * @param resultSet result-set
     * @param columnName column name
     * @param typeCache for caching types
     * @return object
     */
    public static int getColumnType(ResultSet resultSet, String columnName, Map<String, Integer> typeCache)
            throws SQLException {
        Integer type = typeCache.get(columnName);
        if (type == null) {
            try {
                type = Types.OTHER;
                for (int i = resultSet.getMetaData().getColumnCount(); i > 0; --i) {
                    if (columnName.equalsIgnoreCase(resultSet.getMetaData().getColumnName(i))) {
                        type = resultSet.getMetaData().getColumnType(i);
                        break;
                    }
                }
            } catch (Exception e) {
            }
            typeCache.put(columnName, type);
        }
        return type;
    }
}

Related

  1. getColumnNames(ResultSet rs)
  2. getColumnNamesFromResultSet(ResultSet rs)
  3. getColumns(ResultSetMetaData rsmd)
  4. getColumns(ResultSetMetaData rsMetaData)
  5. getColumnsNames(ResultSet rs)
  6. getColumnType(ResultSetMetaData rsm)
  7. getColumnType(ResultSetMetaData rsmd, int idx)
  8. getColumnTypes(ResultSet rs)
  9. getColumnValue(ResultSet rs, ResultSetMetaData rsmd, int colIndex)