I have a table of products containing an item's product number and other details, and a ReviewTable with the product number, rating and review. Because an item can have multiple ratings and reviews I need to retrieve all ratings and reviews for that item.
"drop table ProductTable", // The product table
"create table ProductTable ("+
"productNo Char(4)," + // Unique product number
"description Varchar(40)," +
"picture Varchar(80)," +
"price Float)",
"insert into ProductTable values " +
"('0001', '40 inch LED HD TV', 'images/pic0001.jpg', 269.00)",
"drop table ReviewTable",
"create table ReviewTable ("+
"productNo Char(4)," +
"ratingScore Integer," +
"review Varchar(200))",
"insert into ReviewTable values ( '0001', 2, 'Very high quality, but I had to replace it after 1 year' )",
"insert into ReviewTable values ( '0001', 3, 'Very good' )", // Two reviews for the same product
"select * from ReviewTable, ProductTable " +
" where ReviewTable.productNo = ProductTable.productNo",
I have a Product object that takes as arguments the product number, an array of all its ratings and an array of its reviews:
public Product(String aProductNum, double[] aRating, String[] aReviews) {
theProductNum = aProductNum; // Product number
theRating = aRating; // ratings of product
theReviews = aReviews; // All the reviews for the product
}
Finally, I have a getDetails method that retrieves the data about a product, and this is where I need to add multiple values to an array..
public synchronized Product getDetails( String pNum )
throws StockException
{
try
{
String [] reviews = new String[0]; // Initialise reviews
double [] ratings = new double[0]; // Initialise ratings
Product dt = new Product( "0", ratings, reviews); // Initialise product
ResultSet rs = getStatementObject().executeQuery(
"select ratingScore, review " +
" from ProductTable, ReviewTable " +
" where ProductTable.productNo = '" + pNum + "' " +
" and ReviewTable.productNo = '" + pNum + "'"
);
if ( rs.next() )
{
dt.setProductNum( pNum );
dt.setRating(rs.getDouble("ratingScore") ); // CURRENTLY PULLING ONLY ONE RATING
dt.setReviews(rs.getString("review")); // CURRENTLY PULLING ONLY ONE REVIEW
}
rs.close();
return dt;
} catch ( SQLException e )
{
throw new StockException( "SQL getDetails: " + e.getMessage() );
}
}
Any help please? Thanks a lot in advance
dt.setRating()etc. seem to set only one value of theProductinstance you're reusing for the entire query. If you want to retrieve multiple reviews you'd need to provide code for that, e.g. create aReviewclass and aaddReview(Review)method inProductand then call it likedt.addReview(new Review(rating, review))with the values extracted from the resultset. Later on you might want to use a proper ORM framework for this but that's how you could start.productNoto theProductclass , sorry if that was not clear. I also changed the comment, my mistake - I don't need the average of the ratings but an array with each individual rating.