Problem inserting into the database by java

1

I'm trying to make an app from a veterinary clinic and I followed all the right steps to create the database, connect with the eclipse, and so on. The program connects but does not enter anything that was entered into the program in the database, although the message shows that the registration was successful.

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

For 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!");

        }
    }
    });
    
asked by anonymous 23.05.2017 / 01:33

1 answer

0

Your connection never starts, your connection method is returning true or false, you must use the connect 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 invoke the method of connecting to 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();
   }
}
    
23.05.2017 / 01:56