Hello All,

Please find my code below where I am trying to make multiple connections ( It has some threading related code as well but I don't think that's the problem here). For some IP addresses, the results are getting inserted into the desired database (located at ZZ.Z.Z.ZZZ) but for some cases, I do see "No results found in this loop" message and I am wondering what I am doing wrong here. I do see that my loop is printing all the IP addresses using the following test statement
Java Code:
System.out.println("IP Address Just Completed is:" +RemoteIPAddress[i]);
I can also see that the value of "i" is incrementing properly. with the another test statement
Java Code:
System.out.println("Value of i is :" + i);
1) Do I need to add more error handling code here?

2) Is my usage of finally block correct and do I need to add something inside the catch block where I have mentioned "//ignore"?

Here is my code:

Java Code:
import javafx.concurrent.Task;
import javafx.event.ActionEvent;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import static com.Ostermiller.util.StringHelper.escapeSQL;
import static java.lang.Math.abs;

       
        
 public class ResultsReader extends Task<Integer> 
 {
        private static int NumberOfResultsThreads = 10;
private static int NumberOfResultsContactsToProcessPerThread = 1000;
private static int NumberOfRecordsToInsertAtATime = 100;
private static String TypeMask = "1,2,3,5";
private static String RemoteUser = "myusername";
private static String RemotePass = "mypass";
private static String MainUser = "Mainuser";
private static String MainPass = "mainpass";
private static int ESID = -15;
private int CurrModValue;
private static String IP[];
        
        
        private static int ESID = -15;
        
        
        private int CurrModValue;
        private static String IP[];
        
        
        
        
        // Task local reference to the controller class to call back to when complete       
        private EBMExtractorFXMLController QRVC;
        
        public void setQDVC(EBMExtractorFXMLController QRVC)
        {
           this.QRVC = QRVC;
        }

        public EBMExtractorFXMLController getQDVC()
        {
               return QRVC;
        }
  
    
    
        public ResultsReader(int CurrModValue) 
        {
            this.CurrModValue = CurrModValue;
        }
         
        public void act_StartProcessing() 
        {
            new Thread(this).start();

        }

        public void act_StopProcessing() 
        {
            this.cancel();                  
        }
      
               

         @Override protected Integer call() throws Exception 
         {
            int iterations = 0;
            
            int i = 0;
                             
            
           
            Statement SelectResultsStmt = null;
            Statement UpdateResultsStmt = null;
            Statement InsertRemoteResultsStmt = null;
            ResultSet SelectResultsRS = null;

           
            while(i<5) {
            try 
            {
                
                
                Connection[] MainConnection = new Connection[5];
                
                String[] RemoteIPAddress = new String[5];
                                
                RemoteIPAddress[0] = "aa.aa.a.aaa";
                RemoteIPAddress[1] = "bb.bb.b.bbb";
                RemoteIPAddress[2] = "cc.cc.c.ccc";
                RemoteIPAddress[3] = "dd.dd.d.ddd";
                RemoteIPAddress[4] = "ee.ee.e.eee";
                //RemoteIPAddress[5] = "ff.ff.f.fff";
                
                MainConnection[0] = DriverManager.getConnection("jdbc:mysql://" + RemoteIPAddress[0] + ":3306/test",RemoteUser,RemotePass);
                MainConnection[1] = DriverManager.getConnection("jdbc:mysql://" + RemoteIPAddress[1] + ":3306/test",RemoteUser,RemotePass);
                MainConnection[2] = DriverManager.getConnection("jdbc:mysql://" + RemoteIPAddress[2] + ":3306/test",RemoteUser,RemotePass);
                MainConnection[3] = DriverManager.getConnection("jdbc:mysql://" + RemoteIPAddress[3] + ":3306/test",RemoteUser,RemotePass);
                MainConnection[4] = DriverManager.getConnection("jdbc:mysql://" + RemoteIPAddress[4] + ":3306/test",RemoteUser,RemotePass);
                
                
                Connection connRemote = DriverManager.getConnection("jdbc:mysql://ZZ.Z.Z.ZZZ:3306/test",MainUser,MainPass); 
                 

                 String QueryString = " SELECT "
                                            // my table fields here
                                            + " FROM "
                                            + " sometable "
                                            ;
            
                
                SelectResultsStmt = MainConnection[i].createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                
                SelectResultsRS = SelectResultsStmt.executeQuery(QueryString);

                int CurrentBillingAmount = 0;
                              
                int CurrentRowCount = 0;
                String QueryStringInsertFromRemote = "";
                
                // Now do something with the ResultSet ....
                while ( SelectResultsRS.next( ) && !Thread.interrupted()) 
                {// While SelectResultsRS
                    CurrentRowCount++;
                    
                    // Allow thread to be canceled 
                    if (isCancelled()) 
                    {
                        // Do any additional clean up here - maybe update main DB of non-processed?     
                         updateMessage("Cancelled");
                         break;
                    }
                     
                    // When a progress is bound (.bind()) to this task - this will update the progress meter        
                    updateProgress(SelectResultsRS.getRow(), NumberOfResultsContactsToProcessPerThread);
                    updateMessage("Thread " + CurrModValue + " " + SelectResultsRS.getInt("DTSId_int"));
                  
                                  

                
                        
                    QueryStringInsertFromRemote = " INSERT INTO mytable "
                                                                    + " ( "	
                                                                    // My table field names here
                                                                    + " ) "
                                                                    + " VALUES "
                                                                    + " ( "
                                                                    // my table values goes here
                                                                    + " ) ";
                    
                    
                    SelectResultsRS.updateInt( "CallDetailsStatusId_ti", abs(ESID) );
                                     
                           
                   
                    InsertRemoteResultsStmt = connRemote.createStatement();
                    InsertRemoteResultsStmt.executeUpdate(QueryStringInsertFromRemote);

                    // reset string to empty
                    QueryStringInsertFromRemote = "";

                    SelectResultsRS.updateRow( );                        
                 
                }
                if(QueryStringInsertFromRemote.length() > 0)
                {
                    InsertRemoteResultsStmt = connRemote.createStatement();
                    InsertRemoteResultsStmt.executeUpdate(QueryStringInsertFromRemote);  
                    
                    QueryStringInsertFromRemote = "";
                    
                    SelectResultsRS.updateRow( );
                }
                
                

                if(CurrentRowCount == 0)
                {
                    // Write to log that there was nothing to process
                    System.out.println("No results found in this loop");
                                      
                    // Only sleep while listening for cancel task/thread calls
                    try 
                    {
                        Thread.sleep(100);
                    }
                    catch (InterruptedException interrupted) 
                    {
                        if (isCancelled()) 
                        {
                            System.out.println("QRC Cancelled");
                            // break;
                        }
                    }
                    
                    
                }
                
                    
            System.out.println("IP Address Just Completed is:" +RemoteIPAddress[i]);
                 
           
            } 
            catch (SQLException ex) 
            {
                 // handle any errors
                 System.out.println("SQLException: " + ex.getMessage());
                 System.out.println("SQLState: " + ex.getSQLState());
                 System.out.println("VendorError: " + ex.getErrorCode());
                 
            }
            finally 
            {
                

                if (SelectResultsRS != null) 
                {
                    try
                    {
                        SelectResultsRS.close();
                    }
                    catch (SQLException sqlEx) 
                    { 

                    } // ignore

                    SelectResultsRS = null;
                }

                if (SelectResultsStmt != null) 
                {
                    try 
                    {
                       SelectResultsStmt.close();
                    }
                    catch (SQLException sqlEx) 
                    { 

                    } // ignore

                    SelectResultsStmt = null;
                }

                if (UpdateResultsStmt != null) 
                {
                    try 
                    {
                       UpdateResultsStmt.close();
                    }
                    catch (SQLException sqlEx) 
                    { 

                    } // ignore

                    UpdateResultsStmt = null;
                }
                                        
                if (InsertRemoteResultsStmt != null) 
                {
                    try 
                    {
                       InsertRemoteResultsStmt.close();
                    }
                    catch (SQLException sqlEx) 
                    { 

                    } // ignore

                    InsertRemoteResultsStmt = null;
                }        
                 i++;
                 System.out.println("Value of i is :" + i);     
                                 
             
         }
            return iterations;
 
         }
         
 }