Java SQL Type deserializeSQLTypesToJava(String sqltypename, String value)

Here you can find the source of deserializeSQLTypesToJava(String sqltypename, String value)

Description

deserialize SQL Types To Java

License

Open Source License

Declaration

public static Object deserializeSQLTypesToJava(String sqltypename, String value) throws ParseException 

Method Source Code

//package com.java2s;
/*##############################################################################
    //  w  w w.  j  av  a2 s  .co m
Copyright (C) 2011 HPCC Systems.
    
All rights reserved. This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
    
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
############################################################################## */

import java.math.BigDecimal;

import java.nio.charset.Charset;
import java.sql.Timestamp;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.HashMap;

public class Main {
    public final static HashMap<String, Class> mapSQLtypeNameToJavaClass = new HashMap<String, Class>();
    final public static SimpleDateFormat HoursColonMinutesColonSeconds24HourFormat = new SimpleDateFormat(
            "HH:mm:ss");

    public static Object deserializeSQLTypesToJava(String sqltypename, String value) throws ParseException {
        Class clazz = String.class;

        String cleantypename = sqltypename.trim().toUpperCase();
        if (mapSQLtypeNameToJavaClass.containsKey(cleantypename))
            clazz = mapSQLtypeNameToJavaClass.get(cleantypename);

        if (Boolean.TYPE == clazz)
            return Boolean.parseBoolean(value);
        else if (Byte[].class == clazz)
            return value.getBytes(Charset.forName("UTF-8"));
        else if (Byte.TYPE == clazz)
            return Byte.parseByte(value);
        else if (Integer.TYPE == clazz)
            return Integer.parseInt(value);
        else if (Long.TYPE == clazz)
            return Long.parseLong(value);
        else if (Float.TYPE == clazz)
            return Float.parseFloat(value);
        else if (Double.TYPE == clazz)
            return Double.parseDouble(value);
        else if (Short.TYPE == clazz)
            return Short.parseShort(value);
        else if (Character.TYPE == clazz)
            return value.charAt(0);
        else if (java.math.BigDecimal.class == clazz)
            return new BigDecimal(value.replaceAll(",", ""));
        else if (java.sql.Date.class == clazz) //ASSUMES yyyy-MM-dd!!!
            return java.sql.Date.valueOf(value);
        else if (java.sql.Time.class == clazz)//ASSUMES HH:mm:ss!!!
            try {
                return new java.sql.Time(
                        ((java.util.Date) HoursColonMinutesColonSeconds24HourFormat.parse(value)).getTime());
            } catch (ParseException e) {
                throw new ParseException(
                        "Could not deserialize " + value + " to time (HH:mm:ss) format: " + e.getLocalizedMessage(),
                        e.getErrorOffset());
            } catch (Exception e) {
                throw new ParseException(
                        "Could not deserialize " + value + " to time (HH:mm:ss) format: " + e.getLocalizedMessage(),
                        -1);
            }
        else if (java.sql.Timestamp.class == clazz)
            return Timestamp.valueOf(value); //ASSUMES "2011-10-02 18:48:05.123456";
        else
            return value;
    }

    public static String replaceAll(String input, String forReplace, String replaceWith) {
        if (input == null)
            return "null";

        StringBuffer result = new StringBuffer();
        boolean hasMore = true;
        while (hasMore) {
            int start = input.indexOf(forReplace);
            int end = start + forReplace.length();
            if (start != -1) {
                result.append(input.substring(0, start) + replaceWith);
                input = input.substring(end);
            } else {
                hasMore = false;
                result.append(input);
            }
        }
        if (result.toString().equals(""))
            return input; // nothing is changed
        else
            return result.toString();
    }
}

Related

  1. createTable2()
  2. createTestData()
  3. decodeJavaType(Object pType)
  4. deleteData(Connection conn)
  5. deleteFromAllTables(Connection con)
  6. dumpTypes(Connection pConnection)
  7. extractConstant(String typeName)
  8. fillLogParams(String sql, Map logParams)
  9. findMethod(Class returnType, Class parameterType)