Check number in database

Asked

Viewed 102 times

0

In my database I have a table called "Matrices". In this table I record the livestock matrices I own 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 matrix with the existing number, it does not save.
I was looking for a way to make my system more efficient, making it so that as soon as I typed the number to register a new matrix, it would do a search in my database and if it returned some value, block the registration, and if it does not return any value, let the matrix register continue.

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 you’re making a mistake

Incompatible types: void cannot be converted to Boolean

  • this field should be Auto Increment no ?

  • 1

    Heitor, your application is web or desktop?

  • The application is desktop

  • Luis, the field cannot be Auto Increment, because the number is I who must choose.

  • Hector, this latest edition of yours seems like an attempt at a response. Reverti removing the post but you can post an answer in the "Post an answer" field instead of posting as a question edit.

3 answers

0


I managed to make it work, I do not know if in the correct way. Follow the code:

DAO

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

0

The simplest way to do this is to use an error handling, using a Try catch and, if an error occurs notify the user, but if the requirement is to check this while the user is editing, maybe a Focuslistener will serve:

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

Edit: While creating your screen, as soon as you add the field where the user type this number adds this Focus Listener...

field.addFocusListener(new FocusAdapter() {
    public void focusLost(FocusEvent e) {
        if(dao.verificaValorExistenteNoBanco(e.getComponent().getText())) {
            JOptioPane.showMessage(...);
        }
    }
});
  • I understood the part ai of the Focuslistener, but what would be the command that I would use to verify this value in the bank and then how I do it to give me the message that the number already exists or simply let me perform the registration?

  • I would do the same within the DAO class?

  • Just make a query (a select) in the database using this attribute as filter (select * with table Where number = 'my value')... If any result returns it means that there is already record with this heat, otherwise you can follow normally...

  • But in this case, I wanted a Joptionpane to appear saying that the number already exists. It would be possible to set an example for me?

  • If you have a Dao you always aggregate all the database access functions in the Dao classes... Then this validation during the filling would be done by him... Regarding the other method I mentioned, your rescue method would make an exception (your object may even treat the exception at the level of trying again or adding more information, but if your application has a user interface it is always better than the outer layer - the one that controls the view - handle the exceptions) and then to your user interface will display a helping message of the problem...

  • So man, I tried here and I made a mistake in the "if" part. I think it’s because of my DAO... It’s like sending an example of DAO?

Show 1 more comment

-1

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

  • These links may be a good suggestion, but your response will not be valid if one day the link crashes. In addition, it is important for the community to have content right here on the site. It would be better to include more details in your response. A summary of the content of the links would be helpful enough! Learn more about it in this item of our Community FAQ: We want answers that contain only links?

Browser other questions tagged

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