1

I have been playing around with JDBC drivers. I would like some advice from the experience code masters out there. I was wondering if this is a good way to find if am connected to a database. If it is not, could someone let me know how would they do it?

Connection con = null;

    try {
         con = DriverManager.getConnection("jdbc:mysql://localhost/"+database+"?+user="+user+"&password="+password);
    } catch (SQLException e) {
         System.out.println(e.getMessage());
            if (con != null) {
                System.out.println("hello");
            } else {
                System.out.println("Not Connected!");
            }
    }

I would really appreciate all helpful comments. Many thanks in advance.

3 Answers 3

2

If the getConnection method returns normally, and does not throw an exception, then a connection to the database has been made. The logic within the catch clause is unrequired, because if an exception is caught a connection was not established.


The returned Connection object must be close()d (as many of the JDBC classes must, such as Statements and ResultSets for example). Assuming Java7, a convenient way to ensure the Connection is closed is using the try-with-resources statement:

try (Connection con = DriverManager.getConnection(...))
{
}
catch (final SQLException e)
{
}

As stated by SnakeDoc in the comments this may be impractical in production systems because establishing a connection to the database is a typically expensive operation.

Sign up to request clarification or add additional context in comments.

7 Comments

... this makes it rather un-reusable unless you litterally only need the db for a very small piece of code (it's possible!)
@SnakeDoc, it really depends on what is inside they try of course. Could be an long running loop for example. My intention was to highlight that the connection must be closed and this was a convenient away to achieve that.
Also assuming OP is not stuck with Java 6 or earlier.
I perfectly understood what hmjd was meaning
fair enough for small things i suppose. I like to have a "connection manager" of sorts that opens the db, and keeps it open during runtime, then closes it afterwards. The longest part usually when talking to a db, is opening the connection.
|
1

The JDBC API will perform these checks for you. Whenever you perform a database operation, such as opening a connection, executing a statement, it will check to see if the connection is valid. If not it will throw an exception.

For example, the method Connection.createStatement throws the following:

SQLException - if a database access error occurs or this method is called on a closed connection

All you have to do is some basic exception handling. Surrounding your JDBC calls with try-catch blocks is one way do this. You could also throw the exception and handle it somewhere else.

Comments

-1

That's definitely a good way of doing it! If conn can't be initialised it's either your connection string that is wrong or the database is down.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.