Error connecting to Mysql database in Java

Asked

Viewed 1,040 times

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();
        }
    }

Resulting 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 didn’t mention is the following I’m using mysql Workbench 6.3,and the version of Connector is 5.1 This error.

2 answers

1

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

So in case you just pass it on:

return DriverManager.getConnection("jdbc:mysql://localhost:3306/consultab","root","123");
  • Now there is another detail I am using Mysql Workbench 6.3 and the Connector is version 5.1 that error.

0

It seems to me that in your class Conexao, you forgot to pass user and password from database to method getConnection class DriverManager. 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)"

What translating into Portuguese is:

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

  • A e has my password and number when I put the error ex:("root",123);

  • @Gleistonjosedesantana Poste on your question then what is the code of your class Conexao.

Browser other questions tagged

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