Question: My application uses a Mysql connection, which is obtained using this code:

```
public static void Connect(){
    try
    {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        String url = "jdbc:mysql://" + host + ":" + port + "/"+ db;
        ct = DriverManager.getConnection(url, user, pass);
        st = ct.createStatement();
        System.out.println("Csatlakozva....\n");
    }   
    catch (InstantiationException e) 
    {
        Main.textArea.setText("Error : Instantiation!");
        //System.err.println("Error : Instantiation");
        e.printStackTrace();
    }
    catch (IllegalAccessException e) 
    {
        Main.textArea.setText("Error : Illegális Behatolás!");
        //System.err.println("Error : Illegális Behatolás!");
        e.printStackTrace();
    } 
    catch (ClassNotFoundException e)
    {
        Main.textArea.setText("Error : Class Nem Található!");
        //System.err.println("Error : Class Nem Található!");
        e.printStackTrace();
    } 
    catch (SQLException e) 
    {
        Main.textArea.setText("Error : Adatbázis Nem Található!");
        //System.err.println("Error : Adatbázis Nem Található!");
        e.printStackTrace();
    }
}

```

If the MySQL database server is not running and therefore my app cannot open a connection, how do I make my app wait until a connection can be established?

Answer: 
In order to have your `Connect` method not return until a connection was actually obtained, you need to surround your existing try-block with a loop. Something like this:

```
public static void Connect() throws InterruptedException { // It isn't nice to block forever, so we will allow for interrupt
    for (;;) {
        try
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String url = "jdbc:mysql://" + host + ":" + port + "/"+ db;
            ct = DriverManager.getConnection(url, user, pass);
            st = ct.createStatement();
            System.out.println("Csatlakozva....\n");
            return; // Break out of loop because we got a connection - no exception was thrown
         }   ...

         // One of the exceptions happened. We will continue to loop so
         // we try to connect again until we are successful. We don't want
         // to retry too fast, because painful experience has taught us that
         // bad things can happen (full logs, 100% CPU, etc.).
         Thread.sleep(1);
    }
}

```

Exploring and understanding these sorts of low-level issues is a good exercise. However, I will just make a note here that (in my experience) most applications use a connection pool to manage database connections. Generally speaking, the connection pool would provide the functionality of blocking (usually for a limited time) until a connection can be obtained. In other words, the connection pool not only allows for reuse of previously-created connections, but also deal with connection retries when necessary.
I guess you have thread issue.

you can put the JDBC connection code to new thread

```
private class JDBCConnection extends Thread{

public void run(){
....
}

}

```