[英]JDBC MySQL - Java unable to connect to database server
我在默认端口上运行了本地MySQL,但是由于某种原因我的代码无法连接到它。 restdb是我要连接的数据库名称。 我可以从Workbench进行查询,因此我非常肯定数据库本身已启动。
我有一个Maven项目,并且mysql-connector(mysql-connector-java-8.0.11.jar)在我的Maven依赖关系中。 这是错误的地方吗?
这是我的代码。 它在getConnection失败。 在我的错误日志中,它告诉我:
Could not create connection to database server. Attempted reconnect 3 times. Giving up.
private Connection getConnection()
{
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/restdb?autoReconnect=true&useSSL=true";
String username = "root";
String password = "password";
try
{
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e)
{
System.err.println(e.getMessage());
}
return conn;
}
看来您没有SSL设定。 除非有意,否则请删除URL的“ useSSL = true”部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.