views:

179

answers:

5

hey all, I'm new to Java and was wondering if I define a method to return a database object

like

import java.sql.*;

public class DbConn {

    public Connection getConn() {
        Connection conn;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            if(System.getenv("MY_ENVIRONMENT") == "development") {
                String hostname = "localhost";
                String username = "root";
                String password = "root";
            }
            conn = DriverManager.getConnection("jdbc:mysql:///mydb", username, password);
            return conn;
        } catch(Exception e) {
            throw new Exception(e.getMessage());
        }

    }

}

if the connection fails when I try to create it what should I return? eclipse is telling me I have to return a Connection object but if it fails I'm not sure what to do.

thanks!

UPDATED CODE TO LET EXCEPTION BUBBLE:

public class DbConn {

    public Connection getConn() throws SQLException {
        Connection conn;
        String hostname = "localhost";
        String username = "root";
        String password = "root";

        Class.forName("com.mysql.jdbc.Driver").newInstance();
        if(System.getenv("MY_ENVIRONMENT") != "development") {
            hostname = "localhost";
            username = "produser";
            password = "prodpass";
        }
        conn = DriverManager.getConnection("jdbc:mysql:///mydb", username, password);
        return conn;

    }

}
+2  A: 

If an exception is thrown, there is no normal value returned from the method. Usually the compiler is able to detect this, so it does not even pester you with "return required" style warnings/errors. Sometimes, when it is not able to do so, you need to give an "alibi" return statement, which will in fact never get executed.

Redefining your method like this

public Connection getConn() {
    Connection conn = null;
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        if(System.getenv("MY_ENVIRONMENT") == "development") {
            String hostname = "localhost";
            String username = "root";
            String password = "root";
        }
        conn = DriverManager.getConnection("jdbc:mysql:///mydb", username, password);
    } catch(Exception e) {
        // handle the exception in a meaningful way - do not just rethrow it!
    }
    return conn;
}

will satisfy Eclipse :-)

Update: As others have pointed out, re-throwing an exception in a catch block the way you did is not a good idea. The only situation when it is a decent solution is if you need to convert between different exception types. E.g. a method called throws an exception type which you can not or do not want to propagate upwards (e.g. because it belongs to a proprietary library or framework and you want to isolate the rest of your code from it).

Even then, the proper way to rethrow an exception is to pass the original exception into the new one's constructor (standard Java exceptions and most framework specific exceptions allow this). This way the stack trace and any other information within the original exception is retained. It is also a good idea to log the error before rethrowing. E.g.

public void doSomething() throws MyException {
    try {
        // code which may throw HibernateException
    } catch (HibernateException e) {
        logger.log("Caught HibernateException", e);
        throw new MyException("Caught HibernateException", e);
    }
}
Péter Török
Eclipse is saying this method must return a type of Collection and has an error icon in the line with the method delcaration.
@beagleguy that is because there is a code path which will not result in a return or a throw. However, the code you posted is apparently not complete as it does not show such a code path.
Yishai
thanks Peter, I updated my code to remove the try / catch.. 2nd version look better?
@beagle, it is fine, though you could improve it a little if you eliminate `conn` and simply `return DriverManager.getConnection(...);` in the end. Also, you could factor out the setup of connection parameters into a separate method.
Péter Török
+1  A: 

This is exactly the situation where you should let the exception propagate up the call stack (declaring the method as throws SQLException or wrapping it in a application-specific exception) so that you can catch and handle it at a higher level.

That's the whole point of exceptions: you can choose where to catch them.

Michael Borgwardt
+5  A: 

You should just eliminate your entire try/catch block and allow exceptions to propagate, with an appropriate exception declaration. This will eliminate the error that Eclipse is reporting, plus right now your code is doing something very bad: by catching and re-throwing all exceptions, you're destroying the original stack trace and hiding other information contained in the original exception object.

Plus, what is the purpose of the line Class.forName("com.mysql.jdbc.Driver").newInstance();? You're creating a new mysql Driver object through reflection (why?) but you're not doing anything with it (why?).

JSBangs
If I don't have that line I keep getting the exception: General Exception: No suitable driver found for jdbc:mysql:///mydbis there a better way to get a mysql connection object?
@beagleguy, I see from Google that this method of creating is actually recommended. Huh. I guess the writers of jdbc have never heard of Dependency Injection.
JSBangs
I updated the code to remove any try catches... this look better?
+1  A: 

Never, ever, ever use a generic exception like that. If you don't have a ready made exception (in this case, an SQLException), make your own exception type and throw it. Every time I encounter something that declares that it "throws Exception", and it turns out that it does so because something it calls declares "throws Exception", and so on down the line, I want to strangle the idiot who started that chain of declarations.

Paul Tomblin
A: 

I'm sorry, but you shouldn't be writing code like this, even if you're new to Java.

If you must write such a thing, I'd make it more like this:

public class DatabaseUtils 
{

    public static Connection getConnection(String driver, String url, String username, String password) throws SQLException 
    {
        Class.forName(driver).newInstance();


        return DriverManager.getConnection(url, username, password);
    }
}

And you should also be aware that connection pools are the true way to go for anything other than a simple, single threaded application.

duffymo