Java DatabaseMetaData getTypeInfo() method with example


The getTypeInfo() method of the DatabaseMetadata interface is used to get the description of all the data-types supported by the underlying database.

This method returns a ResultSet object describing data types supported. This object holds values for the following details (as column names) −

Column Name
Data Type
Description
TYPE_NAME
String
Name of the data type.
DATA_TYPE
int
Integer value representing this datatype.
PRECISION
int
Maximum precision of this datatype.
LITERAL_PREFIX
String
Prefix used to quote a string literal.
LITERAL_SUFFIX
String
suffix used to quote a string literal.
CASE_SENSITIVE
boolean
Determines whether this datatype is case sensitive
UNSIGNED_ATTRIBUTE
boolean
Determines whether this datatype is an un-signed attribute.
FIXED_PREC_SCALE
boolean
Determines whether the current datatype can be used as a value of currency.
AUTO_INCREMENT
boolean
Determines whether the current datatype can be used for auto-increment.
LOCAL_TYPE_NAME
String
Localized version of this datatype.

To get the DatabaseMetaData object −

  • Make sure your database is up and running.
  • Register the driver using the registerDriver() method of the DriverManager class. Pass an object of the driver class corresponding to the underlying database.
  • Get the connection object using the getConnection() method of the DriverManager class. Pass the URL the database and, user name, password of a user in the database, as String variables.
  • Get the DatabaseMetaData object with respect to the current connection using the getMetaData() method of the Connection interface.

Finally get ResultSet object holding the description of the supported data types, by invoking the getTypeInfo() method of the DatabaseMetaData interface.

Following JDBC program establishes connection with MySQL database, retrieves the description of all the datatypes.

Example

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DatabaseMetadata_getTypeInfo {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String url = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(url, "root", "password");
      System.out.println("Connection established......");
      //Retrieving the meta data object
      DatabaseMetaData metaData = con.getMetaData();
      //Retrieving the columns in the database
      ResultSet info = metaData.getTypeInfo();
      //Printing the column name and size
      while (info.next()) {
         System.out.println("Data type name: "+info.getString("TYPE_NAME"));
         System.out.println("Integer value representing this datatype: "+info.getInt("DATA_TYPE"));
         System.out.println("Maximum precision of this datatype: "+info.getInt("PRECISION"));
         if(info.getBoolean("CASE_SENSITIVE")) {
            System.out.println("Current datatype is case sensitive ");
         } else {
            System.out.println("Current datatype is not case sensitive ");
         }
         if(info.getBoolean("AUTO_INCREMENT")) {
            System.out.println("Current datatype can be used for auto increment");
         } else {
            System.out.println("Current datatype can not be used for auto increment");
         }
         System.out.println(" ");
      }
   }
}

Output

Connection established......
Data type name: BIT
Integer value representing this datatype: -7
Maximum precision of this datatype: 1
Current datatype is case sensitive
Current datatype can not be used for auto increment
Data type name: BOOL
Integer value representing this datatype: -7
Maximum precision of this datatype: 1
Current datatype is case sensitive
Current datatype can not be used for auto increment
Data type name: TINYINT
Integer value representing this datatype: -6
Maximum precision of this datatype: 3
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: TINYINT UNSIGNED
Integer value representing this datatype: -6
Maximum precision of this datatype: 3
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: BIGINT
Integer value representing this datatype: -5
Maximum precision of this datatype: 19
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: BIGINT UNSIGNED
Integer value representing this datatype: -5
Maximum precision of this datatype: 20
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: LONG VARBINARY
Integer value representing this datatype: -4
Maximum precision of this datatype: 16777215
Current datatype is case sensitive
Current datatype can not be used for auto increment
Data type name: MEDIUMBLOB
Integer value representing this datatype: -4
Maximum precision of this datatype: 16777215
Current datatype is case sensitive
Current datatype can not be used for auto increment
Data type name: LONGBLOB
Integer value representing this datatype: -4
Maximum precision of this datatype: 2147483647
Current datatype is case sensitive
Current datatype can not be used for auto increment
Data type name: BLOB
Integer value representing this datatype: -4
Maximum precision of this datatype: 65535
Current datatype is case sensitive
Current datatype can not be used for auto increment
Data type name: TINYBLOB
Integer value representing this datatype: -4
Maximum precision of this datatype: 255
Current datatype is case sensitive
Current datatype can not be used for auto increment
Data type name: VARBINARY
Integer value representing this datatype: -3
Maximum precision of this datatype: 255
Current datatype is case sensitive
Current datatype can not be used for auto increment
Data type name: BINARY
Integer value representing this datatype: -2
Maximum precision of this datatype: 255
Current datatype is case sensitive
Current datatype can not be used for auto increment
Data type name: LONG VARCHAR
Integer value representing this datatype: -1
Maximum precision of this datatype: 16777215
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: MEDIUMTEXT
Integer value representing this datatype: -1
Maximum precision of this datatype: 16777215
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: LONGTEXT
Integer value representing this datatype: -1
Maximum precision of this datatype: 2147483647
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: TEXT
Integer value representing this datatype: -1
Maximum precision of this datatype: 65535
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: TINYTEXT
Integer value representing this datatype: -1
Maximum precision of this datatype: 255
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: CHAR
Integer value representing this datatype: 1
Maximum precision of this datatype: 255
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: NUMERIC
Integer value representing this datatype: 2
Maximum precision of this datatype: 65
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: DECIMAL
Integer value representing this datatype: 3
Maximum precision of this datatype: 65
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: INTEGER
Integer value representing this datatype: 4
Maximum precision of this datatype: 10
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: INTEGER UNSIGNED
Integer value representing this datatype: 4
Maximum precision of this datatype: 10
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: INT
Integer value representing this datatype: 4
Maximum precision of this datatype: 10
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: INT UNSIGNED
Integer value representing this datatype: 4
Maximum precision of this datatype: 10
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: MEDIUMINT
Integer value representing this datatype: 4
Maximum precision of this datatype: 7
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: MEDIUMINT UNSIGNED
Integer value representing this datatype: 4
Maximum precision of this datatype: 8
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: SMALLINT
Integer value representing this datatype: 5
Maximum precision of this datatype: 5
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: SMALLINT UNSIGNED
Integer value representing this datatype: 5
Maximum precision of this datatype: 5
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: FLOAT
Integer value representing this datatype: 7
Maximum precision of this datatype: 10
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: DOUBLE
Integer value representing this datatype: 8
Maximum precision of this datatype: 17
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: DOUBLE PRECISION
Integer value representing this datatype: 8
Maximum precision of this datatype: 17
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: REAL
Integer value representing this datatype: 8
Maximum precision of this datatype: 17
Current datatype is not case sensitive
Current datatype can be used for auto increment
Data type name: VARCHAR
Integer value representing this datatype: 12
Maximum precision of this datatype: 255
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: ENUM
Integer value representing this datatype: 12
Maximum precision of this datatype: 65535
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: SET
Integer value representing this datatype: 12
Maximum precision of this datatype: 64
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: DATE
Integer value representing this datatype: 91
Maximum precision of this datatype: 0
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: TIME
Integer value representing this datatype: 92
Maximum precision of this datatype: 0
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: DATETIME
Integer value representing this datatype: 93
Maximum precision of this datatype: 0
Current datatype is not case sensitive
Current datatype can not be used for auto increment
Data type name: TIMESTAMP
Integer value representing this datatype: 93
Maximum precision of this datatype: 0
Current datatype is not case sensitive
Current datatype can not be used for auto increment

Vikyath Ram
Vikyath Ram

A born rival

Updated on: 30-Jul-2019

255 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements