简体   繁体   English

在JDBC连接上设置时区

[英]Setting a time zone on a JDBC connection

I'm running some java on a Linux box whose time zone is: 我在时区为:的Linux机器上运行一些Java:

% timedatectl
...
America/Los_Angeles (PDT, -700)

During MySQLDatabase.getConnection() I get error: MySQLDatabase.getConnection()期间出现错误:

java.sql.SQLException: The server time zone value 'PDT' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specific time zone value if you want to utilize time zone support.

I cannot change the server time zone, so I'm looking to tell JDBC about it. 我无法更改服务器时区,因此我想告诉JDBC。 Consulting the documentation here it gives me three options, and I am lost attempting to apply them. 查阅这里的文档它给了我三个选择,而我尝试应用它们却迷失了。

Option 1: I'm looking for a setServerTimezone method on the MysqlDataSource and find none. 选项1:我正在MysqlDataSource上寻找setServerTimezone方法,但没有找到。

Option 2: I don't seem to be calling either of those methods. 选项2:我似乎都不在调用这些方法中的任何一个。

Option 3: I don't have a connection URL either, because I start with MysqlDataSource , set username etc and get a Connection from there. 选项3:我也没有连接URL,因为我以MysqlDataSource开头,设置了用户名等并从那里获取Connection

How do I do this? 我该怎么做呢?

I have not figured out the answer to the original question, but I have figured out another way of solving it: 我还没有想出原始问题的答案,但是我想出了另一种解决方法:

  • The default time zone on the "server" (Mariadb/MySQL) refers to the database daemon, not the server box. “服务器”(Mariadb / MySQL)上的默认时区是指数据库守护程序,而不是服务器框。 (It's ambiguous in the docs) (在文档中含糊不清)
  • You can set a default time zone for the database daemon. 您可以为数据库守护程序设置默认时区。 If none is set (which was my case), it is taken from the server operating system. 如果没有设置(这是我的情况),它将从服务器操作系统中获取。
  • To set the daemon default time zone to UTC, edit /etc/mysql/my.conf (or equivalent) and add default_time_zone = +0:00 to the [mysqld] section. 要将守护程序的默认时区设置为UTC,请编辑/etc/mysql/my.conf (或等效选项),并将default_time_zone = +0:00添加到[mysqld]部分。
  • You need to use an offset, not a named time zone, unless you have the timezone-related tables in the mysql database populated (they always exist, but are empty by default). 除非您在mysql数据库中填充了与时区相关的表(它们始终存在,但默认情况下为空),否则需要使用偏移量而不是命名时区。

PS There are people at ex-Sun/Oracle who understand API design. PS:前Sun / Oracle中有一些了解API设计的人。 What about you dudes, if you read this, run some internal classes to teach those who do not understand yet? 伙计们,如果您读了这篇文章,该如何办一些内部课程来教那些还不懂的人呢? And, dear docs people, if you write that X is a property of Y in your docs, can you make sure Y.getX() exists, please? 而且,亲爱的文档Y.getX() ,如果您在文档中写道XY一个属性,请确定Y.getX()存在吗?

On my Rasperry Pi I just had to change the time zone in Rasperian. 在我的Rasperry Pi上,我只需要更改Rasperian的时区。

Hope that will work, Philipp 希望能奏效,菲利普

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

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