Andrey Safonov
Andrey Safonov

Reputation: 178

Can't connect to the MySql

I'm trying to connect to MySQL server, but appear errors I can't handle.

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)

Code:

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 */ }
        }
    }
}

Upvotes: 0

Views: 4609

Answers (1)

marme1ad
marme1ad

Reputation: 1383

Root cause here is that user chechevica has no access to peoples DB:

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

You just need to grant access to this user to resolve your issue, something like this will work:

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

For additional details about granting in MySQL it will be useful to read this >>

Upvotes: 0

Related Questions