0

hey im working on web application in netbeans and i use the jdbc to store info , im working on search function - the user enter name in string the name could be the exact name or it could be substring of the name i write this function but its not working i may do something wrong :

public List<Book> getListOfBooks(String value) throws SQLException{
    PreparedStatement pStatement ;
      List<Book> List = new ArrayList<>();
    try {
        pStatement = connection.prepareStatement("select * from BOOKS where BOOK_NAME like %?%");
        pStatement.setString(1, value);

        ResultSet rs =pStatement.executeQuery();

        while (rs.next()) {                
            Book book = new Book();
            book.setIsbn(rs.getString("ISBN"));
            book.setName(rs.getString("BOOK_NAME"));
            book.setAuthor(rs.getString("AUTHOR"));
            book.setCategory(rs.getString("CATEGORY"));
            book.setPublishing(rs.getString("PUBLISHING"));

            List.add(book);

        }
    } catch (SQLException ex) {
        Logger.getLogger(BookDB.class.getName()).log(Level.SEVERE, null, ex);
    }   

    return List;
}

the function return empty list and i dont find whats wrong , ty for help.

shmuel
  • 13
  • 6
  • 1
    Been awhile since I did any heavy lifting in JDBC, but shouldn't the query be more like `where BOOK_NAME like ?"` and the like wildcard (`%`) placed in the parameter, `pStatement.setString(1, "%" + value + "%");`? – MadProgrammer Feb 08 '18 at 21:44
  • i would like to add if i delete the % % and leav only ? when i search the exact name its work but with the % % no matter what i put inside its give me empty list – shmuel Feb 08 '18 at 21:45
  • wow i didnt try this 1 , i tried to write it in some ways but not this 1 – shmuel Feb 08 '18 at 21:47
  • sry i didnt read what u say correctly its work now ty :) – shmuel Feb 08 '18 at 21:49

0 Answers0