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

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

  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 Reply

Your email address will not be published. Required fields are marked *