Java SQL Type getSqlType(Object param)

Here you can find the source of getSqlType(Object param)

Description

Returns SQL type matches the object.

License

Apache License

Declaration

public static int getSqlType(Object param) 

Method Source Code

//package com.java2s;
/**//from   w ww.j  a va 2 s  .c o m
 * Copyright 2016 the Rex-Soft Group.
 *
 * 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.math.BigDecimal;
import java.sql.Blob;
import java.sql.Clob;

import java.sql.Types;

import java.util.Date;

public class Main {
    /**
     * Returns SQL type matches the object.
     * 
     * 1. String - Types.VARCHAR
     * 2. int|Integer - Types.INTEGER
     * 3. BigDecimal - Types.NUMERIC
     * 4. long|Long - Types.BIGINT
     * 5. float|Float - Types.FLOAT
     * 6. double|Double - Types.DOUBLE
     * 7. Date - Types.DATE
     * 8. Time - Types.TIME
     * 9. etc
     */
    public static int getSqlType(Object param) {
        int type;
        if (param == null) {
            type = Types.NULL;
        } else {
            if (param instanceof String)
                type = Types.VARCHAR;
            else if (param instanceof Integer)
                type = Types.INTEGER;
            else if (param instanceof Long)
                type = Types.BIGINT;
            else if (param instanceof Float)
                type = Types.FLOAT;
            else if (param instanceof Double)
                type = Types.DOUBLE;
            else if (param instanceof Short)
                type = Types.SMALLINT;
            else if (param instanceof BigDecimal)
                type = Types.NUMERIC;
            else if (param instanceof Date)
                type = Types.TIMESTAMP;
            else if (param.getClass().isArray() && param.getClass().getComponentType() == Byte.class)
                type = Types.VARBINARY;
            else if (param instanceof Blob)
                type = Types.BLOB;
            else if (param instanceof Clob)
                type = Types.CLOB;
            else
                type = Types.OTHER;
        }

        return type;
    }
}

Related

  1. getSimpleTypeString(String dbType)
  2. getSQLAttributeType(int sqlType)
  3. getSQLException(String message, String sqlState, Exception exception)
  4. getSQLParameterSubstring(String value, int type)
  5. getSqlType(int type)
  6. getSqlType(String cubridType)
  7. getSQLType(String text)
  8. getSQLType(String type)
  9. getSqlTypeAsString(int jdbcType)