集册 Java实例教程 将给定值转换为给定类型的实例。

将给定值转换为给定类型的实例。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

511
将给定值转换为给定类型的实例。

/*

 * #%L

 * Wisdom-Framework

 * %%

 * Copyright (C) 2013 - 2014 Wisdom Framework

 * %%

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 * 

 *      http://www.apache.org/licenses/LICENSE-2.0

 * 

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 * #L%

 */// 来 自 nowjava - 时  代  Java

//package com.nowjava;


import java.sql.SQLException;


public class Main {



    /**

     * Transforms the given value to an instance of the given type.

     * @param value the value

     * @param type the type

     * @return an instance of the type having the wrapped value

     * @throws SQLException if the conversion is not possible.

     */

    public static Object toBasicType(String value, String type)

            throws SQLException {


        // Early return from first "if" condition that evaluates to true


        if (value == null) {

            return null;/** 来 自 NowJava.com - 时代Java**/

        }


        if (type == null || type.equals(String.class.getName())) {

            return value;

        }


        if (type.equals(Integer.class.getName())

                || type.equals(int.class.getName())) {

            try {

                return Integer.valueOf(value);

            } catch (NumberFormatException e) {

                throwSQLException(e, "Integer", value);

            }

        }


        if (type.equals(Float.class.getName())

                || type.equals(float.class.getName())) {

            try {

                return Float.valueOf(value);

            } catch (NumberFormatException e) {

                throwSQLException(e, "Float", value);

            }

        }


        if (type.equals(Long.class.getName())

                || type.equals(long.class.getName())) {

            try {

                return Long.valueOf(value);

            } catch (NumberFormatException e) {

                throwSQLException(e, "Long", value);

            }

        }


        if (type.equals(Double.class.getName())

                || type.equals(double.class.getName())) {

            try {

                return Double.valueOf(value);

            } catch (NumberFormatException e) {

                throwSQLException(e, "Double", value);

            }

        }


        if (type.equals(Character.class.getName())

                || type.equals(char.class.getName())) {

            if (value.length() != 1) {

                throw new SQLException("Invalid Character value: " + value);

            }

            return value.charAt(0);

        }


        if (type.equals(Byte.class.getName())

                || type.equals(byte.class.getName())) {

            try {

                return Byte.valueOf(value);

            } catch (NumberFormatException e) {

                throwSQLException(e, "Byte", value);

            }

        }


        if (type.equals(Short.class.getName())

                || type.equals(short.class.getName())) {

            try {

                return Short.valueOf(value);

            } catch (NumberFormatException e) {

                throwSQLException(e, "Short", value);

            }

        }


        // Will be "false" if not in correct format...

        if (type.equals(Boolean.class.getName())

                || type.equals(boolean.class.getNam
展开阅读全文