1
I’m developing software for a video rental company with Pattern MVC design (Model-view-controller) and the problem of not saving people is occurring.
It only saves people’s code and I don’t even know why it’s happening to not save customers because my source code doesn’t show errors!
In Mysql database people are not checked NOT NULL.
Class VideoPessoa, layer view:
public class VideoPessoa extends javax.swing.JFrame {
    PessoaController pessoaController;
    Pessoa pessoa;
    /**
     * Creates new form Pessoa
     */
    public VideoPessoa() {
        initComponents();
        new Conexao();
        pessoaController = new PessoaController();
        pessoa = new Pessoa();
    }
private boolean salvarPessoa(){
     if (pessoaController.salvar(pessoa)) {
     JOptionPane.showMessageDialog(this, "Registro gravado com sucesso!");
    }else{
            JOptionPane.showMessageDialog(this, "Erro ao gravar os dados!", "ERRO", JOptionPane.ERROR_MESSAGE);
        }
    return true;
    }
Class PessoaController:
  public class PessoaController {
    private final PessoaDAO pessoaDAO;
    public PessoaController() {
        pessoaDAO = new PessoaDAO();
    }
    public boolean salvar(Pessoa pessoa) {
  boolean retorno ;
    retorno = pessoaDAO.salvar(pessoa);
  return retorno;
}
Class PessoaDAO, including the method of saving people:
public class PessoaDAO {
    private Connection con;
    private final String SQLINSERT = " INSERT INTO pessoa(nome, endereco, bairro, sexo, telefone, celular, CPF, uf, cidade)"
            + " VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?) ";
    private final String SQLPESSOAPELOCODIGO = "SELECT  nome, endereco, bairro, sexo, telefone, celular, CPF, uf, cidade"
            + " FROM pessoa"
            + " WHERE codigo=? ";
    private final String SQLSELECT = " SELECT  codigo, nome, endereco, bairro, sexo, telefone, celular, CPF,  uf, cidade FROM PESSOA";
    private final String SQLUPDATE = " UPDATE pessoa"
            + " SET nome = ?, "
            + " endereco = ?, "
            + " bairro   = ?, "
            + " sexo     = ?, "
            + " telefone = ?, "
            + " celular  = ?, "
            + " CPF      = ?, "
            + " uf       =?, "
            + " cidade   =?, "
            + " WHERE codigo = ?";
    private final String SQLDELETE = "DELETE FROM pessoa"
            + " WHERE codigo = ?";
    private PreparedStatement psInsert, sqlPessoaPeloCodigo, sqlSelect, sqlUpdate, sqlDelete;
    public PessoaDAO() {
        con = Conexao.getConnection();
        try {
            psInsert = con.prepareStatement(SQLINSERT);
            sqlPessoaPeloCodigo = con.prepareStatement(SQLPESSOAPELOCODIGO);
            sqlSelect = con.prepareStatement(SQLSELECT);
            sqlUpdate = con.prepareStatement(SQLUPDATE);
            sqlDelete = con.prepareStatement(SQLDELETE);
        } catch (SQLException ex) {
            Logger.getLogger(PessoaDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
public Pessoa getPessoaPeloCodigo(int codigo) {
        Pessoa pessoa = null;
        try {
            sqlPessoaPeloCodigo.setInt(1, codigo);
            ResultSet rs = sqlPessoaPeloCodigo.executeQuery();
            if (rs.next()) {
                //Instancia o novo filme
                pessoa = new Pessoa();
                //Seta as informações no filme
                pessoa.setCodigo(rs.getInt("codigo"));
                pessoa.setNome(rs.getString("nome"));
                pessoa.setEndereco(rs.getString("endereco"));
                pessoa.setBairro(rs.getString("bairro"));
                pessoa.setSexo(rs.getString("sexo"));
                pessoa.setTelefone(rs.getString("telefone"));
                pessoa.setCelular(rs.getString("celular"));
                pessoa.setCPF(rs.getString("CPF"));
                pessoa.setUf(rs.getString("uf"));
                pessoa.setCidade(rs.getString("cidade"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(PessoaDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return pessoa;
    }
 public boolean salvar(Pessoa pessoa) {
        boolean retorno = false;
        try {
            psInsert.setString(1, pessoa.getNome());
            psInsert.setString(2, pessoa.getEndereco());
            psInsert.setString(3, pessoa.getBairro());
            psInsert.setString(4, pessoa.getSexo());
            psInsert.setString(5, pessoa.getTelefone());
            psInsert.setString(6, pessoa.getCelular());
            psInsert.setString(7, pessoa.getCPF());
            psInsert.setString(8, pessoa.getUf());
            psInsert.setString(9, pessoa.getCidade());
            psInsert.executeUpdate();
            retorno = true;
        } catch (SQLException ex) {
            Logger.getLogger(PessoaDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return retorno;
    }
Class Conexao:
public class Conexao {
    private static Connection con;
    public Conexao() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sistemavideolocadora2", "root", "1234");
            System.out.println(" Conexão obtida!!! ");
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(" Conexão estabelecida com sucesso!!! ");
        }
    }
    public static Connection getConnection() {
        return con;
    }
    public static void closeConnection() {
        try {
            con.close();
            System.out.println(" Conexão fechada!!! ");
        } catch (SQLException ex) {
            Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(" Conexão finalizada com sucessso!!! ");
        }
    }
}
						
I think that’s one of the problems, as he said himself when he put a
sysoutin the save methodPessoa() this is displayed: Person: Person{name=null, address=null, neighborhood=null, sex=null, phone=null, cell=null, CPF=null, code=0, city=null, Uf=null, person=null} `– DiegoAugusto
@Techies, should be and only does not return error because it should be all not null and auto primary incremental key in the database.
– Tiago Oliveira de Freitas
Yes I thought about it too, but his code is very "messy" I couldn’t get it right
– DiegoAugusto
@Tiagooliveiradefrost this, but the attributes of the person are marked [tag:null].
– Igor Contini
@Techies gives discount I’m beginner yet!
– Igor Contini
@Tiagooliveiradefreitas It was what was missing to save the attributes of my person and I did not know that need to set them before my insertion. Thanks a lot for the answer!
– Igor Contini