从结果集中获取信息

时间:2012-11-28 23:33:46

标签: jdbc

我为我的班级做了一个名为亚马逊的项目数据库。 在本报告中,我想从最好的selled产品到最后一个selled显示。 我希望得到最顶级的产品,并展示购买它们的所有客户 并显示和他们的电子邮件以及他们推出的该产品的数量。 好的,我可以完全展示最好的selled产品。我知道如何获得第一行(如果两个或更多产品具有相同的销售数量,我会遇到问题)但我不能从结果集中的第一个表中获取标题其他查询:/ 谁能帮我解决这个问题?

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();
}

RESULT

commerce.java:405:变量title2可能尚未初始化                       “and title ='”+ title2 +“'”;                                      ^ 1错误

1 个答案:

答案 0 :(得分: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";

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