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 will try to be more specific. I define the structure of my database in Java code. For instance;
Column TITLE = new Column(JDBCType.VARCHAR).size(100).title("Created"); And then my small validator code creates this column (assume the table definition is there). Then, later if I modify this code as
Column TITLE = new Column(JDBCType.VARCHAR).size(200).title("Created"); My small validator alters the column size to 200. To do that, I retrieve the metadata as
DatabaseMetaData metaData = connection.getMetaData(); And then access the column properties as
ResultSet resultSet = metaData.getColumns(getCatalog(), schema, table, columnName); This returns me JDBCType enumeration. Let's assume that I run this query on a MySQL DATETIME column. I do know that in Java, there is no such thing and its equivalent is java.sql.Timestamp class. But in MySQL, DATETIME is not TIMESTAMP.
How can I differentiate between MySQL DATETIME and MySQL TIMESTAMP in Java code?
TIMESTAMPis the correct type. You are thinking of the MySQL datatype, and not of the way JDBC (and the SQL standard) define types.ResultSetMetaData.getColumnTypeName, which should return the database-specific name of the column (though I haven't verified if MySQL Connector/J returns the correct value here)TIMESTAMPis the correct type in Java but not in MySQL. I need to know the exact column type (or simply its column name) as it appears in MySQL which isDATETIMEin my case.DatabaseMetaData.getColumns, columnTYPE_NAME(and for its name,COLUMN_NAME).