return statement issue

I am trying to get my jdbc method (with boolean data type) to compile but I get compile error. I seem to have a problem with my return statement:

import java.io.*;

import java.text.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.sql.*;

publicclass UserDB

{

publicstaticboolean isMatch(String lastname)throws SQLException

{

try

{

Class.forName("org.gjt.mm.mysql.Driver");

String dbURL ="jdbc:mysql://localhost/dbhere";

String username ="namehere";

String password ="passwordhere";

Connection connection = DriverManager.getConnection(dbURL, username, password);

Statement stmt = connection.createStatement();

ResultSet results = stmt.executeQuery("SELECT lastname from user where lastname = '" + lastname +"'");

boolean myhit = results.next();

results.close();

stmt.close();

return myhit;

}

catch(ClassNotFoundException e)

{

System.out.println("Database driver not found.");

}

catch(SQLException e)

{

System.out.println("Error opening the db connection: " + e.getMessage());

}

}

}

Error message:

prompt>javac UserDB.java

UserDB.java:34: missingreturn statement

}

^

1 error

Please advise what I am doing wrong?

[2495 byte] By [oaklandara] at [2007-10-3 5:10:18]
# 1

The problem is that in your code if you have a SQLException for example you catch it (and output a message) but then what? The code continues execution to the end of the method. Except there is no return at the end of the method so it's not valid.

You could do one of the following.

If you catch an exception you could return false. Or you could simply not catch the exception here.

cotton.ma at 2007-7-14 23:16:36 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Thanks, it worked like this:

catch(ClassNotFoundException e)

{

System.out.println("Database driver not found.");

return false;

}

catch(SQLException e)

{

System.out.println("Error opening the db connection: " + e.getMessage());

return false;

}

"Or you could simply not catch the exception here. "

If I wanted to do it this way without catching the exception how would I not catch it?

oaklandara at 2007-7-14 23:16:36 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
Remove the whole try/catch blocks.
cotton.ma at 2007-7-14 23:16:36 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
PS Your code is quite lacking. You aren't closing your connection anywhere for starters. That is a BIG no-no and can lead to serious problems.What you should be doing is having the try/catch and a finally block where you close your statement and connection.
cotton.ma at 2007-7-14 23:16:36 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

If I remove the try catch like this without it:

public static boolean isMatch(String lastname) throws SQLException

{

Class.forName("org.gjt.mm.mysql.Driver");

.......rest of db connection here etc

}

Error message:

Tryc.java:15: unreported exception java.lang.ClassNotFoundException; must be cau

ght or declared to be thrown

Class.forName("org.gjt.mm.mysql.Driver");

^

1 error

If I remove try and catch along with the method throw:

public static boolean isMatch(String lastname)

{

Class.forName("org.gjt.mm.mysql.Driver");

....... rest of db connection here etc

}

I get alot of error messages:

ses\data>javac Tryc.java

Tryc.java:15: unreported exception java.lang.ClassNotFoundException; must be cau

ght or declared to be thrown

Class.forName("org.gjt.mm.mysql.Driver");

^

Tryc.java:19: unreported exception java.sql.SQLException; must be caught or decl

ared to be thrown

Connection connection = DriverManager.getConnection(dbURL, username, pas

sword);

^

Tryc.java:20: unreported exception java.sql.SQLException; must be caught or decl

ared to be thrown

Statement stmt = connection.createStatement();

....

7 errors

Please advise if possible?

oaklandara at 2007-7-14 23:16:36 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

Or rewrite your code like this:

import java.io.*;

import java.text.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.sql.*;

public class UserDB

{

public static boolean isMatch(String lastname) throws SQLException

{

boolean myhit = false;

Connection connection = null;

Statement stmt = null;

ResultSet results = null;

try

{

Class.forName("com.mysql.jdbc.Driver");

String dbURL = "jdbc:mysql://localhost/dbhere";

String username = "namehere";

String password = "passwordhere";

connection = DriverManager.getConnection(dbURL, username, password);

stmt = connection.createStatement();

results = stmt.executeQuery("SELECT lastname from user where lastname = '" + lastname + "'");

myhit = results.next();

}

catch(Exception e)

{

e.printStackTrace();

}

finally

{

close(results);

close(stmt);

close(conn);

}

return myhit;

}

public static void close(ResultSet rs)

{

try

{

if (rs != null)

rs.close();

}

catch (Exception e)

{

e.printStackTrace();

}

}

public static void close(Statement stmt)

{

try

{

if (stmt != null)

stmt.close();

}

catch (Exception e)

{

e.printStackTrace();

}

}

public static void close(Connection conn)

{

try

{

if (conn != null)

conn.close();

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

%

duffymoa at 2007-7-14 23:16:36 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7
hardwired connection URL and driver class? Not a very flexible way to do it. Externalize those from your code.%
duffymoa at 2007-7-14 23:16:36 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8
Thanks so much for your help.I will now attempt to externalize the connection URL and driver class as you suggested. I assume I will need to put that info in my web.xml file?
oaklandara at 2007-7-14 23:16:36 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9
As an init parameter? that might work.A better thing to do is to use a connection pool. put all that info in the container configuration and just do a jndi lookup. that way changing databases is a configuration issue.%
duffymoa at 2007-7-14 23:16:36 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 10
duffymo and cotton,Thanks for all your time and help on this!
oaklandara at 2007-7-14 23:16:36 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...