Solved

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

Posted on 2006-06-21
3
199 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now