Check number in the database

0

In my database I have a table called "Matrices". In this table I make a record of the livestock matrices that I have on the farm, and each matrix has its respective number, which is stored in the "Number" column of the table. This column is UNIQUE in the database, so if I try to save a new array with the existing number, it does not save. I was looking for a way to make my system more efficient, so that as soon as I type the number to register a new matrix, it would search my database and if it returns any value, it blocks the registration, and if it does not returns no value, lets you proceed with the matrix register.

Edit: I created this code:

 private void txtnumeroFocusLost(java.awt.event.FocusEvent evt) {                                    
   MatrizesDAO dao = new MatrizesDAO();
   if(dao.verificarNumero(evt.getComponent().toString())){
      JOptionPane.showMessageDialog(this.rootPane, "Numero já cadastrado");
      } else {
    }
}                     

but is giving the error

  

Incompatible types: void can not be converted to boolean

    
asked by anonymous 18.10.2016 / 18:58

3 answers

0

I managed to make it work, I do not know if it's the right way. Here is the code:

DAO Code

public List<Matrizes> verificarNumero(String numero) {
    try {
        List<Matrizes> lista = new ArrayList<Matrizes>();

        String cmdSql = "SELECT * FROM matrizes WHERE numero LIKE ?";
        PreparedStatement stmt = conecta.prepareStatement(cmdSql);
        stmt.setString(1, numero);

        ResultSet rs = stmt.executeQuery();

        if (rs != null) {
            while (rs.next()) {
                Matrizes n = new Matrizes();
                n.setNumero(rs.getString("numero"));
                JOptionPane.showMessageDialog(null, "Numero de Matriz já cadastrado");
                NovaMatriz.txtnumero.grabFocus();
                lista.add(n);
            }
        }

        rs.close();
        stmt.close();
        return lista;
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}  

TextField Code

 private void txtnumeroFocusLost(java.awt.event.FocusEvent evt) {                                    
    MatrizesDAO dao = new MatrizesDAO();
    String numero = txtnumero.getText();
    List<Matrizes> lista = dao.verificarNumero(numero);
}                                   
    
22.10.2016 / 21:37
0

The easiest way to do this is to use an error handling using a try catch, and in the event of an error notifying the user, but if the requirement is to check this while the user is editing, a FocusListener may be useful:

field.addFocusListener(new FocusAdapter() {
    public void focusLost(FocusEvent e) {
        verificaValorExistenteNoBanco(e.getComponent().getText());
    }
});

Edit: While creating your screen, once you add the field where the user types that number adds that focus listener ...

field.addFocusListener(new FocusAdapter() {
    public void focusLost(FocusEvent e) {
        if(dao.verificaValorExistenteNoBanco(e.getComponent().getText())) {
            JOptioPane.showMessage(...);
        }
    }
});
    
18.10.2016 / 22:30
-1

You can implement this requirement as follows: In your text field add an event to capture the loss of focus (Example here and documentation here ) Or add a text field customization to support autocomplete (Examples here )

    
18.10.2016 / 19:17