简体   繁体   English

无论数据库供应商如何,如何通过JDBC从数据库服务器获取当前日期/时间?

[英]How can I get the current date/time from a DB server via JDBC, regardless of DB vendor?

I'd like to be able to get the current date/time (timestamp) from the DB sever I have a Connection to, regardless of the DB. 我希望能够从与我有连接的数据库服务器获取当前日期/时间(时间戳),而与数据库无关。 (Ideally, I'd like something that works for Oracle, DB2, SQL Server, and Postgres). (理想情况下,我想要适用于Oracle,DB2,SQL Server和Postgres的东西)。

Searching the internet, I've seen allusions to the LOCALTIMESTAMP function and to NOW() and to CURRENT_TIMESTAMP, but I've seen no clarity as to whether any of these truly works in an interoperable fashion across RDBMSes. 在Internet上搜索时,我已经看到了对LOCALTIMESTAMP函数,NOW()和CURRENT_TIMESTAMP的暗示,但是对于其中的任何一个是否真的可以跨RDBMS互操作,我尚不清楚。

I'm willing to code up alternatives for different DBs if necessary, but haven't even seen any clarity on the best way to do this for the individual DB vendors. 我愿意在必要时为不同的数据库编写替代方案,但甚至没有看到针对各个数据库供应商的最佳方法的任何清晰之处。 And surely if it needs to be done differently for each there must be some Java library that wraps JDBC connections and does this? 当然,如果每种方法都需要做不同的事情,那么肯定有一些Java库可以包装JDBC连接,这样做吗?

Aside from figuring out which SQL function to use, I'm also wondering if there's a standard way to do a SQL query that is just a function invocation -- some DBs seem to support 除了弄清楚要使用哪个SQL函数外,我还想知道是否存在一种标准的方法来执行仅是函数调用的SQL查询-一些数据库似乎支持

SELECT fn();

...but Oracle seems to require adding ...但是Oracle似乎需要添加

...FROM DUAL;

I know that the SQL "standard" is a bit dodgy, but I wondered if anyone who has more experience than me with working across databases and with JDBC and related libraries might be able to point me in the right direction. 我知道SQL“标准”有点晦涩难懂,但我想知道是否有人比我有更多的跨数据库,JDBC和相关库的工作经验,是否可以为我指明正确的方向。

thanks! 谢谢!

Function calls differs on various database. 函数调用在各种数据库上有所不同。 While with PostgreSQL it is simple SELECT fun_name() in Oracle you must add FROM dual . 在PostgreSQL中,这是简单的SELECT fun_name()在Oracle中,您必须添加FROM dual In other database I work with: Informix you call procedure/function not with SELECT but with execute procedure/function . 在其他与我一起使用的数据库中:Informix您不是通过SELECT而是通过execute procedure/function

I think the simplest solution is to check during run time what database is currently in use and then build query required by that database. 我认为最简单的解决方案是在运行时检查当前正在使用哪个数据库,然后构建该数据库所需的查询。

current_timestamp是ansi函数,为了兼容,您可以使用double:从dual中选择current_timestamp

You should also keep in mind that different RDBMSes will return the current date and time in different formats. 您还应该记住,不同的RDBMS将以不同的格式返回当前日期和时间。 At the very least, there will be differences in the separator between the date and the time, some will include microseconds by default, and some will include the timezone by default. 至少,日期和时间之间的分隔符会有差异,默认情况下某些会包含微秒,默认情况下会包含时区。

Or write a function/sp to do it and execute that. 或者编写一个函数/ sp来执行它。 Encapsulate the platform differences in the SP. 将平台差异封装在SP中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM