Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How to retrieve specific data from database and display?

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

I define the array

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

my output

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

My database only contains 2 items which is P0001 and P0002, how come the result will display 1 more item Sorry, I'm slightly confuse about array

share|improve this question
 
First this not a array, It is array list –  Ruchira Jul 13 at 7:44
 
seems to be while loop not close –  Ruchira Jul 13 at 7:50
add comment

5 Answers

You're printing the entire list contents every time you add a record from the result set. In the first iteration, list.size() == 3, so it prints:

Item0:P0001 Item1:1 Item2:37.0

In the second iteration, list.size() == 6, so it prints:

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

Thereby making your eventual output:

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

Moving your for loop outside the while statement should fix it.

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);
}
share|improve this answer
 
solve it..thank you very much.. Just because of the loop. Now i understand, clear explain –  Q123Q Jul 13 at 15:58
add comment

First of all this is not the correct way of using PreparedStatement . You should use it in following way:

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

Now, the reason for your output.

You are adding records in list assuming that at each index of list only one product is placing. But it is not the case. For example , if your program gets product P0001 then you are adding p001 at index 0 , Order_Quantity 1 at index 1 and subtotal 37 at index 2. So , you are getting such output. I would suggest you to create a separate class for Product as follows:

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

And finally, You can use the object of Product class as follows:

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);
}
share|improve this answer
add comment

You are not clearing your list in between iterations. This means that when you process the first row, your list will contain three items, when you display the second row, your list will contain the items from the first row and the second row...

Why don't you forget the list and just

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"));

}

and of course don't forget to wrap the whole thing in try finally and:

rs.close();
pst.close();
share|improve this answer
add comment

Make sure you are iterating list out side the aray list

while(){
    //implementation
   }
   for(){
    //Iterate your list
    }
share|improve this answer
add comment

Say your database table is as follows:

Product_ID  Order_Quantity  Sub_Total 
P0001       1               37.0
P0002       2               666.0

Your while loop adds elements to your list as:

First iteration:

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

Second iteration:

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

So when the while loop executes the first time, You are getting the output as:

Item0:P0001
Item1:1
Item2:37.0

In the second iteration, since list is ["P0001","1","37.0","P0002","2","666.0"],hence you get further output.

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

combining the above two outputs,you are getting:

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

So you need two dimensional data structure.I would suggest add a one dimensional array of strings to the list as:

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);
}
share|improve this answer
 
I solved the problem..thanks for the clear explaination..and thanks for the help..now I understand why my output will be like that. –  Q123Q Jul 13 at 16:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.