title: Execute a SQL query in a loop
body: <p>I have this scenario. I will trigger a job in the server and as soon as the job is triggered an entry will be made into the job table with Execution_status_code as 1. I need to wait for some time say 5 mins and recheck the Execution_status_code value. As soon as the value is changed to 2, I need to proceed further. </p>
  
  <p>I am using an existing connection for connecting to the database. I need to execute the SQL and if the SQL output is In progress, I need to wait for some time and then again execute the statement. Do this until the SQL output is success, until then keep waiting. </p>
  
  <p>Below is the code I have tried.</p>
  
  <pre><code>Thread t = new Thread();
  java.sql.Connection conn_javaComp = (java.sql.Connection)globalMap.get("conn_tNetezzaConnection_1");
  java.sql.Statement st = null;
  java.sql.ResultSet rs = null;
  String check = null;
  String dbquery_javaComp = "select case when EXECUTION_STATUS_CODE = 2 then 'Success' when EXECUTION_STATUS_CODE = 1 then 'In progress' else 'Failure' end as EXECUTION_STATUS_CODE from JOB_BKUP_NCR where JOB_TYPE_CODE="+context.JobTypeCode+" and Load_id = (select max(load_id) from JOB_BKUP_NCR where job_type_code="+context.JobTypeCode+") and START_DATETIME = (select max(START_DATETIME) from JOB_BKUP_NCR where job_type_Code="+context.JobTypeCode+")";
  try 
      {
          do 
          {
              st = conn_javaComp.createStatement();
              rs = st.executeQuery(dbquery_javaComp);
              if(rs.next()) 
              {
              check = rs.getString(1);
              System.out.println(check);
              if (check.equalsIgnoreCase("In Progress"))
              {
                  t.sleep(1000);
                  System.out.println("thread executed1");
                  System.out.println(dbquery_javaComp);
                  System.out.println(check);
              } 
              } 
              else {
              System.out.println(" No data found");
              }
          }while (!"Success".equals(check));
  
      }
  catch (Exception e) {
      e.printStackTrace();
  } finally {
      try {
          if( rs != null) 
              rs.close();
          if( st!= null)
              st.close();
      } 
      catch (Exception e1) {
          e1.printStackTrace();
      }
  }
  </code></pre>
  
  <p>The output i am getting is 'In Progress'. The loop is struck at In progress even after i change the value in the database. I am not sure where i am doing wrong. Any suggestions?</p>