0

I'm trying to create an application for checking in and out devices. So far I've had success but I'm trying to have the main index.jsp page that is called display a message like "connecting" until a connection with the database can actually be made and then display the actual login form. So something to the effect of.

Connection con = null;
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
boolean connected = false;

while(!connected) {
    try{
        String url = "jdbc:mysql://localhost:3306/test";
        con = DriverManager.getConnection(url,"username","password");
        connected = true;

        //Display form allowing user to authenticate login

    } catch(Exception e) {
        //Display Message "Attempting to connect to database"
    }
}

The problem I have is the same message will get repeated over and over on the web page but i just want it to display once and stay there until the connection is found and then be removed and replace with the login form. Any thoughts?

johnsoga
  • 15
  • 1
  • 5

3 Answers3

1

I suggest you use connection pooling.... it is quite easy to manage connections and the overheads will be reduced making your app efficient.

see the following:

Community
  • 1
  • 1
mykey
  • 575
  • 2
  • 10
  • 24
0

Any thoughts?

Yes.

You need to get your head around HOW stuff gets displayed to the user. The normal model is that browser sends an HTTP request to the server, the server creates a response consisting of an HTML page, and the browser displays the page. In this model, if you want the user to see the results, the server has to finish and send the response. Until it does, the user sees a blank screen (or the page he / she was on previously).

So to get a page that updates you have two options:

  • You can create a page that contains a <meta> tag that requests that the browser resends the request after a fixed delay; see http://en.wikipedia.org/wiki/Meta_refresh

  • You can embed javascript in the page to send an AJAX request to the server asking for an update. On getting a response, the javascript then needs to modify the DOM of the displayed page with the new information.

But in either case, it should be clear to you that you cannot do this in a single call to a single JSP. So you don't want a while loop in the JSP, or in the controller that dispatches to the JSP. The looping has to happen at the browser end.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

Change your code in this way

Connection con = null;
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
boolean connected = false;
int errors = 0;

while(!connected) {
    try{
        String url = "jdbc:mysql://localhost:3306/test";
        con = DriverManager.getConnection(url,"root","ikkakumon");
        connected = true;

        //Display form allowing user to authenticate login

    } catch(Exception e) {

        if (errors == 0) {
            //Display Message "Attempting to connect to database"
        }
        ++errors;

        Thread.sleep(1000); // wait a little before the next attempts
    }
}

So you display the error message only the first time. Then wait between one attempt and another.

However the loop is not the usual way to manage connections. Why do you choose this solution? What kind of exception do you receive?

dash1e
  • 7,677
  • 1
  • 30
  • 35
  • I don't receive any actual exceptions as long as the database is up everything goes through fine. But I'm essentially trying to make the error checking a little nicer. Currently the login form displayed first and if the database isnt up then when they submit it goes straight to error in sql page that tomcat shows showing the exception that happened etc... very ugly I'd rather just show them a connecting dialog until the database is up and then show them the form to login – johnsoga Apr 22 '12 at 23:09