SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.US);
                            SimpleDateFormat df_db = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                          
                    //get "test_no"
                            //check existing test_no first
                            // If same env_id , sc_id and start_time between +- 30 seconds, then it should be considered as same test
                          
                            ps.execute("lock table execution in exclusive mode");
                            rs = ps.executeQuery("SELECT test_no FROM execution where env_id="+envID+" and sc_id="+scID+" and test_start between to_date('" + df_db.format(df.parse(testStart_timestamp)) + "','YYYY-MM-DD HH24:MI:SS') - 30 seconds and " +
                                            "to_date('" + df_db.format(df.parse(testStart_timestamp)) + "','YYYY-MM-DD HH24:MI:SS') + 30 seconds ");
                          
                            if (rs!=null && rs.next())
                                    {
                                       test_no = rs.getInt(1);
                                    }
                            else {
                    rs = ps.executeQuery("SELECT nextval for Test_Seq FROM SYSDUMMY1");
                            if (rs!=null && rs.next())
                                    test_no = rs.getInt(1);
                            }
                          
                            //get exec_id
                            rs = ps.executeQuery("SELECT nextval for Exec_Seq FROM SYSDUMMY1");
                            if (rs!=null && rs.next())
                            {
                                    execId = rs.getInt(1);
                            }
               
                            rs.close();
                            rs = null;
                          
                          
                            //execution
                            String sqlcmd = "INSERT INTO Execution(Exec_ID,Test_No, Build_ID,sc_id, env_id,VU,TESTER,Levelload_Start,Levelload_End" +
                                            ",Driver_Interval,Test_Start,Test_End, Description, machine_id) " +
                                            "VALUES(" + execId + "," + test_no  + ","+ buildId + "," + scID + "," + envID + "," +
                                            vu + ",'" + tester + "',to_date('" + df_db.format(df.parse(levelloadStart_timestamp)) + "','YYYY-MM-DD HH24:MI:SS')," +
                                                            "to_date('" + df_db.format(df.parse(levelloadEnd_timestamp)) + "','YYYY-MM-DD HH24:MI:SS')," +
                                                            Config.getInstance().histoInterval + "," +
                                                            "to_date('" + df_db.format(df.parse(testStart_timestamp)) + "','YYYY-MM-DD HH24:MI:SS')," +
                                                            "to_date('" + df_db.format(df.parse(testStop_timestamp)) + "','YYYY-MM-DD HH24:MI:SS'),'" +
                                                            test_desc+ "',"+ machine_ID+")";
                            //System.out.println(sqlcmd);
                            ps.executeUpdate(sqlcmd);
                          
                          
                            for (i = 0; i< testcases.size(); i++)
                            {
                              //case
                              //Get case ID
                                    caseId = 0;
                                  
                                    rs = ps.executeQuery("SELECT nextval for Case_Seq FROM SYSDUMMY1");
                                    if (rs!=null && rs.next())
                                            caseId = rs.getInt(1);
                                    //ps.close();
                                    rs.close();
                                    rs = null;
                                  
                                    ArrayList casedetail = (ArrayList)testcases.get(i);
                                    String tc_desc = casedetail.get(0).toString();
                                sqlcmd = "INSERT INTO CASEINFO(CASE_ID, CASE_NAME, DESCRIPTION, EXEC_ID) VALUES(" +
                                caseId + ", '" + tc_desc.substring(tc_desc.indexOf(" "), tc_desc.lastIndexOf(":")).trim() + "', '"+ tc_desc.substring(tc_desc.indexOf(":")+1, tc_desc.lastIndexOf(" ")).trim()+"'," + execId + ")" ;
                               
                                //System.out.println(sqlcmd);
                                    ps.executeUpdate(sqlcmd);
                                  
                                    //result
                                    PreparedStatement ps1 = conn.prepareStatement("INSERT INTO TRESULT( CASE_ID, INTERVAL_SEQ" +
                                    ", LOOP_COUNT, AVG, MIN, MAX, TOTAL_SLEEPTIME,ERROR_COUNT, EXE_COUNT,  Exec_ID) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
                                  
                                    for (int j = 2; j<casedetail.size();j++) {
                                            //System.out.println(casedetail.get(j).toString());
                                            String tmp[] = casedetail.get(j).toString().split(" ");

                                            ps1.setInt(1, caseId);
                                            ps1.setInt(2, Integer.parseInt(tmp[0]));
                                            ps1.setInt(3, Integer.parseInt(tmp[1]));
                                            ps1.setInt(4, Integer.parseInt(tmp[2]));
                                            ps1.setInt(5, Integer.parseInt(tmp[3]));
                                            ps1.setInt(6, Integer.parseInt(tmp[4]));
                                            ps1.setInt(7, Integer.parseInt(tmp[5]));
                                            ps1.setInt(8, Integer.parseInt(tmp[6]));
                                            ps1.setInt(9, Integer.parseInt(tmp[7]));
                                            ps1.setInt(10, execId);
                                            ps1.addBatch();
                                    }
                                  
                      
                                    ps1.clearParameters();
                        ps1.executeBatch();
                      
                            }
                          
                    conn.commit();
                   
                   
                    conn.close();