How may i retrieve & populate output components at a .jsp page with MySQL? Also, how may i retrieve records pertaining to a particular item_id? Im using IBM Rational Application Developer.
The following is what i have so far, ridden with errors or missing statements I’m sure.
@DB.class
public Items getItemDetails() {
Connection con = connect(true);
PreparedStatement stmt = null;
ResultSet rs = null;
String select = "SELECT * FROM TEST.ITEMBOUGHT WHERE ITEM_ID = ?";
Items item = null;
try {
stmt = con.prepareStatement(select);
//stmt.setString(1,item_id);
rs = stmt.executeQuery();
while (rs.next()) {
//System.out.println("Record Found!");
item = new Items();
//item.setItem_id(item_id);
item.setItem_name(rs.getString("item_name"));
item.setItem_bidding_start_price(rs.getDouble("item_bidding_starting_price"));
item.setItem_bidding_highest_price(rs.getDouble("item_bidding_highest_price"));
item.setItem_description(rs.getString("item_description"));
item.setItem_quantity(rs.getInt("item_quantity"));
item.setItem_closing_date(rs.getDate("item_closing_date"));
item.setItem_image(rs.getString("item_image"));
item.setItem_status(rs.getString("item_closing_date"));
}
} catch (Exception e) {
e.printStackTrace();
item = null;
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (con != null) con.close();
} catch (SQLException e) {}
}
return item;
}
You’re using PreparedStatement for the SQL:
SELECT * FROM TEST.ITEMBOUGHT WHERE ITEM_ID = ?. That means you must tell JDBC what value to replace the question mark (?) in query string, but you put comment on it in//stmt.setString(1,item_id);If you want to select ITEM_ID 1 and ITEM_ID is INTEGER (numeric field), you can use the following code:
If you want to select all records, remove the WHERE clause:
SELECT * FROM TEST.ITEMBOUGHT. If you don’t need PreparedStatement, you can rewrite the code into: