Error in Java connection to Mysql Database

Asked

Viewed 241 times

0

I am connecting a Java application to an SQL Database, I have the following connection code:

Connectionfactory.java.:

package connection;

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

public class ConnectionFactory {
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/dbloja";
    private static final String USER = "root";
    private static final String PASS = "****************";

    public static Connection getConnection(){
        try {
            Class.forName(DRIVER);            
            return DriverManager.getConnection(URL, USER, PASS);

        } catch (ClassNotFoundException | SQLException ex) {
            throw new RuntimeException("Erro na conexao", ex);
        }
    }

    public static void closeConnection(Connection con) {
        if(con != null) {
            try {
                con.close();
            } catch (SQLException ex) {
                System.err.println("ERROR: " + ex);                        
            }
        }
    }

    public static void closeConnection(Connection con, PreparedStatement stmt) {
        if(stmt != null) {
            try {
                stmt.close();
            } catch (SQLException ex) {
                System.err.println("ERROR: " + ex);  
            }
        }

        closeConnection(con);
    }

    public static void closeConnection(Connection con, PreparedStatement stmt, ResultSet rs) {
        if(rs != null) {
            try {
                rs.close();
            } catch (SQLException ex) {
                System.err.println("ERROR: " + ex);  
            }
        }

        closeConnection(con, stmt);
    }
}

Categoriadao.java:

package model.dao;

import connection.ConnectionFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import model.bean.Categoria;

/**
 *
 * @author Vinicius Souza
 */
public class CategoriaDAO {
    private Connection con = null;

    public CategoriaDAO() {
        con = ConnectionFactory.getConnection();
    }

    public boolean save(Categoria categoria){
        String sql = "INSERT INTO categoria (descricao) VALUES (?)";

        PreparedStatement stmt = null;        

        try {
            stmt = con.prepareStatement(sql);
            stmt.setString(1, categoria.getDescricao());
            stmt.executeUpdate();
            return true;
        } catch (SQLException ex) {
            System.err.println("ERROR: " + ex);
            return false;
        } finally {
            ConnectionFactory.closeConnection(con, stmt);
        }
    }

}

Categoriadaotest.java

package model.dao;

import model.bean.Categoria;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Vinicius Souza
 */
public class CategoriaDAOTest {

    public CategoriaDAOTest() {
    }

    @Test
    public void inserir() {

        Categoria cat = new Categoria("Roupas");
        CategoriaDAO dao = new CategoriaDAO();

        if(dao.save(cat)) {
            System.out.println("Saved");
        } else {
            fail("Error saving");
        }
    }

}

However, I’m getting this error:

Testcase: inserir(model.dao.CategoriaDAOTest):  Caused an ERROR
Erro na conexao
java.lang.RuntimeException: Erro na conexao
    at connection.ConnectionFactory.getConnection(ConnectionFactory.java:21)
    at model.dao.CategoriaDAO.<init>(CategoriaDAO.java:17)
    at model.dao.CategoriaDAOTest.inserir(CategoriaDAOTest.java:20)
Caused by: java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:923)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1725)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1250)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2465)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at connection.ConnectionFactory.getConnection(ConnectionFactory.java:18)

I believe it is an error in the password passage, but I do not know how to fix it, someone could help me?

  • 1

    What version of mysql_connector are you using ?

  • @Viktorhugo am using mysql-Connector-java-8.0.16.jar

  • 1

    Face the only solution I could find to help you would be to create a new user without the caching_sha2_password: CREATE USER '<seu-usuario>'@'localhost' IDENTIFIED WITH mysql_native_password BY '<sua-senha>'; So you will have a user of the old form of Mysql

1 answer

-2

Vinicius Souza,

I think the problem is in the jdbc driver of your mysql, try to use the driver in another version.

Abç.

Browser other questions tagged

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