The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.sql  [73 examples] > Database Meta Data  [8 examples]

e291. Getting the Name of a JDBC Type

This example implements a convenient method for converting a java.sql.Types integer value into a printable name. This method is useful for debugging. The method uses reflection to get all the field names from java.sql.Types. It then retrieves their values and creates a map of values to names.
    // This method returns the name of a JDBC type.
    // Returns null if jdbcType is not recognized.
    public static String getJdbcTypeName(int jdbcType) {
        // Use reflection to populate a map of int values to names
        if (map == null) {
            map = new HashMap();
    
            // Get all field in java.sql.Types
            Field[] fields = java.sql.Types.class.getFields();
            for (int i=0; i<fields.length; i++) {
                try {
                    // Get field name
                    String name = fields[i].getName();
    
                    // Get field value
                    Integer value = (Integer)fields[i].get(null);
    
                    // Add to map
                    map.put(value, name);
                } catch (IllegalAccessException e) {
                }
            }
        }
    
        // Return the JDBC type name
        return (String)map.get(new Integer(jdbcType));
    }
    static Map map;

 Related Examples
e284. Listing All Non-SQL92 Keywords Used by a Database
e285. Listing the String Functions Supported by a Database
e286. Listing the Numeric Functions Supported by a Database
e287. Listing the System Functions Supported by a Database
e288. Listing the Time and Date Functions Supported by a Database
e289. Getting the Maximum Table Name Length in a Database
e290. Listing Available SQL Types Used by a Database

See also: Batching    Connections    Deleting Data    Drivers    Importing and Exporting    Inserting and Updating Data    Oracle OBJECTs    Oracle VARRAYs    Procedures and Functions    Retrieving Data    Scrollable Result Sets    Tables    Updatable Result Sets   


© 2002 Addison-Wesley.