0

Using "like" wildcard in prepared statement I still didn't get why I lost a row with this question, Can anyone help me?

As the code below if I search the object contain "N" from the sql command the result will be 4 This picture is what the result suppose to be

However when I run the code, the first row will always disappear This picture is the result I've got when I run the code

I am new to JDBC, Thank You

   String query = "select * from item where title like ?";
    String search = SearchItemText.getText();
    if (SearchItemText.getText().isEmpty()) {
        JOptionPane.showMessageDialog(null, "Search cannot leave blank");
    } else {
        ArrayList<String> item = new ArrayList<>();
        try (
                Connection con = DriverManager.getConnection(url, user, pass);
                PreparedStatement pstmt = con.prepareStatement(query);) {
            {
                pstmt.setString(1, "%" + search + "%");

                ResultSet rs = pstmt.executeQuery();
                if (rs.next() == false) {
                    JOptionPane.showMessageDialog(null, search + " is not exist, try "
                            + "switch to uppercase or lowercase");
                }
                while (rs.next()) {
                    String item_no = rs.getString("item_no");
                    String title = rs.getString("title");
                    String number_available = rs.getString("number_available");
                    item.add(item_no + "," + title + "," + number_available);
                    Object o = item_no + " " + title + " Stock = " + number_available;
                    System.out.println(o);
                    dlm.addElement(o);
                }
            }
            ItemList.setModel(dlm);
        } catch (SQLException e) {
            System.err.println(e);
        }
    } 
Community
  • 1
  • 1
Tom
  • 1
  • 2
  • So much wrong with this code. – duffymo Mar 07 '17 at 00:58
  • I know there are lots of the mistakes in my code, first time doing JDBC T^T any idea how to fix it – Tom Mar 07 '17 at 01:18
  • The below block of code makes your resultset first record to move. if (rs.next() == false) { ...... } Sample : if (executeQuery.next() == false) { System.out.println("Calling false block"); }else{ System.out.println("True and reads first record"); } – johnsi george Mar 07 '17 at 06:32

0 Answers0