Java SQL Table Column getColumnType(ResultSetMetaData rsmd, int idx)

Here you can find the source of getColumnType(ResultSetMetaData rsmd, int idx)

Description

Get the type of an external database's column as a Derby type name.

License

Apache License

Declaration

private static String getColumnType(ResultSetMetaData rsmd, int idx) throws SQLException 

Method Source Code

//package com.java2s;
/*//from   w w  w .j a v  a  2s .c  o  m
    
Derby - Class org.apache.derbyDemo.vtis.core.QueryVTIHelper
    
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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.*;

public class Main {
    /**
     * <p>
     * Get the type of an external database's column as a Derby type name.
     * </p>
     *
     */
    private static String getColumnType(ResultSetMetaData rsmd, int idx) throws SQLException {
        int jdbcType = rsmd.getColumnType(idx);
        int precision = rsmd.getPrecision(idx);
        int scale = rsmd.getScale(idx);

        switch (jdbcType) {
        case Types.BIGINT:
            return "bigint";
        case Types.BINARY:
            return "char " + precisionToLength(precision) + "  for bit data";
        case Types.BIT:
            return "smallint";
        case Types.BLOB:
            return "blob";
        case Types.BOOLEAN:
            return "smallint";
        case Types.CHAR:
            return "char" + precisionToLength(precision);
        case Types.CLOB:
            return "clob";
        case Types.DATE:
            return "date";
        case Types.DECIMAL:
            return "decimal" + precisionAndScale(precision, scale);
        case Types.DOUBLE:
            return "double";
        case Types.FLOAT:
            return "float";
        case Types.INTEGER:
            return "integer";
        case Types.LONGVARBINARY:
            return "long varchar for bit data";
        case Types.LONGVARCHAR:
            return "long varchar";
        case Types.NUMERIC:
            return "numeric" + precisionAndScale(precision, scale);
        case Types.REAL:
            return "real";
        case Types.SMALLINT:
            return "smallint";
        case Types.TIME:
            return "time";
        case Types.TIMESTAMP:
            return "timestamp";
        case Types.TINYINT:
            return "smallint";
        case Types.VARBINARY:
            return "varchar " + precisionToLength(precision) + "  for bit data";
        case Types.VARCHAR:
            return "varchar" + precisionToLength(precision);

        default:
            throw new SQLException("Unknown external data type. JDBC type = " + jdbcType + ", external type name = "
                    + rsmd.getColumnTypeName(idx));
        }
    }

    /**
     * <p>
     * Turns precision into a length designator.
     * </p>
     *
     */
    private static String precisionToLength(int precision) {
        return "( " + precision + " )";
    }

    /**
     * <p>
     * Build a precision and scale designator.
     * </p>
     *
     */
    private static String precisionAndScale(int precision, int scale) {
        return "( " + precision + ", " + scale + " )";
    }
}

Related

  1. getColumns(ResultSetMetaData rsmd)
  2. getColumns(ResultSetMetaData rsMetaData)
  3. getColumnsNames(ResultSet rs)
  4. getColumnType(ResultSet resultSet, int i, Map typeCache)
  5. getColumnType(ResultSetMetaData rsm)
  6. getColumnTypes(ResultSet rs)
  7. getColumnValue(ResultSet rs, ResultSetMetaData rsmd, int colIndex)
  8. getColumnValue(String column, String defaultValue, Map columnIndexMap, ResultSet res)
  9. getColumnValueFromResultSet(int columnIndex, int argType, ResultSet rs)