java.sql中的datetime等价物? (有java.sql.datetime吗?) [英] A datetime equivalent in java.sql ? (is there a java.sql.datetime ?)

查看:187
本文介绍了java.sql中的datetime等价物? (有java.sql.datetime吗?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我还没有找到一个明确的答案。



我想知道使用PreparedStatement的SQL类型DATETIME和java类型的等价物。



我发现: http://www.java2s .com / Code / Java / Database-SQL-JDBC / StandardSQLDataTypeswithTheirJavaEquivalents.htm



但是它表示SQL类型DATETIME与sql相同。日期,但是当查看SQL日期文档( http: //download.oracle.com/javase/7/docs/api/java/sql/Date.html ),它表示时间被截断(全零)。



我想要的是可以指定一个 preparedStatement.setDateTime()或某种类型。



我唯一看到的方法是使用时间戳,但是这需要我更改列类型,而我无法想象别人从来没有之前的问题?



任何提示?



编辑:我正在使用MYSQL。

解决方案

java.sql包有三个日期/时间类型:





你想要最后一个: 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:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆