I have a ResultSet that returns data of different types. The query is constructed dynamically so, at compile time, I don't know what type of value the query will return.
I have written the following code assuming that all results are Strings. But I want to get the type of each value too. How can I do this?
Below is the code I have written.
while (reportTable_rst.next()) {
String column = reportTable_rst.getString(columnIterator);
}
At this point, I would like to get the column type, and get the value according to the data type.
You can call,
To returns designated column's SQL type.
int ResultSetMetaData.getColumnType(int column)
To return designated column's database-specific type name.
String ResultSetMetaData.getColumnTypeName(int column)
I think the above answer is not going in loop and have some lack in details. This code snippet can improve to just show Column Name and corresponding datatype. Here is the fully working code
The
ResultSetMetaData.getColumnType(int column)
returns aint
value specifying the column type found injava.sql.Types
.Example: