Java SQL Type getTypeforValue(int type)

Here you can find the source of getTypeforValue(int type)

Description

Gets the Field name corresponding to the given type value.

License

Mozilla Public License

Parameter

Parameter Description
type that corresponds to an SQL type.

Return

field name based on the value type.

Declaration

public static String getTypeforValue(int type) 

Method Source Code

//package com.java2s;
/*/*from w w w  .  j  a  va  2  s.c o  m*/
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */

import java.lang.reflect.Field;

import java.sql.Types;

public class Main {
    /**
     * Gets the Field name corresponding to the given type value.
     * <p>
     * Simple reflection to find the field name that has the same value given. If the value cannot be found the text
     * 'OTHER' will be returned.
     * <p>
     * This method will also return null if an error occurs during reflection.
     * 
     * @param type that corresponds to an SQL type.
     * @return field name based on the value type.
     * @see Types
     */
    public static String getTypeforValue(int type) {

        try {
            Class c = Types.class;
            Field[] fields = c.getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                String name = fields[i].getName();
                int value = fields[i].getInt(null);
                if (value == type) {
                    return name;
                }
            }
            return "OTHER";
        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. getSqlValue(Object o)
  2. getStringFormat(int colType)
  3. getTableTypes(DatabaseMetaData dbmd)
  4. getTinyIntTypeString(Connection conn)
  5. getType(int csqltype)
  6. getTypeName(int dataType)
  7. getTypeName(int typeNumber)
  8. getTypeNameForJDBCType(int jdbcType)
  9. getTypeStringForObject(Object o)