Java SQL Time getClassForSqlType(String sqlType, Class dateTimeClass)

Here you can find the source of getClassForSqlType(String sqlType, Class dateTimeClass)

Description

Returns the Java class for a given SQL type.

License

Mozilla Public License

Parameter

Parameter Description
sqlType the SQL type
dateTimeClass the preferred date class (java.util.Date or java.sql.Timestamp)

Return

Class of type

Declaration

static Class<?> getClassForSqlType(String sqlType,
        Class<? extends java.util.Date> dateTimeClass) 

Method Source Code

//package com.java2s;
/*/*from w  w w .  j  a  v a2 s. c o  m*/
 * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
 * and the EPL 1.0 (http://h2database.com/html/license.html).
 * Initial Developer: James Moger
 */

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**
     * The list of supported data types. It is used by the runtime mapping for
     * CREATE statements.
     */
    private static final Map<Class<?>, String> SUPPORTED_TYPES = new HashMap<Class<?>, String>();
    /**
     * Convert SQL type aliases to the list of supported types.
     * This map is used by generation and validation.
     */
    private static final Map<String, String> SQL_TYPES = new HashMap<String, String>();

    /**
     * Returns the Java class for a given SQL type.
     *
     * @param sqlType the SQL type
     * @param dateTimeClass the preferred date class (java.util.Date or
     *            java.sql.Timestamp)
     * @return Class of type
     */
    static Class<?> getClassForSqlType(String sqlType,
            Class<? extends java.util.Date> dateTimeClass) {
        sqlType = sqlType.toUpperCase();
        // TODO dropping "UNSIGNED" or parts like that could be trouble
        sqlType = sqlType.split(" ")[0].trim();

        if (SQL_TYPES.containsKey(sqlType)) {
            // convert the sqlType to a standard type
            sqlType = SQL_TYPES.get(sqlType);
        }
        Class<?> mappedClass = null;
        for (Class<?> clazz : SUPPORTED_TYPES.keySet()) {
            if (SUPPORTED_TYPES.get(clazz).equalsIgnoreCase(sqlType)) {
                mappedClass = clazz;
                break;
            }
        }
        if (mappedClass != null) {
            if (mappedClass.equals(java.util.Date.class)
                    || mappedClass.equals(java.sql.Timestamp.class)) {
                return dateTimeClass;
            }
            return mappedClass;
        }
        return null;
    }
}

Related

  1. deserializeSqlTime(String text)
  2. double2Time(Number zahl)
  3. equalsTime(Time time1, Time time2)
  4. extractTime(Date date)
  5. getCalTime(Time startTime, Calendar cal)
  6. getClosestIDByTime(Connection con, String table, long time)
  7. getDateTimeValue(ResultSet result, String strField, String strDateFormat)
  8. getDBTime(String yyyyMMddHHmmss)
  9. getFormatDateTimeDesc(long longDate)