How to get column type of a column in a table with JDBC?

In the following example, all the Employee table column names and their data types are printed using methods of ResultSetMetaData like getColumnName() and getColumnType().

Example on column type JDBC
import java.sql.*;
public class GetDataInfo
{
  public static void main(String args[]) throws Exception
  {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con =  DriverManager.getConnection("jdbc:odbc:snrao","scott","tiger");
    Statement stmt = con.createStatement( );
    ResultSet res = stmt.executeQuery("select * from Employee");
    ResultSetMetaData rsmd = res.getMetaData( );
    for(int i = 1 ; i < = rsmd.getColumnCount(); i++)
    {
      System.out.print(rsmd.getColumnName(i) + "  " + rsmd.getColumnType(i));

      switch(rsmd.getColumnType(i))   
      {
        case java.sql.Types.BIGINT:  
          System.out.println("BIGINT");      break;
        case java.sql.Types.FLOAT:   
          System.out.println("FLOAT");       break;
        case java.sql.Types.REAL:  
          System.out.println("REAL");        break;
        case java.sql.Types.DOUBLE: 
          System.out.println("DOUBLE");      break;
        case java.sql.Types.NUMERIC:  
          System.out.println("NUMERIC");     break;
        case java.sql.Types.DECIMAL:  
          System.out.println("DECIMAL");     break;
        case java.sql.Types.CHAR:  
          System.out.println("CHAR");        break;
        case java.sql.Types.VARCHAR:  
          System.out.println("VARCHAR|");    break;
        case java.sql.Types.LONGVARCHAR: 
          System.out.println("LONGVARCHAR"); break;
        case java.sql.Types.DATE: 
          System.out.println("DATE");        break;
        case java.sql.Types.TIME: 
          System.out.println("TIME");        break;
        case java.sql.Types.TIMESTAMP: 
          System.out.println("TIMESTAMP");   break;
         case java.sql.Types.BINARY:  
           System.out.println("BINARY");     break;
         case java.sql.Types.VARBINARY:  
           System.out.println("VARBINARY");  break;
         case java.sql.Types.LONGVARBINARY:   
           System.out.println("LONGVARBINARY"); break;
         case java.sql.Types.NULL: 
           System.out.println("NULL");        break;
         case java.sql.Types.OTHER:  
           System.out.println("OTHER");       break;
         case java.sql.Types.JAVA_OBJECT:  
           System.out.println("JAVA_OBJECT"); break;
         case java.sql.Types.DISTINCT:  
           System.out.println("DISTINCT");    break;
         case java.sql.Types.STRUCT:  
           System.out.println("STRUCT");      break;
         case java.sql.Types.ARRAY:  
           System.out.println("ARRAY");      break;
         case java.sql.Types.BLOB:  
           System.out.println("BLOB");        break;
         case java.sql.Types.CLOB:  
           System.out.println("CLOB");        break;
         case java.sql.Types.REF:  
           System.out.println("REF");         break;
      }                                    // switch closing
    }                                      // for closing
  }  
} 
  1. int getColumnCount(): Returns the number of columns of a table
  2. String getColumnName(int columnNumber): Returns the name of the column whose index number is passed as parameter
  3. int getColumnType(int columnNumber): Returns the name of the column as an integer value whose index number is passed as parameter. For example, 3 indicates DECIMAL and 12 indicates VARCHAR. 12 is represented as java.sql.Types.VARCHAR.

Output:
EMPID 3 DECIMAL
EMPNAME 12 VARCHAR
EMPSAL 3 DECIMAL

Alternatively, you can use rsmd.getColumnTypeName(column) that returns database specific name.

Leave a Comment

Your email address will not be published.