Java SQL Type convertNumeric(Object value, int srcType, int destType)

Here you can find the source of convertNumeric(Object value, int srcType, int destType)

Description

convert Numeric

License

Open Source License

Declaration

public static Object convertNumeric(Object value, int srcType, int destType) throws Exception 

Method Source Code

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

import java.math.*;

public class Main {
    public static Object convertNumeric(Object value, int srcType, int destType) throws Exception {
        // is same source/destination then return value
        // * commented to allow cloneObject to allow clone creation
        // if (srcType == destType) {
        //     return value;
        // }//from   w w w  . jav a  2  s.co  m

        if (isNumeric(destType) == false && isString(destType) == false) {
            throw new Exception("numeric conversion failed. (" + value.toString().trim() + "/" + srcType + ":"
                    + destType + ")");
        }

        try {
            switch (destType) {
            case java.sql.Types.BIT:
                Integer i = new Integer(value.toString().trim());
                return i.equals(new Integer((int) 0)) ? new Boolean(false) : new Boolean(true);
            case java.sql.Types.TINYINT:
                return new Byte(value.toString().trim());
            case java.sql.Types.SMALLINT:
                return new Short(value.toString().trim());
            case java.sql.Types.INTEGER:
                return new Integer(value.toString().trim());
            case java.sql.Types.BIGINT:
                return new Long(value.toString().trim());
            case java.sql.Types.NUMERIC:
            case java.sql.Types.DECIMAL:
                return new BigDecimal(value.toString().trim());
            case java.sql.Types.REAL:
            case java.sql.Types.FLOAT:
                return new Float(value.toString().trim());
            case java.sql.Types.DOUBLE:
                return new Double(value.toString().trim());
            case java.sql.Types.CHAR:
            case java.sql.Types.VARCHAR:
            case java.sql.Types.LONGVARCHAR:
            case java.sql.Types.NCHAR:
            case java.sql.Types.NVARCHAR:
            case java.sql.Types.LONGNVARCHAR:
                return new String(value.toString());
            default:
                throw new Exception("numeric conversion failed. (" + value.toString().trim() + "/" + srcType + ":"
                        + destType + ")");
            }
        } catch (NumberFormatException ex) {
            throw new Exception("numeric conversion failed. (" + value.toString().trim() + "/" + srcType + ":"
                    + destType + ")");
        }
    }

    public static boolean isNumeric(int dataType) {
        switch (dataType) {
        case java.sql.Types.NUMERIC:
        case java.sql.Types.DECIMAL:
        case java.sql.Types.BIT:
        case java.sql.Types.TINYINT:
        case java.sql.Types.SMALLINT:
        case java.sql.Types.INTEGER:
        case java.sql.Types.BIGINT:
        case java.sql.Types.REAL:
        case java.sql.Types.DOUBLE:
        case java.sql.Types.FLOAT:
            return true;
        default:
            return false;
        }
    }

    public static boolean isString(int dataType) {
        switch (dataType) {
        case java.sql.Types.CHAR:
        case java.sql.Types.VARCHAR:
        case java.sql.Types.LONGVARCHAR:
        case java.sql.Types.NCHAR:
        case java.sql.Types.NVARCHAR:
        case java.sql.Types.LONGNVARCHAR:
            return true;
        default:
            return false;
        }
    }
}

Related

  1. columnTypesDiffer(int t1, int t2)
  2. compressType(int type)
  3. convert2MysqlType(String cls)
  4. convertBoolean(Object value, int srcType, int destType)
  5. convertKnownType(Object one, Class to)
  6. convertOracleValue(Object value, int type)
  7. convertParam(Object oValue, Class cType, String sFieldName)
  8. convertSQLtype2JavaClassName(int type)
  9. convertStringToType(String type)