How to properly convert oracle data types to java types using JDBC?

Viewed 124

I need to know data types of columns in a JDBC ResultSet.

I was able to get JDBC types, which are generated from Oracle types:

ResultSet rs = databaseMetaData.getColumns(null, "C##TMPUSER", null, null);
rs.next();
int code = rs.getInt(5);
String sqlJavaType = JDBCType.valueOf(code).getName();

which for example returns NUMERIC if the column in oracle is NUMBER.

This is the java sql Type, but I need the corresponding Java types, eg String, int, float, etc.

How I can get these data types?

1 Answers

Inspect the class of the object returned from ResultSet's getObject(i) method:

Class<?> javaColumnType = rs.getObject(n).getClass();

which works for all column types.

Note that if the column contains a null, the call to getClass() will throw a NullPointerException, so you may need to guard against that.

Related