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

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

Description

convert Boolean

License

Open Source License

Declaration

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

Method Source Code

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

public class Main {
    public static Object convertBoolean(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 ww  .j a  va2 s  .  c  om*/

        if ((isNumeric(destType) == true) || ((isString(destType) == false) && (isBoolean(destType) == false))) {
            throw new Exception("boolean 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.BOOLEAN:
                return new Boolean(value.toString().trim());
            default:
                throw new Exception("boolean conversion failed. (" + value.toString().trim() + "/" + srcType + ":"
                        + destType + ")");
            }
        } catch (NumberFormatException ex) {
            throw new Exception("boolean 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;
        }
    }

    public static boolean isBoolean(int dataType) {
        switch (dataType) {
        case java.sql.Types.BIT:
        case java.sql.Types.BOOLEAN:
            return true;
        default:
            return false;
        }
    }
}

Related

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