Getting the data type of a ResultSet column [dupli

2019-06-16 04:07发布

问题:

This question already has an answer here:

  • Map database type to concrete Java class 2 answers

I am using SQL Server. My table has a column with the data type datetime2.

I am accessing the table data using ResultSet. I need to get the data type of a column. The method rs.getColumnTypeName() returns nvarchar for database data type datetime2.

Is there any way to get datetime2 or Timestamp for the same?

回答1:

If you are doing it in Java, there is a class ResultSetMetaData.

You can use ResultSetMetaData.getColumnClassName() to get the fully qualified name of the class.

For example, if the datatype in database is VARCHAR, the value returned is java.lang.String.

Also try using ResultSetMetaData.getColumnLabel() - see whether its applicable in your case.

Java Documentation for ResultSetMetaData