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);
}
}