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
Solved

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running JavaFX on JDeveloper 12C 1 54
Android development question 2 54
sql import cannot be resolved jsp 3 45
How to configure empty element in XML Document parser? 15 36
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

839 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