1

I am trying to insert data to a table (order_details) from another table (cart). When I am executing this, It insert data twice in database. I can't figure why it happens. Can anybody help me? Thanks in advance.

CartDAO

public boolean insertCart(String username)
    {
        try
        {
            sql = "INSERT INTO order_details (username, product_code, product_name, qty, product_price, product_pic, order_status)"
                  +"SELECT username, product_code, product_name, qty, product_price, product_pic, order_status FROM cart WHERE username = ? AND order_status = 1";
            con = DbConnection.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, username);
            pst.executeUpdate();
            DbConnection.close();
            return true;
        }catch(ClassNotFoundException | SQLException ex)
        {
            Logger.getLogger(CartDAO.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

CartWS

public boolean insertOrderDetails(String username)
    {
        CartDAO dao = new CartDAO();
        return dao.insertCart(username);
    }
10
  • maybe you call the method twice? Commented Apr 11, 2018 at 8:31
  • 3
    run your select "SELECT username, product_code, product_name, qty, product_price, product_pic, order_status FROM cart WHERE username = ? AND order_status = 1" query and check how many records it returns Commented Apr 11, 2018 at 8:31
  • please close the statement and the connection in finally block! Commented Apr 11, 2018 at 8:33
  • @JayShankarGupta It returns 2 records. Commented Apr 11, 2018 at 8:33
  • and it inserting 4 records in order_details ??@AbrahamArnold Commented Apr 11, 2018 at 8:35

1 Answer 1

3

Add some log statement in your code,

there may be 2 possible reasons for duplicate insertion issue

(1) Your method "insertCart" are calling 2 times from the code. if you add System.out.println() OR some log statement inside insertCart method ... you will be able to find out.

OR

(2) Print your SELECT query before insertion query, like below

System.out.println(SELECT username, product_code, product_name, qty, product_price, product_pic, order_status FROM cart WHERE username = ? AND order_status = 1) 

// donot forget to replace ? with appproriate value

The number of records you are getting from the SELECT statement must be same what you are inserting into the table

Sign up to request clarification or add additional context in comments.

4 Comments

I tried it and it printed SQL query twice. How can I fix that?
it means you are calling the method twice, just check the code from where you are calling insertCart() method. you will have to debug your code by adding some log statement.
I added the complete code here. I am not calling this method twice.. So what will be the problem?
Since Sql statement is printing twice, so it proves that method is calling twice.But without getting access of complete code, I can not comment anything. it may be possible that insertOrderDetails() method is calling twice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.