java.sql中的datetime等价物? (有java.sql.datetime吗?) [英] A datetime equivalent in java.sql ? (is there a java.sql.datetime ?)
问题描述
我想知道使用PreparedStatement的SQL类型DATETIME和java类型的等价物。
但是它表示SQL类型DATETIME与sql相同。日期,但是当查看SQL日期文档( http: //download.oracle.com/javase/7/docs/api/java/sql/Date.html ),它表示时间被截断(全零)。
我想要的是可以指定一个 preparedStatement.setDateTime()
或某种类型。
我唯一看到的方法是使用时间戳,但是这需要我更改列类型,而我无法想象别人从来没有之前的问题?
任何提示?
编辑:我正在使用MYSQL。
java.sql包有三个日期/时间类型:
-
java.sql。日期
- 仅限日期(无时间部分) -
java.sql.Time
- 仅限时间(无日期部分) -
java.sql.Timestamp
- 日期和时间
你想要最后一个: java.sql.Timestamp
。
如果您使用这些类型,则不需要调用一个特定的setter;只需使用:
java.util.Date date = new Date();
Object param = new java.sql.Timestamp(date.getTime());
// JDBC驱动程序知道如何使用java.sql类型:
preparedStatement.setObject(param);
So far, I have not found a clear answer to this.
I'd like to know what the equivalent is for a SQL type DATETIME and the java type, using a PreparedStatement.
I have found: http://www.java2s.com/Code/Java/Database-SQL-JDBC/StandardSQLDataTypeswithTheirJavaEquivalents.htm
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).
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?
Any hints?
Edit: I am using MYSQL.
The java.sql package has three date/time types:
java.sql.Date
- A date only (no time part)java.sql.Time
- A time only (no date part)java.sql.Timestamp
- Both date and time
You want the last one: java.sql.Timestamp
.
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中的datetime等价物? (有java.sql.datetime吗?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!