2013-07-13 167 views
1

如何从数据库检索特定数据并显示?从数据库中检索特定数据并显示

String sql = "Select Product_ID,Order_Quantity,Sub_Total from Order_Menu Where Order_ID='"+order_id_1+"'"; 
    pst = conn.prepareStatement(sql); 
    rs = pst.executeQuery(); 

    while(rs.next()){ 

     list.add(rs.getString("Product_ID")); 
     list.add(rs.getString("Order_Quantity")); 
     list.add(rs.getString("Sub_Total")); 

     for(int i = 0;i<list.size();i++){ 
      String item = list.get(i); 
      System.out.println("Item" + i + ":" + item); 
     } 

我定义数组

ArrayList <String> list = new ArrayList<String>(); 

我的输出

Item0:P0001 
Item1:1 
Item2:37.0 
Item0:P0001 
Item1:1 
Item2:37.0 
Item3:P0002 
Item4:2 
Item5:666.0 

我的数据库只包含2项是P0001和P0002,为什么结果会显示1个项目 对不起,我对数组有点混淆

+0

首先这不是一个数组,它是数组列表 –

+0

好像是while循环不关闭 –

回答

2

您正在打印整个列表每次从结果集中添加一条记录时的内容。 在第一次迭代,list.size() == 3,所以它打印:

Item0:P0001项目1:1项目2:37.0

在第二迭代中,list.size() == 6,所以它打印:

Item0:P0001项目1:1项目2:37.0项目3:P0002项目4:2项目5:666.0

从而使你的最终输出:

Item0:P0001项目1:1项目2:37.0 Item0:P0001项目1:1项目2:37.0项目3:P0002项目4:2项目5:666.0

移动你的for循环while语句之外应该修复它。

while(rs.next()){ 
    list.add(rs.getString("Product_ID")); 
    list.add(rs.getString("Order_Quantity")); 
    list.add(rs.getString("Sub_Total")); 
} 

for(int i = 0;i<list.size();i++){ 
    String item = list.get(i); 
    System.out.println("Item" + i + ":" + item); 
} 
+0

解决它..非常感谢你..只是因为循环。现在我明白了,清楚解释 – Q123Q

0

您在迭代期间没有清除您的列表。这意味着当您处理第一行时,您的列表将包含三个项目,当您显示第二行时,您的列表将包含第一行和第二行的项目...

为什么你不忘记了名单,只是

for (int i = 0; rs.next(); ++i){ 

    System.out.println("Item " + i); 
    System.out.println("Product ID : " + rs.getString("Product_ID")); 
    System.out.println("Order Quantity: " + rs.getString("Order_Quantity")); 
    System.out.println("Sub_Total  : " + rs.getString("Sub_Total")); 

} 

当然,不要忘记来包装整个事情tryfinally和:

rs.close(); 
pst.close(); 
0

确保您遍历列表了侧阿雷列表

while(){ 
    //implementation 
    } 
    for(){ 
    //Iterate your list 
    } 
1

首先这不是使用PreparedStatement的正确方法。你应该按以下方式使用它:

String sql = "Select Product_ID,Order_Quantity,Sub_Total from Order_Menu Where Order_ID=?"; 
pst = conn.prepareStatement(sql); 
pst.setString(order_id_1); 
rs = pst.executeQuery(); 

现在,你输出的原因。

您正在list中添加记录,假设列表中的每个索引只有一个产品正在放置。但事实并非如此。例如,如果您的程序获得产品P0001,那么您将在索引0处添加p001,索引1处的Order_Quantity 1以及索引2处的小计37。所以,你会得到这样的输出。我建议你创建一个单独的类Product如下:

class Product 
{ 
    String product_id; 
    String order_quantity; 
    String sub_total; 
    public Product(String product_id, String order_quantity, String sub_total) 
    { 
    this.product_id = product_id; 
    this.order_quantity = order_quantity; 
    this.sub_total = sub_total; 
    } 
    public void setProduct_id(String product_id) 
    { 
    this.product_id = product_id; 
    } 
    public void setOrder_quantity(String order_quantity) 
    { 
    this.order_quantity = order_quantity; 
    } 
    public void setSub_total(String sub_total) 
    { 
    this.sub_total = sub_total; 
    } 
    public String getProduct_id() 
    { 
    return product_id; 
    } 
    public String getOrder_quantiy() 
    { 
    return order_quantity; 
    } 
    public String getSub_total() 
    { 
    return sub_total; 
    } 
    @Override 
    public boolean equals(Object obj) 
    { 
    if (obj instanceof Product) 
    { 
     temp = (Product)obj; 
     if (product_id.equals(temp.product_id)) 
     { 
      return true; 
     } 
    } 
    return false; 
    } 
    @Override 
    public int hashCode()//override it in such a way so that you get different hashCode for each product ID. 
    { 
    return product_id.hashCode(); 
    } 
    @Override 
    public String toString() 
    { 
    return "product_id:"+product_id+", order_quantity:"+order_quantity+", sub_total:"+sub_total; 
    } 
} 

最后,您可以使用Product类的对象如下:

List<Product> list = new ArrayList<Product>(); 
while(rs.next()){ 
Product product = new Product(rs.getString("Product_ID"),rs.getString("Order_Quantity"),rs.getString("Sub_Total")); 
    list.add(product); 
} 
for(int i = 0;i<list.size();i++){ 
Product item = list.get(i); 
System.out.println("Item" + i + ":" + item); 
} 
0

说你的数据库表如下:

Product_ID Order_Quantity Sub_Total 
P0001  1    37.0 
P0002  2    666.0 

while循环添加元素到您的列表如下:

第一次迭代:

list=["P0001","1","37.0"] 

第二次迭代:

list=["P0001","1","37.0","P0002","2","666.0"] 

所以当while循环执行的第一次, 你所得到的输出为:

Item0:P0001 
Item1:1 
Item2:37.0 

在第二次迭代,因为列表是[“P0001”,“1”,“37.0”,“P0002”,“2”,“666.0”],因此您会得到更多输出。

Item0:P0001 
Item1:1 
Item2:37.0 
Item3:P0002 
Item4:2 
Item5:666.0 

结合上述两个输出,提供给你:

Item0:P0001 
Item1:1 
Item2:37.0 
Item0:P0001 
Item1:1 
Item2:37.0 
Item3:P0002 
Item4:2 
Item5:666.0 

所以你需要二维数据structure.I建议添加字符串的一个维数组列表为:

String sql = "Select Product_ID,Order_Quantity,Sub_Total from Order_Menu Where Order_ID='"+order_id_1+"'"; 
    pst = conn.prepareStatement(sql); 
    rs = pst.executeQuery(); 
    String[] row=new String[3] 
    while(rs.next()){ 

     row[0]=rs.getString("Product_ID"); 
     row[1]=rs.getString("Order_Quantity"); 
     row[2]=rs.getString("Sub_Total"); 

     list.add(row); 
} 
+0

我解决了这个问题..感谢您的明确解释..感谢您的帮助..现在我明白为什么我的输出会是这样的。 – Q123Q

相关问题