我正在尝试使用JDBC连接Azure数据库。但联系不上。我正确地使用了服务器名、用户名、数据库名和密码。
使用Mysql连接器v8.0.13
import java.sql.*;
import java.util.Properties;
public class MyConnection {
public static Connection getConnection() {
System.out.println("MySQL JDBC driver detected in library path.");
Connection connection = null;
try
{
String url ="jdbc:mysql://<servername>:3306/<databaseName>?useSSL=true&requireSSL=false";
connection = DriverManager.getConnection(url, <username>, <password>);
}
catch (SQLException e)
{
//throw new SQLException("Failed to create connection to database.", e);
}
if (connection != null)
{
System.out.println("Successfully created connection to database.");
}
else {
System.out.println("Failed to create connection to database.");
}
System.out.println("Execution finished.");
return connection;
}
}我希望展示“成功创建到数据库的连接”。但是它的显示“未能创建到数据库的连接”。
发布于 2019-07-16 06:09:04
作为评论的摘要和我的补充内容。
正如@LomatHaider所说,Java代码引发的问题引发了一个关于时区的错误。根据MySQL官方文件MySQL Server Time Zone Support,如下所示。
时区变量 MySQL服务器维护多个时区设置:
因此,如果从未为MySQL设置时区变量,则MySQL中的默认时区设置将跟随到OS时区。如我所知,Azure上的默认时区是UTC,那么客户端连接的时区--前提或本地的--可能与Azure上的MySQL不同,冲突导致了如下所示的问题信息。
java.sql.SQLException:服务器时区值“马来半岛标准时间”无法识别或表示多个时区。如果要利用时区支持,必须配置服务器或JDBC驱动程序(通过serverTimezone配置属性)以使用更特定的时区值。
因此,修复它的解决方案如下所示:
SET GLOBAL time_zone = timezone;。?useTimezone=true&serverTimezone=UTC,以强制对JDBC连接会话使用相同的时区值。有一个博客MySQL JDBC Driver Time Zone Issue :: \[SOLVED\]介绍了与这个问题相同的问题,并通过上面的第二个选项来修正它。
https://stackoverflow.com/questions/57030657
复制相似问题