2013-07-13 218 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

相關問題