Solved

will the while break out of the loop or do i need another if

Posted on 2006-06-21
3
201 Views
Last Modified: 2010-03-31
i am concerned with this method.

if my counter reaches 4 i want it to terminate the application
not just break out of the while loop and go on with the code

do i need a break in the while or do i need an if to say if counter is greater than 4 System.exit???

thanks in advance annie



      public static Connection getDbConnection(String USERIDin, String PWDin, String SERVERin)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, InterruptedException
      {
            int dbctr = 0;
            while(dbctr <= 3)//do i need another if to say if it reaches 4 kill ps???
            {
                  try
                  {
                        Class.forName(COM_IBM_DB2_JDBC_APP_DB2DRIVER).newInstance();
                        conn = DriverManager.getConnection(JDBC_DB2 + SERVER, USERID, PWD);
                  }
                  catch(Exception e)
                  {                        
                        dbctr++;
                        
                        logError();
                        
                        try
                        {
                              Thread.sleep(10000);
                        }
                        catch(InterruptedException ie)
                        {
                              throw ie;
                        }
                  }
            }
            return conn;
      }
0
Comment
Question by:annie613
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
StillUnAware earned 50 total points
ID: 16950874
That code should be:

public static Connection getDbConnection(String USERIDin, String PWDin, String SERVERin)
          throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, InterruptedException
     {
          int dbctr = 0;
          while(dbctr <= 3)//do i need another if to say if it reaches 4 kill ps???
          {
               try
               {
                    Class.forName(COM_IBM_DB2_JDBC_APP_DB2DRIVER).newInstance();
                    conn = DriverManager.getConnection(JDBC_DB2 + SERVER, USERID, PWD);
                    return conn;
               }
               catch(Exception e)
               {                    
                    dbctr++;
                   
                    logError();
                   
                    try
                    {
                         Thread.sleep(10000);
                    }
                    catch(InterruptedException ie)
                    {
                         throw ie;
                    }
               }
          }
          System.exit(1); //some error code
          return null;
     }
0
 

Author Comment

by:annie613
ID: 16950890
sometimes having another set of eyes and ideas is a great advantage, i didnt even think about putitng the return conn in the try and that is what i was fearing about using the while loop. thanks for your help CHEERS
0
 
LVL 14

Expert Comment

by:StillUnAware
ID: 16950898
glad to help
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question