How to return an answer from sql?

-1

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

This is the following, I need to register a book in the bank, but before that, I would need to know if the author is already registered in the bank, or if not, I must register first. So I thought I'd create an "Authenticator" method that would query the Author table in the database, and if there was no user-entered name, I would be directed to the login screen. I thought about giving a select in the bank, and if I did not find anything, return null, type one

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

But I have no idea how to do this, I made a

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

In my method it checks for an author, but I do not know how to know if it returned null

FORGIVE ME IF YOU ARE VERY CONFUSED

    
asked by anonymous 01.06.2018 / 05:18

1 answer

2
Very simple friend. Since you have ResultSet on hand, you only have to make a simple if in% method% of it. Example:

if (rs.next()) {
    // Autor encontrado
} else {
    JOptionPane.showMessageDialog(null, "Autor não encontrado");
    // Redirecionar para o cadastro
}
    
01.06.2018 / 05:37