Java SQL Type getPrimitiveInstance(Type type, String valueString)

Here you can find the source of getPrimitiveInstance(Type type, String valueString)

Description

get Primitive Instance

License

Open Source License

Parameter

Parameter Description
type a parameter
valueString a parameter

Exception

Parameter Description
Exception an exception

Declaration

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Object getPrimitiveInstance(Type type, String valueString) throws Exception 

Method Source Code

//package com.java2s;
/**//from www .j ava  2s  .  c om
Copyright (C) 2012  Delcyon, Inc.
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static final String DEFAULT_DATE_FORMAT = "MM/dd/yyyy";
    public static final String DEFAULT_DATE_TIME_FORMAT = "MM/dd/yyyy HH:mm";

    /**
     * @param type
     * @param valueString
     * @return
     * @throws Exception
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static Object getPrimitiveInstance(Type type, String valueString) throws Exception {

        Object typeInstanceObject = null;
        try {
            if (valueString == null) {
                return null;
            } else if (type == int.class || type == Integer.class) {
                typeInstanceObject = Integer.parseInt(valueString);
            } else if (type == float.class || type == Float.class) {
                typeInstanceObject = Float.parseFloat(valueString);
            } else if (type == double.class || type == Double.class) {
                typeInstanceObject = Double.parseDouble(valueString);
            } else if (type == long.class || type == Long.class) {
                typeInstanceObject = Long.parseLong(valueString);
            } else if (type == short.class || type == Short.class) {
                typeInstanceObject = Short.parseShort(valueString);
            } else if (type == byte.class || type == Byte.class) {
                typeInstanceObject = Byte.parseByte(valueString);
            } else if (type == boolean.class || type == Boolean.class) {
                typeInstanceObject = Boolean.parseBoolean(valueString);
            } else if (type == String.class) {
                return new String(valueString);
            } else if (((Class) type).isEnum()) {
                Class enumClass = (Class) type;
                Enum[] objects = (Enum[]) enumClass.getEnumConstants();
                for (Enum object : objects) {
                    if (object.toString().equals(valueString)) {
                        typeInstanceObject = object;
                        break;
                    }
                }
            } else if (type == Date.class) {
                if (valueString.matches("\\d{2}/\\d{2}/\\d{4} \\d{2}:\\d{2}") == true) {
                    typeInstanceObject = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT).parse(valueString);
                } else if (valueString.matches("\\d{2}/\\d{2}/\\d{4}") == true) {
                    typeInstanceObject = new SimpleDateFormat(DEFAULT_DATE_FORMAT).parse(valueString);
                } else {
                    throw new Exception("bad date '" + valueString + "'");
                }

            } else if (type == java.sql.Date.class) {
                if (valueString.matches("\\d{2}/\\d{2}/\\d{4}") == true) {
                    typeInstanceObject = new SimpleDateFormat(DEFAULT_DATE_FORMAT).parse(valueString);
                } else {
                    throw new Exception("bad date '" + valueString + "'");
                }
            }

            else if (type == Timestamp.class) {
                if (valueString.matches("\\d{2}/\\d{2}/\\d{4} \\d{2}:\\d{2}") == true) {
                    Date timestampDate = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT).parse(valueString);

                    if (timestampDate != null) {
                        typeInstanceObject = new Timestamp(timestampDate.getTime());
                    } else {
                        throw new Exception("bad timestamp '" + valueString + "'");
                    }
                } else {
                    throw new Exception("bad timestamp '" + valueString + "'");
                }

            }
        }
        //return null for bad number strings
        catch (NumberFormatException numberFormatException) {
            return null;
        }
        return typeInstanceObject;
    }
}

Related

  1. getOdaTypeName(int odaTypeCode)
  2. getParamArray(String signature, int numParam, boolean returnAllParams)
  3. getPartitionSizeValidationError(int colType, String column, String partitionSize)
  4. getPreferredHibernateType(int sqlType, int size, int precision, int scale, boolean nullable, boolean generatedIdentifier)
  5. getPreparedStatementSetMethodMap()
  6. getReturnClass(String typeName)
  7. getSemestre(Integer actionformation_id, Connection connection, String actionformation_libellecourt)
  8. getSerialPrimaryKeyTypeString(Connection conn)
  9. getSimpleTypeString(String dbType)