get Sql Type - Java java.sql

Java examples for java.sql:Clob

Description

get Sql Type

Demo Code


//package com.java2s;

public class Main {
    public static int getSqlType(String dbType) {
        dbType = dbType.toUpperCase();/*www.j  a v  a 2 s . c  o  m*/
        if (dbType.startsWith("CHARACTER VARYING")
                || dbType.startsWith("VARCHAR")) {
            return java.sql.Types.VARCHAR;
        } else if (dbType.startsWith("BOOL")) {
            return java.sql.Types.BOOLEAN;
        } else if (dbType.startsWith("TIMESTAMP")) {
            return java.sql.Types.DATE;
        } else if (dbType.startsWith("DATE")) {
            return java.sql.Types.DATE;
        } else if (dbType.startsWith("DOUBLE")) {
            return java.sql.Types.DOUBLE;
        } else if (dbType.startsWith("FLOAT")
                || dbType.startsWith("NUMBER")) {
            return java.sql.Types.FLOAT;
        } else if (dbType.startsWith("INTEGER")) {
            return java.sql.Types.INTEGER;
        } else if (dbType.startsWith("SHORT")
                || dbType.startsWith("SMALLINT")) {
            return java.sql.Types.SMALLINT;
        } else if (dbType.startsWith("LONG") || dbType.startsWith("BIGINT")) {
            return java.sql.Types.BIGINT;
        } else if (dbType.startsWith("BLOB") || dbType.startsWith("BYTEA")) {
            return java.sql.Types.BLOB;
        }

        return 0;
    }

    public static int getSqlType(Class nativeType) {
        if (nativeType == Boolean.class) {
            return java.sql.Types.BOOLEAN;
        }

        if (nativeType == String.class) {
            return java.sql.Types.VARCHAR;
        }

        if (nativeType == Date.class) {
            return java.sql.Types.TIMESTAMP;
        }

        if (nativeType == Double.class) {
            return java.sql.Types.DOUBLE;
        }

        if (nativeType == Float.class) {
            return java.sql.Types.FLOAT;
        }

        if (nativeType == Integer.class) {
            return java.sql.Types.INTEGER;
        }

        if (nativeType == Short.class) {
            return java.sql.Types.SMALLINT;
        }

        if (nativeType == Long.class) {
            return java.sql.Types.BIGINT;
        }

        if (nativeType == (new Byte[] {}).getClass()) {
            return java.sql.Types.BLOB;
        }

        return java.sql.Types.VARCHAR;
    }
}

Related Tutorials