How to get the data type name from the java.sql.Type code using JDBC?

JDBCObject Oriented ProgrammingProgramming

The java.sql.Types class represents the SQL datatype in integer format. The valueOf() method of the enumeration JDBCType accepts an integer value representing the java.sql.Type and, returns the JDBC type corresponding to the specified value.

Example

Let us create a table with name MyPlayers in MySQL database using CREATE statement as shown below −

CREATE TABLE MyPlayers(
   ID INT,
   First_Name VARCHAR(255),
   Last_Name VARCHAR(255),
   Date_Of_Birth date,
   Place_Of_Birth VARCHAR(255),
   Country VARCHAR(255),
   PRIMARY KEY (ID)
);

Following JDBC program establishes connection with the MySQL database retrieves the contents of the MyPlayers table into a ResultSet object, obtains its metadata, obtains the column types of all the columns of the table as java.sql.Types values (integers), and retrieves the names of the respective types using the valueOf() method.

Example

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.JDBCType;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class SQLTypeName {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating the Statement
      Statement stmt = con.createStatement();
      //Query to retrieve records
      String query = "Select * from MyPlayers";
      //Executing the query
      ResultSet rs = stmt.executeQuery(query);
      //Getting the ResultSetMetadata object
      ResultSetMetaData mertadata = rs.getMetaData();
      //Returns the specified column's java.sql.Type name
      //Retrieving the data type of a column
      int ID_Type = mertadata.getColumnType(1);
      int FirstName_Type = mertadata.getColumnType(2);
      int LastName_Type = mertadata.getColumnType(3);
      int DateOfBirth_Type = mertadata.getColumnType(4);
      int PlaceOfBirth_Type = mertadata.getColumnType(5);
      int Country_Type = mertadata.getColumnType(6);
      System.out.println("Data type of the column ID: "+JDBCType.valueOf(ID_Type));
      System.out.println("Data type of the column First_Name: "+JDBCType.valueOf(FirstName_Type));
      System.out.println("Data type of the column Last_Name: "+JDBCType.valueOf(LastName_Type));
      System.out.println("Data type of the column Date_Of_Birth: "+JDBCType.valueOf(DateOfBirth_Type));
      System.out.println("Data type of the column Place_Of_Birth: "+JDBCType.valueOf(PlaceOfBirth_Type));
      System.out.println("Data type of the column Country: "+JDBCType.valueOf(Country_Type));
   }
}

Output

Connection established......
Price values updated ......
Contents of the Sales table after the update:
Name: Key-Board, Customer Name: Raja, Dispatch Date: 2019-09-01, Delivery Time: 11:00:00, Price: 8500, Location: Hyderabad
Name: Earphones, Customer Name: Roja, Dispatch Date: 2019-05-01, Delivery Time: 11:00:00, Price: 2000, Location: Vishakhapatnam
Name: Mouse, Customer Name: Puja, Dispatch Date: 2019-03-01, Delivery Time: 10:59:59, Price: 4500, Location: Vijayawada
Name: Mobile, Customer Name: Vanaja, Dispatch Date: 2019-03-01, Delivery Time: 10:10:52, Price: 9000, Location: Chennai
Name: Headset, Customer Name: Jalaja, Dispatch Date: 2019-04-06, Delivery Time: 11:08:59, Price: 7500, Location: Goa
raja
Published on 03-Jul-2019 13:15:31
Advertisements