Error connecting to MySQL database in Java

0

Connection code:

package br.bent.jdbc;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;

public class Conexao {

    private static Conexao conexao;

    public static Conexao getInstance(){
        if (conexao == null){
            conexao = new Conexao();
        }
        return conexao;
    }
    public Connection getConnection() throws ClassNotFoundException,SQLException{
        Class.forName("com.mysql.jdbc.Driver");

        return DriverManager.getConnection("jdbc:mysql://localhost:3306/consultab","root","root");
    } 
    public static void main(String[]args){
        try{
        System.out.println(getInstance().getConnection());
        } catch (Exception e){
            e.printStackTrace();
        }
    }

Resultant error:

Invalid authorization specification message from server: "Access denied for user 'nobody'@'localhost' (using password: NO)"
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2001)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1907)
    at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:2524)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:818)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:1808)
    at com.mysql.jdbc.Connection.<init>(Connection.java:452)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at br.bent.jdbc.Conexao.getConnection(Conexao.java:20)
    at br.bent.jdbc.Conexao.main(Conexao.java:24)

Another detail I did not mention, is the following I'm using mysql workbench 6.3, and the connector version is 5.1 of this error.

    
asked by anonymous 06.07.2015 / 05:13

2 answers

1

In case the error is user and password, the getConnection method receives only String in this case Doc Java .

So in case just pass this:

return DriverManager.getConnection("jdbc:mysql://localhost:3306/consultab","root","123");
    
06.07.2015 / 06:41
0

It seems to me that in your Conexao class, you forgot to pass the database user and password to the getConnection method of the DriverManager class. And because of this your MySQL refused the connection as can be seen by this text:

  Invalid authorization specification message from server: "Access denied for user 'nobody' @ 'localhost' (using password: NO)"

That translating into Portuguese is:

  

Invalid authorization specification server message: "Access denied for user 'nobody' @ 'localhost' (using password: NOT)"

    
06.07.2015 / 05:22