Error in connection with the database. Java + Workbench

Asked

Viewed 1,133 times

1

Error in connection with bank. Can anyone help me?

package conexaoprojeto;

import java.sql.*;
import javax.swing.JOptionPane;

public class ConexaoProjeto {

    private final String Driver = "com.mysql.jdbc.Driver";
    private final String Url = "jdbc:mysql://localhost:3306/ConexaoProjeto";
    private final String User = "root";
    private final String Pass = "123456";
    public Connection conn;
    public Statement stmt;
    public ResultSet rs;


    public Statement Conectar() {
        try {
            System.setProperty("jdbc.drivers", Driver);
            conn = DriverManager.getConnection(Url, User, Pass);
            stmt = conn.createStatement();
            JOptionPane.showMessageDialog(null, "Conectado com Sucesso!");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Ocorreu uma falha! Não pode ser conectado" + "\n" + ex.getMessage());
        }
        return stmt;
    }

    public void Desconectar() {
        try {
            conn.close();
            JOptionPane.showMessageDialog(null, "Conexão fechada com sucesso!");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Conexão falhou ao ser encerrada!" + "\n" + ex.getMessage());
        }
    }
}

1 answer

1

Try it like this:

package conexaoprojeto;

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

public class Conecta {

    private String DRIVER = "com.mysql.jdbc.Driver";
    private String BD = "ConexaoProjeto";
    private String URL = "jdbc:mysql://localhost:3306/" +BD;
    private String USERNAME = "root";
    private String PASSWORD = "123456";
    private Connection conexao;
    private Statement stm;
    private String msg;

    public Conecta() {
        this.msg = this.iniciaConexao();                
    }

    public Conecta(String bd, String user, String senha) {
        this.BD = bd;
        this.USERNAME = user;
        this.PASSWORD = senha;
        this.msg = this.iniciaConexao();
    }

    public String iniciaConexao() {
        try {
            Class.forName(this.DRIVER);
            this.conexao = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            // Definimos o objeto responsável por executar os comandos
            this.stm = this.getConexao().createStatement();
            return "sucesso";

        } catch (ClassNotFoundException e) {
            this.conexao = null;
            return "Não foi possivel encontrar o driver de banco: " + e.getMessage();
        } catch (SQLException e) {
            this.conexao = null;
            return "SQLException Erro!" + e.getMessage();
        }
    }

    public String fechaConexao() {
        try {
            if (this.getConexao() != null) {
                this.getConexao().close();
                this.conexao = null;
            }
            if (this.getStm() != null) {
                this.stm = null;
            }
            return "Conexão Encerrada";
        } catch (SQLException ex) {
            return "Houve erro no fechamento da conexão! "+ex.getMessage();
        }
    }

    public Connection getConexao() {
        return conexao;
    }

    public Statement getStm() {
        return stm;
    }

    public String getMsg() {
        return msg;
    }

}

Make sure that the database exists, that the username and password are correct, and that you have added the .jar of the driver JDBC do MySQL in your project. JDBC driver is responsible for integrating Java with the database.

If you’re using Netbeans, click Bibliotecas right click, then go to Adicionar Biblioteca..., how the following image shows:

inserir a descrição da imagem aqui

Look for Driver JDBC do MySQL and add to your project:

inserir a descrição da imagem aqui

  • If it doesn’t work, let me know ;)

  • What would be the . driver jar?

  • @Luizsantos edited my answer. You’re using Netbeans?

  • 1

    I am. I will add and check if it works. I’ll be back with the feed, thanks.

  • Could you add the error messages to your question? (If there are)

  • @Avelino So it is only possible crude when we configure the connection driver, right? Because I posted two questions based on an example from Camilo Lopes' blog. As dúvidas: http://answall.com/questions/89842/o-que-fazer-do-mapping/89854?noredirect=1#comment181874_89854 http://answall.com/questions/90033/conex%C3%A3o-com-database-e-mapping/90035#90035

Show 1 more comment

Browser other questions tagged

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