Java SQL Type sqlTypeToClassType(final Integer sqlType)

Here you can find the source of sqlTypeToClassType(final Integer sqlType)

Description

sql Type To Class Type

License

LGPL

Declaration

public static Class sqlTypeToClassType(final Integer sqlType) throws SQLException 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;

import java.util.*;

public class Main {
    public static Class sqlTypeToClassType(final Integer sqlType) throws SQLException {
        if (sqlType.equals(Types.ARRAY)) {
            return Array.class;
        } else if (sqlType.equals(Types.BIGINT)) {
            return BigInteger.class;
        } else if (sqlType.equals(Types.BINARY)) {
            return Object.class;
        } else if (sqlType.equals(Types.BIT)) {
            return Byte.class;
        } else if (sqlType.equals(Types.BLOB)) {
            return Object.class;
        } else if (sqlType.equals(Types.BOOLEAN)) {
            return Boolean.class;
        } else if (sqlType.equals(Types.CHAR)) {
            return Byte.class;
        } else if (sqlType.equals(Types.CLOB)) {
            return Object.class;
        } else if (sqlType.equals(Types.DATALINK)) {
            return null;
        } else if (sqlType.equals(Types.DATE)) {
            return Date.class;
        } else if (sqlType.equals(Types.DECIMAL)) {
            return BigDecimal.class;
        } else if (sqlType.equals(Types.DISTINCT)) {
            return null;
        } else if (sqlType.equals(Types.DOUBLE)) {
            return Double.class;
        } else if (sqlType.equals(Types.FLOAT)) {
            return Float.class;
        } else if (sqlType.equals(Types.INTEGER)) {
            return Integer.class;
        } else if (sqlType.equals(Types.JAVA_OBJECT)) {
            return Object.class;
        } else if (sqlType.equals(Types.LONGVARBINARY)) {
            return Object.class;
        } else if (sqlType.equals(Types.LONGVARCHAR)) {
            return String.class;
        } else if (sqlType.equals(Types.NULL)) {
            return null;
        } else if (sqlType.equals(Types.OTHER)) {
            return String.class;
        } else if (sqlType.equals(Types.REAL)) {
            return BigDecimal.class;
        } else if (sqlType.equals(Types.REF)) {
            return null;
        } else if (sqlType.equals(Types.SMALLINT)) {
            return Integer.class;
        } else if (sqlType.equals(Types.STRUCT)) {
            return null;
        } else if (sqlType.equals(Types.TIME)) {
            return Date.class;
        } else if (sqlType.equals(Types.TIMESTAMP)) {
            return Timestamp.class;
        } else if (sqlType.equals(Types.TINYINT)) {
            return Integer.class;
        } else if (sqlType.equals(Types.VARBINARY)) {
            return Object.class;
        } else if (sqlType.equals(Types.VARCHAR)) {
            return String.class;
        } else {/*from w  ww  .ja va  2  s  . c om*/
            throw new SQLException("Unsupported data type: " + sqlType.toString());
        }
    }

    /**
     * Converts objects to String.
     * Null returns null, Arrays and Collections return their first value,
     * or null if they have no values.
     *
     * @param value the object to be turned into a String
     * @return the string value of the object or null if the value is null
     *         or it is an array whose first value is null
     */
    public static String toString(final Object value) {
        if (value instanceof String) {
            return (String) value;
        }
        if (value == null) {
            return null;
        }
        if (value.getClass().isArray()) {
            if (Array.getLength(value) > 0) {
                // recurse on the first value
                return toString(Array.get(value, 0));
            }
            return null;
        }
        return String.valueOf(value);
    }

    /**
     * Returns the first value as a String, if any; otherwise returns null.
     *
     * @param values the Collection to be turned into a string
     * @return the string value of the first object in the collection
     *         or null if the collection is empty
     */
    public static String toString(Collection values) {
        if (values != null && !values.isEmpty()) {
            // recurse on the first value
            return toString(values.iterator().next());
        }
        return null;
    }
}

Related

  1. sqlTypeFromString(String sqlType)
  2. sqlTypeIsNumeric(int sqlType)
  3. sqlTypeName(final int value)
  4. sqlTypeString(int sqlType)
  5. sqlTypeToClass(int sqlType, String className)
  6. sqlTypeToHiveType(Integer type)
  7. sqlTypeToSetterGetter(int sqlType)
  8. sqlTypeToSqlTypeName(final int type)
  9. toClass(final int type)