gpt4 book ai didi

java - 无法连接到MySql

转载 作者:行者123 更新时间:2023-11-29 16:58:44 24 4
gpt4 key购买 nike

我正在尝试连接到 MySQL 服务器,但出现了我无法处理的错误。

java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up. at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:902) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:827) at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:456) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240) at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at Main.main(Main.java:21) Caused by: com.mysql.cj.exceptions.CJException: Access denied for user 'chechevica'@'%' to database 'peoples' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:129) at com.mysql.cj.protocol.a.NativeProtocol.checkErrorMessage(NativeProtocol.java:810) at com.mysql.cj.protocol.a.NativeProtocol.checkErrorMessage(NativeProtocol.java:735) at com.mysql.cj.protocol.a.NativeProtocol.checkErrorMessage(NativeProtocol.java:703) at com.mysql.cj.protocol.a.NativeProtocol.checkErrorMessage(NativeProtocol.java:132) at com.mysql.cj.protocol.a.NativeAuthenticationProvider.proceedHandshakeWithPluggableAuthentication(NativeAuthenticationProvider.java:557) at com.mysql.cj.protocol.a.NativeAuthenticationProvider.connect(NativeAuthenticationProvider.java:220) at com.mysql.cj.protocol.a.NativeProtocol.connect(NativeProtocol.java:1443) at com.mysql.cj.NativeSession.connect(NativeSession.java:165) at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:846) ... 7 more Exception in thread "main" java.lang.NullPointerException at Main.main(Main.java:39)

代码:

import java.sql.*;

public class Main {

// JDBC URL, username and password of MySQL server
private static final String url = "jdbc:mysql://localhost:3306/Peoples?autoReconnect=true&useSSL=false";
private static final String user = "chechevica";
private static final String password = "**ur_SQLparol**";

// JDBC variables for opening and managing connection
private static Connection connection;
private static Statement statement;
private static ResultSet resultSet;

public static void main(String[] args) {
String query = "SELECT id_user FROM users";

try {
// opening database connection to MySQL server
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(url, user, password);

// getting Statement object to execute query
statement = connection.createStatement();

// executing SELECT query
resultSet = statement.executeQuery(query);

while (resultSet.next()) {
int count = resultSet.getInt(1);
System.out.println("Total number of users in the table : " + count);
}
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
//close connection ,stmt and resultSet here
try { connection.close(); } catch(SQLException se) { /*can't do anything */ }
try { statement.close(); } catch(SQLException se) { /*can't do anything */ }
try { resultSet.close(); } catch(SQLException se) { /*can't do anything */ }
}
}
}

最佳答案

这里的根本原因是用户chechevica无权访问peoples数据库:

Caused by: com.mysql.cj.exceptions.CJException: Access denied for user 'chechevica'@'%' to database 'peoples' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at 

您只需授予此用户访问权限即可解决您的问题,如下所示:

GRANT ALL PRIVILEGES ON peoples.* TO 'chechevica'@'%';

有关 MySQL 中授予的更多详细信息,阅读 this >> 会很有用。

关于java - 无法连接到MySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52409789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com