0

我为我的班级创建了一个名为 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 个错误

4

1 回答 1

0

这似乎是一个家庭作业或练习。

variable title2 might not have been initialized  

是因为您还没有初始化任何字符串变量,包括title2

改变

String title,title2,name,email,t;

String title="", title2="", name="", email="", t="";.

第二,

String query2 = "select  

这是不完整的,它应该是带有结束引号和分号的有意义的值。

喜欢String query2 = "select * from tblName";

在旁注中,
始终提供问题的完整错误堆栈。

于 2012-11-29T10:09:24.903 回答