How to return an sql answer?

Asked

Viewed 41 times

-1

This is my first forum post, so I apologize if I make a mistake.

The thing is, I need to register a book in the bank, but before that, I need to know if the author is already registered in the bank, because if not, I must register first. So I thought of creating a "checkAuthor" method that would consult the Author table in the database, and if there was no name typed by the user, would direct to the registration screen. I thought I’d give a select at the bank, and if I didn’t find anything, return null, type one

if (RESULTADODOBANCO = null)
         cadastroAutor.setVisible(true);

But I have no idea how to do that, I made one

String sql="SELECT nome FROM autor WHERE nome=?";
    try {
        PreparedStatement stmt = c.prepareStatement(sql);
        stmt.setString   (5, livro.getAutor());

in my checking methodAuthor, but I don’t know how to know if returned null

FORGIVE ME IF I AM VERY CONFUSED

  • I think an approach to skipping this step of checking if the author the guy typed in exists, would be, instead of him typing the author’s name at the time of registering the book, you create a select with all authors of the Database, and the user select which one he wants.

  • Rick, thanks for the quick response. This job is freaking me out! kk In this case, the client would be on the book registration screen, type the name of the author, and at the time you click on the REGISTER button, call the verification methodAuthor(), this method would check the bank if the author is registered, if not, forward the user to the registration screen

  • In the case you wanted to say that you had a table with the name of all the registered authors, and if not found, the user clicked on the button to register the author, that? Sorry for the complication, I’m new to programming

  • All right, Weslley, I just made a suggestion from another approach, but I’ll answer your question.

  • The approach you suggest, is that on the book registration screen, have a field of type select where all authors will be listed and the user will choose which one he wants, so there is no possibility of him choosing an author that does not exist.

1 answer

2


Very simple friend. Having your ResultSet in hand, all you have to do is make a simple if in the method next() his. Example:

if (rs.next()) {
    // Autor encontrado
} else {
    JOptionPane.showMessageDialog(null, "Autor não encontrado");
    // Redirecionar para o cadastro
}
  • The next() method returns true or false? ?

  • Yes Weslley. If my answer helped you, a certain one in it, it will help me a lot

Browser other questions tagged

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