Solved

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

Posted on 2006-06-21
3
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

724 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