Tuesday, December 24, 2013

java Multiple DB Table Insert Statement and Rollback

  try {
                                    //DB OPERATION
                                    //TURN OFF AUTO COMMIT
                                    conn.setAutoCommit(false);
                                    //CREATE DB SAVEPOINT
                                    Savepoint save1 = conn.setSavepoint();
           
                              try{
                             
                          
                                        java.sql.Statement pst = conn.createStatement();
                                        pst.executeUpdate("INSERT INTO sales_dcl_orders ("
                                        + "order_no, trans_type, version, type,"
                                        + "debtor_no, branch_code, reference, customer_ref,"
                                        + "comments, ord_date, order_type, ship_via,"
                                        + "delivery_address, contact_phone, contact_email, deliver_to,"
                                        + "freight_cost, from_stk_loc, delivery_date, payment_terms,"
                                        + "total, insert_date,user_name) VALUES "
                                        + "('"+ max_id + "','"  + 32+ "','"  + 0 + "','"  + 0 + "'"
                                        + ",'"  + cus_id + "','"  + branch_id + "','"  + reference + "','"  + customer_ref + "'"
                                        + ",'"  + comments + "','"  + Ord_Date + "','"  + Order_Type_id + "','"  + Ship_Vaia_Id + "'"
                                        + ",'"  + Delv_Address + "','"  + Phone + "','"  + "" + "','"  + deliver_to + "'"
                                        + ",'"  + shipping_charge + "','"  + From_Loc_Id + "','"  + val_date + "','"  + Payment_tarm_id + "'"
                                        + ",'"  + inv_total + "','"  + u.todays_datetime() + "','"  + Dashboard.user_id + "')",Statement.RETURN_GENERATED_KEYS);
                
                               
                                   pst.executeUpdate("INSERT INTO audit_trail ("
                                        + "type, trans_no, user_id, description,"
                                        + "fiscal_year, gl_date, type_details ) VALUES "
                                        + "('"+ 32 + "','"  + max_id + "','"  + Dashboard.user_id  + "','"  + "" + "'"
                                        + ",'"  + fiscal_year_id + "','"  + u.todays_date() + "','"  + "Sales Quotation" + "')",Statement.RETURN_GENERATED_KEYS);
                             
                                  
                                 
                                  // get details from table and send to DB
                                 
                                int rowcount = jTable_sales_cotation.getRowCount();
                                   String item_code;
                                   String item_desc;
                                   String item_unit;
                                   String item_quentity;
                                   String item_price;
                                   String item_discount;
                                   while(rowcount > 0){
                                    rowcount--;
                                    item_code = jTable_sales_cotation.getModel().getValueAt(rowcount, 0).toString();
                                    item_desc = jTable_sales_cotation.getModel().getValueAt(rowcount, 1).toString();
                                    item_quentity = jTable_sales_cotation.getModel().getValueAt(rowcount, 2).toString();
                                    item_unit = jTable_sales_cotation.getModel().getValueAt(rowcount, 3).toString();
                                    item_price = jTable_sales_cotation.getModel().getValueAt(rowcount, 4).toString();
                                    item_discount = jTable_sales_cotation.getModel().getValueAt(rowcount, 5).toString();

                                      pst.executeUpdate("INSERT INTO sales_dcl_order_details ("
                                        + "order_no, trans_type, stk_code, description, unit,"
                                        + "unit_price, quantity, discount_percent, insert_date ) VALUES "
                                        + "('"+ max_id + "','"  + 32+ "','"  + item_code + "','"  + item_desc + "','"  + item_unit + "'"
                                        + ",'"  + item_price + "','"  + item_quentity + "','"  + item_discount + "','"  + u.todays_datetime() + "')",Statement.RETURN_GENERATED_KEYS);
                                     
                                }
                        //ALL DONE, COMMIT DB CHANGES
                                       conn.commit();
                                       JOptionPane.showMessageDialog(this, "Successful", "Successful", JOptionPane.PLAIN_MESSAGE);
                            } catch (Exception e) {
                                        pst.close();
                                        conn.rollback(save1);
                                        conn.setAutoCommit(true);
                                        JOptionPane.showMessageDialog(this, "Failed 3" +e, "Error in connectivity", JOptionPane.ERROR_MESSAGE);
                                        return;
                            }
                    } catch (Exception e) {
                        JOptionPane.showMessageDialog(this, e, "Invalid", JOptionPane.ERROR_MESSAGE);
                    }
            
                //TURN ON AUTO COMMIT
                       try {
                           conn.setAutoCommit(true);
                       } catch (SQLException ex) {
               //            Logger.getLogger(PurchaseOrder.class.getName()).log(Level.SEVERE, null, ex);
                       }
                    }

No comments:

Post a Comment