Here you can find the source of convertNumeric(Object value, int srcType, int destType)
public static Object convertNumeric(Object value, int srcType, int destType) throws Exception
//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; } } }