Java SQL Type getClass(int sqlType, int precision, int scale)

Here you can find the source of getClass(int sqlType, int precision, int scale)

Description

get Class

License

Open Source License

Declaration

public static Class<?> getClass(int sqlType, int precision, int scale) 

Method Source Code

//package com.java2s;
/**/*from   w w w .  j av a 2s  .co  m*/
 * Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com
 * All rights reserved.
 *
 * This software is licensed to you under the Apache License, Version 2.0
 * (the "License"); You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * . Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 * . Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * . Neither the name "jOOQ" nor the names of its contributors may be
 *   used to endorse or promote products derived from this software without
 *   specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

import java.math.BigDecimal;
import java.math.BigInteger;

import java.sql.Date;

import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;

public class Main {
    private static final int LONG_PRECISION = String.valueOf(Long.MAX_VALUE).length();
    private static final int INTEGER_PRECISION = String.valueOf(Integer.MAX_VALUE).length();
    private static final int SHORT_PRECISION = String.valueOf(Short.MAX_VALUE).length();
    private static final int BYTE_PRECISION = String.valueOf(Byte.MAX_VALUE).length();

    public static Class<?> getClass(int sqlType, int precision, int scale) {
        switch (sqlType) {
        case Types.BLOB:
        case Types.BINARY:
        case Types.LONGVARBINARY:
        case Types.VARBINARY:
            return byte[].class;

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

        case Types.TINYINT:
            return Byte.class;

        case Types.SMALLINT:
            return Short.class;

        case Types.INTEGER:
            return Integer.class;

        case Types.BIGINT:
            return Long.class;

        case Types.REAL:
            return Float.class;

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

        case Types.DECIMAL:
        case Types.NUMERIC: {

            // Integer numbers
            if (scale == 0 && precision != 0) {
                if (precision < BYTE_PRECISION) {
                    return Byte.class;
                }
                if (precision < SHORT_PRECISION) {
                    return Short.class;
                }
                if (precision < INTEGER_PRECISION) {
                    return Integer.class;
                }
                if (precision < LONG_PRECISION) {
                    return Long.class;
                }

                // Default integer number
                return BigInteger.class;
            }

            // Real numbers should not be represented as float or double
            else {
                return BigDecimal.class;
            }
        }

        case Types.CLOB:
        case Types.CHAR:
        case Types.LONGNVARCHAR:
        case Types.LONGVARCHAR:
        case Types.NCHAR:
        case Types.NCLOB:
        case Types.NVARCHAR:
        case Types.VARCHAR:
            return String.class;

        case Types.DATE:
            return Date.class;

        case Types.TIME:
            return Time.class;

        case Types.TIMESTAMP:
            return Timestamp.class;

        default:
            return Object.class;
        }
    }
}

Related

  1. getBinaryStream(Object value, int columnType)
  2. getBIRTDataType(Integer datatype)
  3. getCallableStatementValue(CallableStatement cstm, int index)
  4. getCastExpression(Class aClass)
  5. getCharStream(Object value, int columnType)
  6. getClassByJdbcType(int type, int decimalDigits)
  7. getCloverTypeFromJdbcType(int jdbcDataType)
  8. getColumeTypeDesc(Class clazz)
  9. getColumnClass(int sqlType)