我为我的班级创建了一个名为 amazon 的项目数据库。在这份报告中,我想展示从最畅销的产品到最后售出的产品。我想获得销量最高的产品,并展示所有购买和展示它们的客户,以及他们的电子邮件和他们购买该产品的数量。好的,我可以完全展示最畅销的产品。而且我知道如何获得第一行(如果两个或更多产品的销售数量相同,我会遇到问题),但我不能只从结果集中的第一个表中获取标题,同时制作另一个查询:/谁能帮我解决这个问题?
void report2 (Connection conn)
throws SQLException, IOException {
String query0 = "select title, SUM (quantityb) AS total from product,buy where id = idb group by title order by total DESC";
String title,title2,name,email,t;
int quan,quanp;
Statement stmt = conn.createStatement ();
ResultSet rset0;
System.out.print("\n\n************************************MOST POPULAR PRODUCTS************************************\n");
System.out.print(" TITLE OF THE PRODUCT TOTAL PURCHASED\n"+
"-------------------------------------------- -------------------------\n");
try {
rset0 = stmt.executeQuery(query0);
} catch (SQLException e) {
System.out.println("Problem reading purchases");
while (e != null) {
System.out.println("Message : " + e.getMessage());
e = e.getNextException();
}
return;
}
while (rset0.next())
{
title = rset0.getString(1);
quan = rset0.getInt(2);
System.out.printf("%-108s %-3d \n ",title,quan);
}
PreparedStatement statement = conn.prepareStatement(query0);
statement.setMaxRows(1);
ResultSet rset1 = statement.executeQuery();
while (rset0.next())
{
title2= rset1.getString(1);
}
String query2 = "select
String query1 = "Select fname,email,quantityb"+
"from custumer,product,buy"+
"where email = emailb"+
"and id = idb"+
"and title ='"+title2+"'";
System.out.print("..............................Who Purchased the most popular product.............................\n");
System.out.print("PRODUCT:"+title2+"\n\n");
System.out.print(" FIRST NAME EMAIL QUANTITY\n"+
"--------------------- ----------------------------- ------------------\n");
ResultSet rset2;
try {
rset2= stmt.executeQuery(query1);
} catch (SQLException e) {
System.out.println("Problem reading people");
while (e != null) {
System.out.println("Message : " + e.getMessage());
e = e.getNextException();
}
return;
}
while (rset2.next())
{
name=rset2.getString(1);
email=rset2.getString(2);
quanp=rset2.getInt(3);
System.out.printf("%-8s \t\t\t%-25s\t\t\t %3d \n ",name,email,quanp);
}
stmt.close();
}
结果
commerce.java:405: 变量 title2 可能没有被初始化 "and title ='"+title2+"'"; ^ 1 个错误