无法连接到MySql

时间:2018-09-19 15:50:59

标签: java mysql

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

  

java.sql.SQLNonTransientConnectionException:无法创建与数据库服务器的连接。尝试重新连接3次。放弃。           在com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)           在com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)           在com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)           在com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)           在com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)           在com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:902)           在com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:827)           在com.mysql.cj.jdbc.ConnectionImpl。(ConnectionImpl.java:456)           在com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)           在com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)           在java.sql.DriverManager.getConnection(DriverManager.java:664)           在java.sql.DriverManager.getConnection(DriverManager.java:247)           在Main.main(Main.java:21)       引起原因:com.mysql.cj.exceptions.CJException:用户'chechevica'@'%'对数据库'peoples'的访问被拒绝           在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)处           在sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)           在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)           在java.lang.reflect.Constructor.newInstance(Constructor.java:423)           在com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)           在com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)           在com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)           在com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:129)           在com.mysql.cj.protocol.a.NativeProtocol.checkErrorMessage(NativeProtocol.java:810)           在com.mysql.cj.protocol.a.NativeProtocol.checkErrorMessage(NativeProtocol.java:735)           在com.mysql.cj.protocol.a.NativeProtocol.checkErrorMessage(NativeProtocol.java:703)           在com.mysql.cj.protocol.a.NativeProtocol.checkErrorMessage(NativeProtocol.java:132)           在com.mysql.cj.protocol.a.NativeAuthenticationProvider.proceedHandshakeWithPluggableAuthentication(NativeAuthenticationProvider.java:557)           在com.mysql.cj.protocol.a.NativeAuthenticationProvider.connect(NativeAuthenticationProvider.java:220)           在com.mysql.cj.protocol.a.NativeProtocol.connect(NativeProtocol.java:1443)           在com.mysql.cj.NativeSession.connect(NativeSession.java:165)           在com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:846)           ...另外7个       线程“主”中的异常java.lang.NullPointerException           在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 */ }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

根本原因是用户 chechevica 无法访问人民数据库:

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 >>