JDBC datetime type
P粉486138196
P粉486138196 2023-12-27 20:52:41
0
1
483

I have aDATETIMEcolumn in a MySQL database. I useResultSetmetadata to retrieve the column type and it returnsTIMESTAMPwhich is incorrect. How can I get the correctjava.sql.SQLTypevalue 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 theJDBCTypeenumeration. Let's say I run this query on theMySQL DATETIMEcolumn. I do know that there is no such thing in Java, its equivalent is thejava.sql.Timestampclass. But in MySQL, DATETIME is not TIMESTAMP.

How to distinguish MySQLDATETIMEand MySQLTIMESTAMPin Java code?

P粉486138196
P粉486138196

reply all (1)
P粉338969567

From a generic JDBC perspective, you can useDatabaseMetaData .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 returnDATETIME).

NOTE: I haven't actually verified this, my answer is purely based on generic JDBC requirements.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!