Issue
Code seems to work fine, but I noticed whenever I queried a string with only one result, it returned nothing. Somehow I am skipping the first result I think but have no idea why.
else{
Conn con = null;
try {
con = new Conn();
} catch (Exception e) {
e.printStackTrace();
}
String sql = "SELECT productname, quantityperunit, unitprice FROM products pr, categories ca WHERE pr.categoryID = ca.categoryID AND ProductName LIKE '%" + searchTerm + "%'";
System.out.println("last try");
try {
searchResults = con.query(sql);
if (searchResults.next()){
session.setAttribute("searchResults", searchResults);
}
} catch (Exception e) {
e.printStackTrace();
}
}
and this is the display code:
java.sql.ResultSet resultSet = (java.sql.ResultSet) session.getAttribute("searchResults");
if(resultSet == null){
out.println("Nullified");
}
if(resultSet!=null){
out.println("<table border='1'>");
out.println("<tr><th>Product Name</th><th>Quantity per Item</th><th>Price</th><th>Quantity</th><th><Add to Cart</th></tr>");
while(resultSet.next()){
out.println("<tr><td>"+resultSet.getString("ProductName")+"</td></tr>");
}
out.println("</table>");
}
any help would be appreciated.
Solution
I'd imagine that resultSet.next() moves the cursor to the next result, thus it immediately would skip the first result on the first iteration of the while loop.
Answered By - Khorkrak