I have a DATETIME
column in a MySQL database. I use ResultSet
metadata to retrieve the column type and it returns TIMESTAMP
which is incorrect. How can I get the correct java.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 the JDBCType
enumeration. Let's say I run this query on the MySQL DATETIME
column. I do know that there is no such thing in Java, its equivalent is the java.sql.Timestamp
class. But in MySQL, DATETIME is not TIMESTAMP.
How to distinguish MySQL DATETIME
and MySQL TIMESTAMP
in Java code?
P粉3389695672023-12-28 13:22:01
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.