I have aDATETIME
column in a MySQL database. I useResultSet
metadata to retrieve the column type and it returnsTIMESTAMP
which is incorrect. How can I get the correctjava.sql.SQLType
value for this column?
I'll try to be more specific. I define the structure of the database using Java code. For example;
Column TITLE = new Column(JDBCType.VARCHAR).size(100).title("Created");
Then my little validator code creates this column (assuming the table is defined there). Then later if I modify this code to
Column TITLE = new Column(JDBCType.VARCHAR).size(200).title("Created");
My small validator changes the column size to 200. To do this I retrieve the metadata as
DatabaseMetaData metaData = connection.getMetaData();
Then access the column properties
ResultSet resultSet = metaData.getColumns(getCatalog(), schema, table, columnName);
This will return theJDBCType
enumeration. Let's say I run this query on theMySQL DATETIME
column. I do know that there is no such thing in Java, its equivalent is thejava.sql.Timestamp
class. But in MySQL, DATETIME is not TIMESTAMP.
How to distinguish MySQLDATETIME
and MySQLTIMESTAMP
in Java code?
From a generic JDBC perspective, you can use
DatabaseMetaData .getColumns(...)
, this should return:In other words, it should contain the name of the data type used in the data source (in your case, MySQL should return
DATETIME
).NOTE: I haven't actually verified this, my answer is purely based on generic JDBC requirements.