0

I have been working with a java database for this class I'm just trying to search and see if the entry with the same name the user inputs exists. It used to work but for some, it keeps saying it doesn't exist in the database even if it does. If someone could give me a solution it would be greatly appreciated.

  private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {                                          
    name = txtSearch.getText();
    sr.setSearch(name);

    try {
        String url = "jdbc:derby://localhost:1527/Cookbook";
        Connection conn = DriverManager.getConnection(url);
        String sql = "SELECT * from RECIPES where NAME = ?";
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setString(1, name);
        ResultSet rs = pst.executeQuery();
        boolean found = false;

        while (rs.next()) {
            if (rs.getString(1).equalsIgnoreCase(name)) {
                JOptionPane.showMessageDialog(null, "Found " + name);
                found = true;                 
                break;

            }

        }

        if (found) {
            this.dispose();
            modifyingRecipe m = new modifyingRecipe();
            m.setVisible(true);

        } else {
            JOptionPane.showMessageDialog(null, "Item Not Found!");
        }

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

}  
  • 1
    Please, provide some working and not working examples. – lainatnavi Nov 20 '19 at 10:11
  • And full stacktrace pls. – pburgr Nov 20 '19 at 10:27
  • Maybe this answers your question: https://stackoverflow.com/a/12414762/2972052 – ziga1337 Nov 20 '19 at 10:34
  • Why are you doing a `select *` if you only want one specific field? Regardless performance, if you don't specify the fields in the select, there's the possibility that the order in which the database return them vary (for any reason). In that scenario, `getString(1)` would not refer to the name but other field. – Luis Iñesta Nov 20 '19 at 10:40
  • I see you're doing an `equalsIgnoreCase` in your code, but you're also doing `where name = ?` in your select statement. The where clause in your select is going to perform a **case-sensitive** search, so if you're expecting the query to match ignoring case, you need a different approach. – Bryan Pendleton Nov 20 '19 at 18:35

0 Answers0