Error while trying to connect Mysql database to eclipse using JDBC

Asked

Viewed 232 times

-2

I’ve tried some solutions, I’ve reinstalled Mysql and Workbench, but it doesn’t seem to accept my password. It just keeps giving the same error. Note: I did not change the user name in the database, I left as default. This is happening after I try to run this class:

public class Conexao {
         public static final String SERVIDOR = "jdbc:mysql://localhost/bdJava";
         public static final String USUARIO = "root";
         public static final String SENHA = "123456";
         
         public Connection getConexao() {
             try {
                 return DriverManager.getConnection(SERVIDOR, USUARIO, SENHA);
                     
           }catch (Exception e) {
                 throw new RuntimeException(e);
             }
      } }

Error shown in console:

Exception in thread "main" java.sql.Sqlexception: Access denied for '@localhost' user (using password: NO) on com.mysql.Cj.jdbc.exceptions.SQLError.createSQLException (Sqlerror.java:129) in com.mysql.Cj.jdbc.exceptions.SQLError.createSQLException (Sqlerror.java:97) in com.mysql.Cj.jdbc.exceptions.SQLExceptionsMapping.translateException (Sqlexceptionsmapping.java.jdbcl) in com.mysql.Cj.jdbc.exceptions.SQLExceptionsMapping.translateException (SQLExceptionsMapping.cjava.jdbcl. ConnectionImpl.createNewIO (Connectionimpl.java:836) in com.mysql.Cj.jdbc.Connectionimpl. (Connectionimpl.java:456) in com.mysql.Cj.jdbc.Connectionimpl.getInstance (Connectionimpl.java:246) com. mysql.cj.jdbc.NonRegisteringDriver.connect (Nonregisteringdriver.java:197) in java.sql.Drivermanager.getConnection (unknown source) in java.sql.Drivermanager.getConnection (unknown source) in com.mysql.Cj.jdbc.admin .TimezoneDump.main (Timezonedump.java:70)

when trying to exchange the password of the bank connection the error persists. there is a peculiar error that caught my attention, error: Exception in thread "main" java.sql.Sqlexception: Access denied to the user '' @ 'localhost' (using password: NO), it appears that the user is empty, but the user of my database is the default, which would be 'root''.

1 answer

2


Let’s create a connection using Mysql, the first thing you need to check is if your database is active, as it is using mysql you can use a software or even the terminal of your computer to do this, a well-known software is the https://www.mysql.com/products/workbench/ before leaving coding it is essential to check this point.

Now that you have verified that your database is working perfectly and you know which port it is using (Ps. usually Mysql is the 3306) be sure that you will need a connection driver and in this case that of Mysql itself, these drivers are nothing more than libraries that can be used in the project, you need to know how your project will store these libs, if it is using https://maven.apache.org/ you need to add it to your pom.xml, realize that you need to know which version of your database is to have compatibility and work, at this link https://mvnrepository.com/artifact/mysql/mysql-connector-java has the versions to download, if the project is a common project in the eclipse just download the jar and add the dependency to the project by right-clicking on the project by going to the build path sub menu and adding an external jar, the same file that was downloaded.

inserir a descrição da imagem aqui

After these steps you need to start thinking about how to implement the connection:

 public class Conexao {
         public static final String SERVIDOR = 
         "jdbc:mysql://localhost/bdJava";
         public static final String USUARIO = "root";
         public static final String SENHA = "123456";
         
         public Connection getConexao() {
             try {
                 return DriverManager.getConnection(SERVIDOR, USUARIO, SENHA);
                     
           }catch (Exception e) {
                 throw new RuntimeException(e);
             }
      } }

at that point was missing the door:

   "jdbc:mysql://localhost/bdJava";

an example of connection could be:

private Conexao() {
    try {
        String driverName = "com.mysql.cj.jdbc.Driver";

        Class.forName(driverName).newInstance();
        String serverName = "127.0.0.1:3306";
        String mydatabase = "bdJava";
        String url = "jdbc:mysql://" + serverName + "/" + mydatabase + "?autoReconnect=true&useSSL=false";
        String username = "root";
        String password = "123456";

        this.connection = java.sql.DriverManager.getConnection(url, username, password);
    } catch (InstantiationException ex) {
        Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException e) {
        System.out.println("Nao foi possivel encontrar o driver: " + e.toString());
    } catch (java.sql.SQLException e) {
        System.out.println("Nao foi possivel conectar ao banco de dados: " + e.toString());
    }
}

Another subject you need to look at is how much remote data connection in mysql 8, this link has a description in english how to solve. https://stackoverflow.com/a/12844804/2829667

ps. A connection using JDBC without any JPA implementation is no longer common in new projects nowadays, in the link below there is a project that contains a connection class where there are some behaviors that a connection class using JDBC may have. https://github.com/andremartds/JDBC_revisao/tree/main/src

Browser other questions tagged

You are not signed in. Login or sign up in order to post.