+--------+-------+-----+
| Name |number |qty |
+--------+-------+-----+
| ab | 5 | 7 |
+--------+-------+-----+
| cd | 1 | 6 |
+--------+-------+-----+
| ef | 0 | 9 |
+--------+-------+-----+
| gh | 8 | 2 |
+--------+-------+-----+
I'm showing this example product table. I need to get this all table date into one jsp without using resultset. I tried to do up to this part:
public Product search(){
Product p = null;
ResultSet rs = DAO.fetch("SELECT * FROM Products");
if (rs.next()) {
p = new Product();
p.setNumber(rs.getString("ProductNumber"));
p.setName(rs.getString("ProductName"));
p.setQty(rs.getString("ProductQty"));
}
return p;
}
I need to add all table data in to list or ?? and return it using method how do i do that?
解决方案
Make your search() method return a List instead of a single Product.
public List search(){
List products = new ArrayList();
ResultSet rs = DAO.fetch("SELECT * FROM Products");
while (rs.next()) {
Product product = new Product();
product.setNumber(rs.getString("ProductNumber"));
product.setName(rs.getString("ProductName"));
product.setQty(rs.getString("ProductQty"));
products.add(product);
}
return products;
}
Then, in your JSP file, iterate over the returned list: