Java DatabaseMetaData getTypeInfo() method with example



In this article, we will learn the DatabaseMetaData getTypeInfo() method in Java. The DatabaseMetaData.getTypeInfo() method in Java retrieves information about the supported data types in a database.

What is getTypeInfo()?

The getTypeInfo() method of the DatabaseMetaData interface returns a ResultSet containing information about all the data types supported by the database. Each row in this result set describes a single data type, including details such as its name, precision, case sensitivity, and whether it supports auto-increment.

Syntax

ResultSet info = metaData.getTypeInfo();

This method returns a ResultSet object describing the 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.

Retrieving Database Data Type Information

The following program demonstrates how to use the getTypeInfo() method. It connects to a MySQL database and retrieves information about the supported data types in a database.

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 of the database and, the user name, and password of a user in the database, as String variables.
  • Get the DatabaseMetaData object to the current connection using the getMetaData() method of the Connection interface.

Registering the Driver: Select the required database and register the Driver class of the particular database using the registerDriver() method of the DriverManager class ?

DriverManager.registerDriver(new com.mysql.jdbc.Driver());

Get connection: Create a connection object by passing the URL of the database, username, and password of a user in the database (in string format) as parameters to the getConnection() method of the DriverManager class ?

DriverManager.getConnection(url, user, password);

Retrieving Metadata: retrieves the DatabaseMetaData object ?

DatabaseMetaData metaData = con.getMetaData();

Fetching Type Information: Finally get the ResultSet object holding the description of the supported data types, by invoking the getTypeInfo() method of the DatabaseMetaData interface ?

ResultSet info = metaData.getTypeInfo();

Example

Following the JDBC program establishes a connection with MySQL database, and retrieves the description of all the datatypes ?

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

Conclusion

The DatabaseMetaData.getTypeInfo() method is a powerful tool for retrieving supported data type details from a database. It helps in building adaptable database applications that work across different database systems. Using the provided example, you can explore the supported data types of your database and utilize them effectively in your Java applications.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-02-24T18:27:12+05:30

587 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements