Java SQL Type getIntValue(String typeName)

Here you can find the source of getIntValue(String typeName)

Description

Returns an int value for the specified type name from Types or any other class containing SQL type constants, for example oracle.jdbc.OracleTypes .

License

Apache License

Parameter

Parameter Description
typeName the constant name (fully specified, if not from Types )

Exception

Parameter Description
Exception if a constant with the given name is not found, not accessible, not of int type or is null

Return

the corresponding Integer value

Declaration

public static Integer getIntValue(String typeName) throws Exception 

Method Source Code

//package com.java2s;
/*// w  w  w. ja  v a  2  s  .  c  o m
 * Copyright 2014-2015 Victor Osolovskiy, Sergey Navrotskiy
 *
 * 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.
 */

import java.sql.Types;

import java.util.HashMap;
import java.util.Map;

public class Main {
    private static final Map encoder = new HashMap();

    /**
     * Returns an int value for the specified type name from {@link Types} or any other class containing
     * SQL type constants, for example {@link oracle.jdbc.OracleTypes}.
     *
     * @param typeName the constant name (fully specified, if not from {@link Types})
     * @return the corresponding Integer value
     * @throws Exception if a constant with the given name is not found, not accessible, not of int type or is null
     */
    public static Integer getIntValue(String typeName) throws Exception {
        Integer ret = (Integer) encoder.get(typeName);
        if (ret == null) {
            ret = extractConstant(typeName);
            encoder.put(typeName, ret);
        }
        return ret;
    }

    private static Integer extractConstant(String typeName) throws Exception {
        int lastDotPos = typeName.lastIndexOf(".");
        if (lastDotPos < 1) {
            throw new IllegalArgumentException("Type constant is not member of " + Types.class.getName()
                    + " and its name is not fully specified: " + typeName);
        }

        String className = typeName.substring(0, lastDotPos);
        String fieldName = typeName.substring(lastDotPos + 1);

        Class cls = Class.forName(className);
        Integer val = (Integer) cls.getField(fieldName).get(null);

        if (val == null)
            throw new NullPointerException(typeName);
        return val;
    }
}

Related

  1. getDoubleTypeString(Connection conn)
  2. getExpressionChoices(int type)
  3. getFormatTypeAsString(int fieldType)
  4. getInsertarEmpresa()
  5. getIntTypeString(Connection conn)
  6. getJavaType(int dataType, int columnSize, int decimalDegit)
  7. getJavaType(int jdbcType)
  8. getJavaType(int sqlType)
  9. getJavaType(String rdbType)