The Java Developers Almanac 1.4


Order this book from Amazon.

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

e290. Listing Available SQL Types Used by a Database

This example retrieves the SQL data types supported by a database and driver.
    try {
        // Get database meta data
        DatabaseMetaData dbmd = connection.getMetaData();
    
        // Get type info
        ResultSet resultSet = dbmd.getTypeInfo();
    
        // Retrieve type info from the result set
        while (resultSet.next()) {
            // Get the database-specific type name
            String typeName = resultSet.getString("TYPE_NAME");
    
            // Get the java.sql.Types type to which this database-specific type is mapped
            short dataType = resultSet.getShort("DATA_TYPE");
    
            // Get the name of the java.sql.Types value.
            // This method is implemented in e291 Getting the Name of a JDBC Type
            String jdbcTypeName = getJdbcTypeName(dataType);
        }
    } catch (SQLException e) {
    }
Here's an example of output for the MySQL database:
    MySQL Type Name, JDBC Type Name
    
    TINYINT, TINYINT
    BIGINT, BIGINT
    MEDIUMBLOB, LONGVARBINARY
    MEDIUMTEXT, LONGVARBINARY
    LONGBLOB, LONGVARBINARY
    LONGTEXT, LONGVARBINARY
    BLOB, LONGVARBINARY
    TEXT, LONGVARBINARY
    TINYBLOB, VARBINARY
    TINYTEXT, VARBINARY
    CHAR, CHAR
    NUMERIC, NUMERIC
    DECIMAL, DECIMAL
    INT, INTEGER
    MEDIUMINT, INTEGER
    SMALLINT, SMALLINT
    FLOAT, FLOAT
    DOUBLE, DOUBLE
    DOUBLE PRECISION, DOUBLE
    REAL, DOUBLE
    VARCHAR, VARCHAR
    ENUM, VARCHAR
    SET, VARCHAR
    DATE, DATE
    TIME, TIME
    DATETIME, TIMESTAMP
    TIMESTAMP, TIMESTAMP

 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
e291. Getting the Name of a JDBC Type

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.