Error while trying to connect to mysql

Asked

Viewed 127 times

1

I am developing a project with Java Web and Mysql and when I went to create the connection and test this giving enough error, I need help to solve this...

package br.com.farmacia.factory;

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

public class Conexao {
    private static final String USUARIO = "root";
    private static final String SENHA = "admin";
    private static final String URL = "jdbc:mysql://localhost:3306/cafeteria?useSSL=false&serverTimezone=UTC";

    public static Connection conectar() throws SQLException {
        Connection conexao = DriverManager.getConnection(URL, USUARIO, SENHA);
        return conexao;
    }

    public static void main(String[] args) {
        try {
            Connection conexao = Conexao.conectar();
            System.out.println("Conectado com Sucesso !!");
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("Conexão falhou !!");
        }
    }
}

Mistakes that are happening

com.mysql.jdbc.exceptions.jdbc4.Mysqlnontransientconnectionexception: Could not create Connection to database server. Connection failed !! at sun.reflect.Nativeconstructoraccessorimpl.newInstance0(Native Method) at sun.reflect.Nativeconstructoraccessorimpl.newInstance(Unknown Source) at sun.reflect.Delegatingconstructoraccessorimpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) at com.mysql.jdbc.Util.getInstance(Util.java:387) at com.mysql.jdbc.SQLError.createSQLException(Sqlerror.java:917) at com.mysql.jdbc.SQLError.createSQLException(Sqlerror.java:896) at com.mysql.jdbc.SQLError.createSQLException(Sqlerror.java:885) at com.mysql.jdbc.SQLError.createSQLException(Sqlerror.java:860) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(Connectionimpl.java:2330) com.mysql.jdbc.ConnectionImpl.createNewIO(Connectionimpl.java:2083) com.mysql.jdbc.ConnectionImpl.(Connectionimpl.java:806) at com.mysql.jdbc.Jdbc4connection.(Jdbc4connection.java:47) at sun.reflect.Nativeconstructoraccessorimpl.newInstance0(Native Method) at sun.reflect.Nativeconstructoraccessorimpl.newInstance(Unknown Source) at sun.reflect.Delegatingconstructoraccessorimpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) at com.mysql.jdbc.ConnectionImpl.getInstance(Connectionimpl.java:410) at com.mysql.jdbc.NonRegisteringDriver.connect(Nonregisteringdriver.java:328) at java.sql.Drivermanager.getConnection(Unknown Source) at java.sql.Drivermanager.getConnection(Unknown Source) at br.com.farmacia.factory.Connexion.connect(Connexion.java:13) at br.com.farmacia.factory.Conexao.main(Conexao.java:19) Caused by: java.lang.Nullpointerexception com.mysql.jdbc.ConnectionImpl.getServerCharset(Connectionimpl.java:2997) at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(Mysqlio.java:1936) at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(Mysqlio.java:1865) at com.mysql.jdbc.MysqlIO.doHandshake(Mysqlio.java:1228) at com.mysql.jdbc.ConnectionImpl.coreConnect(Connectionimpl.java:2253) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(Connectionimpl.java:2284) ... 14 more

2 answers

2

0


Resolution of the problem: I switched mysql-installer-community-8.0.15.0 to mysql-installer-community-5.7.22.1 and switched mysql-Connector-java-5.1.39-bin. jar by mysql-Connector-java-5.1.5-bin and everything is working 100%

Browser other questions tagged

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