Java database insertion problem

Asked

Viewed 117 times

1

I’m trying to make an app of a veterinary clinic and followed all the steps right to create the database, connect with the eclipse and etc. The program connects, but does not insert anything that was typed in the program in the database, although the message shows that the registration was carried out successfully.

package BD;
import java.sql.*;


import java.sql.*;

public class BancoDeDados {


    private static String url = "jdbc:mysql://localhost:3306/bd?autoReconnect=true&useSSL=false";
    private static String user = "root";
    private static String pass = "artpop";
    protected static Connection conexao = null;

    public BancoDeDados(){
        if(conexao==null) conecta();
    }




    public static boolean conecta(){
        try {
            conexao = DriverManager.getConnection(url,user,pass);   
            return true;
        }catch(SQLException e){
            System.out.println(e.getMessage());
            return false;
        }
    }



    public static boolean desconecta(){
        try{
            conexao.close();
            return true;
        }catch(SQLException e){
            return false;
        }
    }
}

/**************************************DAO**********/

package BD;
import java.sql.*;
import javax.swing.JOptionPane;
import java.sql.Statement;
import java.util.ArrayList;


public class ClienteDao extends BancoDeDados{


    public boolean adicionarCliente(Cliente c){
        try{
            Statement st =  conexao.createStatement();          
    st.executeUpdate("insert into clientes values ('" + c.getNome_cliente()+"','"+c.getCpf_cliente()+"','"+c.getrg_cliente()+"','"+c.gettelefone_cliente()+"','"+c.getEndereco_cliente()+"');");



        return true;
        }catch(SQLException e){
            return false;
        }
    }

Screen:

continuar.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {
        ClienteDao clienteDAO = new ClienteDao();
        try{
        Cliente cliente = new Cliente(nome.getText(), cpf.getText(), rg.getText(), telefone.getText(), endereco.getText());
        clienteDAO.adicionarCliente(cliente);   
        JOptionPane.showMessageDialog(null, nome.getText() + "Cadastro realizado com sucesso!!");
        }catch(Exception er){
            JOptionPane.showMessageDialog(null,"Não foi possível realizar cadastro, verifique!");

        }
    }
    });
  • Apparently you are not starting the connection, try using the connect method().

  • At what point do you start the connection? In the code, this is never done, and therefore nothing will be communicated to the bank. Also, if the method adicionarCliente returns a boolean and you do not use, what is the sense of returning true and false?

1 answer

0


Your connection is never started, your connection method is returning true or false, you must use the connected method to return the connection to the database and then send the data to your database. You can create a constructor in your Client DAO to call the connection method with the database every time you instantiate the object.

public class BancoDeDados {

    public static java.sql.Connection conecta(){
        try {
            conexao = DriverManager.getConnection(url,user,pass);   
            return conexao;
        }catch(SQLException e){
            System.out.println(e.getMessage());
        }
    }
}

public class ClienteDao {
   private Connection connection;
   public ClienteDao() {
      this.connection = BancoDeDados.conecta();
   }
}

Browser other questions tagged

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