java.sql 中的日期时间等价物?(是否有 java.sql.datetime ?) [英] A datetime equivalent in java.sql ? (is there a java.sql.datetime ?)
问题描述
到目前为止,我还没有找到明确的答案.
So far, I have not found a clear answer to this.
我想知道使用 PreparedStatement 的 SQL 类型 DATETIME 和 java 类型的等价物是什么.
I'd like to know what the equivalent is for a SQL type DATETIME and the java type, using a PreparedStatement.
我发现:http://www.java2s.com/Code/Java/Database-SQL-JDBC/StandardSQLDataTypeswithTheirJavaEquivalents.htm
但是它指出 SQL 类型DATETIME"与 sql.date 相同,但是在查看 SQL 日期文档时 (http://download.oracle.com/javase/7/docs/api/java/sql/Date.html),它说时间被截断(全为零).
But it states that SQL type "DATETIME" is the same as sql.date, but when looking at the SQL date docs (http://download.oracle.com/javase/7/docs/api/java/sql/Date.html), it says the time is truncated (all zeros).
我想要的是能够指定 preparedStatement.setDateTime()
或某种类型.
What I want is to be able to specify a preparedStatement.setDateTime()
or some sort.
我看到的唯一另一种方法是使用时间戳,但这需要我更改列类型,而我无法想象其他人以前从未遇到过这个问题?
The only other way I see is using a timestamp, but that would require me to change the column type, while I cannot imagine someone else never had this problem before?
有什么提示吗?
我正在使用 MYSQL.
I am using MYSQL.
推荐答案
java.sql 包具有三种日期/时间类型:
The java.sql package has three date/time types:
java.sql.Date代码>
- 仅日期(无时间部分)java.sql.Time代码>
- 仅时间(无日期部分)java.sql.Timestamp代码>
- 日期和时间
你想要最后一个:java.sql.Timestamp
.
如果您使用这些类型,则不需要调用特定的 setter;只需使用:
If you are using these types, you don't need to call a specific setter; just use:
java.util.Date date = new Date();
Object param = new java.sql.Timestamp(date.getTime());
// The JDBC driver knows what to do with a java.sql type:
preparedStatement.setObject(param);
这篇关于java.sql 中的日期时间等价物?(是否有 java.sql.datetime ?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!