Listing Available SQL Types Used by a Database - Java JDBC

Java examples for JDBC:Database Meta Data

Description

Listing Available SQL Types Used by a Database

Demo Code

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

public class Main {

  public static void main(String[] argv) {
    Connection connection = null;
    try {/*  ww w  .j  av a2 s . c om*/
      // 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");

        short dataType = resultSet.getShort("DATA_TYPE");

        String jdbcTypeName = getJdbcTypeName(dataType);
      }
    } catch (SQLException e) {

    }
  }

  public static String getJdbcTypeName(int jdbcType) {
    HashMap 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));
  }
}

Related Tutorials