Andrews B Anthony
Andrews B Anthony

Reputation: 1381

ResultSetMetaData.getColumnTypeName returns UNKNOWN

I have a query for which the getColumnTypeName returned as UNKNOWN but when getColumnType is invoked it return valid result 92 TIME

I've verified it from here

            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Connecting to database...");
            con= DriverManager.getConnection("jdbc:mysql://localhost/test","root","1234");
            System.out.println("Creating statement...");
            stmt = con.createStatement();
            rs = stmt.executeQuery("select case when id>0 then last_day(date) else makedate(2015,1) end as date from emp.tabDate");
            ResultSetMetaData rsmd=rs.getMetaData();
            System.out.println(rsmd.getColumnTypeName(1));//returns UNKNOWN
            System.out.println(rsmd.getColumnType(1));//returns 92

Is there a way i can get valid Type of the column

Upvotes: 2

Views: 967

Answers (2)

Ravinder Reddy
Ravinder Reddy

Reputation: 24002

As per Java Documentation on Constants of java.sql.Types, the value 92 represents java.sql.TIME.

Your sql statement includes expression and the derived result must be of type 92 i.e. java.sql.TIME. If it is correct, then try to CAST the expression result as TIME and see the result.

Example on CAST:

select cast(
       case when id>0 
                 then last_day(date) 
                 else makedate(2015,1) 
        end as time ) as expr_res 
  from emp.tabDate

Also look into documentation on getColumnTypeName(int). It says,

If the column type is a user-defined type, then a fully-qualified type name is returned.

Upvotes: 1

Andreas
Andreas

Reputation: 159124

If getColumnType(int column) returns Types.DATE (91), then it means you should be calling getDate().

Naming convention is not that difficult to understand. If getColumnType returns Types.XXXXX, it means you should call getXxxxx().

Simple enough, right?

Upvotes: 0

Related Questions