Java SQL Type getDbDataType(Object o)

Here you can find the source of getDbDataType(Object o)

Description

get Db Data Type

License

Open Source License

Declaration

public static int getDbDataType(Object o) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.math.*;

import java.sql.*;

public class Main {
    public static int getDbDataType(Object o) {
        int result = java.sql.Types.VARCHAR;

        if (o.getClass().equals(Array.class)) {
            result = java.sql.Types.ARRAY;
        } else if (o.getClass().equals(Long.class)) {
            result = java.sql.Types.BIGINT;
        } else if (o.getClass().equals(Boolean.class)) {
            result = java.sql.Types.BIT;
        } else if (o.getClass().equals(Blob.class)) {
            result = java.sql.Types.BLOB;
        } else if (o.getClass().equals(Clob.class)) {
            result = java.sql.Types.CLOB;
        } else if ((o.getClass().equals(java.sql.Date.class)) || (o.getClass().equals(java.util.Date.class))) {
            result = java.sql.Types.DATE;
        } else if (o.getClass().equals(Double.class)) {
            result = java.sql.Types.DOUBLE;
        } else if (o.getClass().equals(Integer.class)) {
            result = java.sql.Types.INTEGER;
        } else if (o.getClass().equals(Float.class)) {
            result = java.sql.Types.REAL;
        } else if (o.getClass().equals(BigDecimal.class)) {
            result = java.sql.Types.NUMERIC;
        } else if (o.getClass().equals(Ref.class)) {
            result = java.sql.Types.REF;
        } else if (o.getClass().equals(SQLXML.class)) {
            result = java.sql.Types.SQLXML;
        } else if (o.getClass().equals(Short.class)) {
            result = java.sql.Types.SMALLINT;
        } else if (o.getClass().equals(Struct.class)) {
            result = java.sql.Types.STRUCT;
        } else if ((o.getClass().equals(java.sql.Time.class)) || (o.getClass().equals(Time.class))) {
            result = java.sql.Types.TIME;
        } else if ((o.getClass().equals(java.sql.Timestamp.class)) || (o.getClass().equals(Timestamp.class))) {
            result = java.sql.Types.TIMESTAMP;
        } else if (o.getClass().equals(Byte.class)) {
            result = java.sql.Types.TINYINT;
        } else if (o.getClass().equals(Byte[].class)) {
            result = java.sql.Types.VARBINARY;
        } else if (o.getClass().equals(String.class)) {
            result = java.sql.Types.VARCHAR;
        }//w  w  w . j a  va2  s .  c  om

        return result;
    }

    public static int getDbDataType(String value) {
        int result = java.sql.Types.VARCHAR;

        if ((value == null) || (value.length() == 0)) {
            result = java.sql.Types.VARCHAR;
        } else if (value.equals("ARRAY")) {
            result = java.sql.Types.ARRAY;
        } else if ((value.equals("BIGINT")) || (value.equals("LONG"))) {
            result = java.sql.Types.BIGINT;
        } else if ((value.equals("BIT")) || (value.equals("BOOLEAN"))) {
            result = java.sql.Types.BIT;
        } else if (value.equals("BLOB")) {
            result = java.sql.Types.BLOB;
        } else if (value.equals("CLOB")) {
            result = java.sql.Types.CLOB;
        } else if (value.equals("DATE")) {
            result = java.sql.Types.DATE;
        } else if (value.equals("DOUBLE")) {
            result = java.sql.Types.DOUBLE;
        } else if (value.equals("INTEGER")) {
            result = java.sql.Types.INTEGER;
        } else if ((value.equals("REAL")) || (value.equals("FLOAT"))) {
            result = java.sql.Types.REAL;
        } else if ((value.equals("NUMERIC")) || (value.equals("BIGDECIMAL"))) {
            result = java.sql.Types.NUMERIC;
        } else if (value.equals("REF")) {
            result = java.sql.Types.REF;
        } else if (value.equals("SQLXML")) {
            result = java.sql.Types.SQLXML;
        } else if ((value.equals("SMALLINT")) || (value.equals("SHORT"))) {
            result = java.sql.Types.SMALLINT;
        } else if (value.equals("STRUCT")) {
            result = java.sql.Types.STRUCT;
        } else if (value.equals("TIME")) {
            result = java.sql.Types.TIME;
        } else if (value.equals("TIMESTAMP")) {
            result = java.sql.Types.TIMESTAMP;
        } else if ((value.equals("TINYINT")) || (value.equals("BYTE"))) {
            result = java.sql.Types.TINYINT;
        } else if ((value.equals("VARBINARY")) || (value.equals("BYTEARRAY"))) {
            result = java.sql.Types.VARBINARY;
        } else if ((value.equals("VARCHAR")) || (value.equals("STRING"))) {
            result = java.sql.Types.VARCHAR;
        }

        return result;
    }
}

Related

  1. getColumeTypeDesc(Class clazz)
  2. getColumnClass(int sqlType)
  3. getColumnType(Connection conn, String table, String column)
  4. getColumnType(String type)
  5. getConnection(String jdbcUrl, Properties properties)
  6. getDefaultPrecision(int sqlType)
  7. getDefaultScale(int sqlType)
  8. getDefaultValue(Class type)
  9. getDoubleTypeString(Connection conn)