This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JDBC and the fly likes Database Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Database Connection" Watch "Database Connection" New topic
Author

Database Connection

smitha rai
Greenhorn

Joined: Jun 29, 2001
Posts: 18
Hi,
I am getting the following errror for my code.
JDBCMsAccessConnection.java:23: catch not reached.
} catch(ClassNotFoundException e) {
^
JDBCMsAccessConnection.java:27: catch not reached.
} catch(SQLException sqlex) {
^
JDBCMsAccessConnection.java:68: catch not reached.
} catch(ClassNotFoundException e) {
^
JDBCMsAccessConnection.java:71: catch not reached.
} catch(SQLException sqlex) {
^
4 errors
Can any one suggest me, where is my mistake?
Thanks
Smitha
==========================================
My code is :
package DatabaseConnection;
import java.sql.*;
class JDBCMsAccessConnection {
protected static Connection getAccessConnection() throws Exception {
String url = "jdbcdbc:data";
String uid = "";
String pwd = "";
Connection conn = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection (url,uid,pwd);
} catch(Exception e) {
System.err.print("Exception: Occurred while Establishing Connection ---> method getAccessConnection() " + e);
System.err.println(e.getMessage());
throw e;
} catch(ClassNotFoundException e) {
System.err.print("ClassNotFoundException: Occurred while Establishing Connection ---> method getAccessConnection() " + e);
System.err.println(e.getMessage());
throw e;
} catch(SQLException sqlex) {
System.err.print("Exception: Occurred while performing Database Operations ---> method getAccessConnection() " + sqlex);
System.err.println(sqlex.getMessage());
throw sqlex;
}
return conn;
}
protected void closeAll(Statement stmt, ResultSet rs, Connection conn) {
try {
stmt.close();
rs.close();
conn.close();
} catch(SQLException e){
System.out.println("Exception Occurred while Closing ---> method closeAll() " + e);
}
}
public static void main (String args[]) {
JDBCMsAccessConnection joc = new JDBCMsAccessConnection();
try {
Connection con = joc.getAccessConnection();
Statement stmt = con.createStatement ();
String query = "Select * from coffees";
ResultSet rs = stmt.executeQuery (query);
while (rs.next()) {
System.out.println(rs.getString("cofname") + " " + rs.getInt(2) + " " +
rs.getFloat(3)+ " " + rs.getInt(4) + " " + rs.getInt(5));
} // ** End of While
joc.closeAll();
} catch(Exception e) {
System.err.print("Exception: Occurred while Establishing Connection ---> method Main() " + e);
System.err.println(e.getMessage());
} catch(ClassNotFoundException e) {
System.err.print("ClassNotFoundException: Occurred while Establishing Connection ---> method getAccessConnection() " + e);
System.err.println(e.getMessage());
} catch(SQLException sqlex) {
System.err.print("Exception: Occurred while performing Database Operations ---> method Main() " + sqlex);
System.err.println(sqlex.getMessage());
}
} // ** End of Main
} // ** End of Class
asteriod
Greenhorn

Joined: Jul 03, 2001
Posts: 2
Hi ! Smitha ,
Just remove the stuff shown below in your code.
catch(Exception e) {
System.err.print("Exception: Occurred while Establishing Connection ---> method Main() " + e);
System.err.println(e.getMessage());
}

Because all exceptions will be first handled by this handler and
more specific handlers willnot get a chance to catch the more specific handlers listed below.

Hope this will help .
Dayanand

Originally posted by smitha rai:
Hi,
I am getting the following errror for my code.
JDBCMsAccessConnection.java:23: catch not reached.
} catch(ClassNotFoundException e) {
^
JDBCMsAccessConnection.java:27: catch not reached.
} catch(SQLException sqlex) {
^
JDBCMsAccessConnection.java:68: catch not reached.
} catch(ClassNotFoundException e) {
^
JDBCMsAccessConnection.java:71: catch not reached.
} catch(SQLException sqlex) {
^
4 errors
Can any one suggest me, where is my mistake?
Thanks
Smitha
==========================================
My code is :
package DatabaseConnection;
import java.sql.*;
class JDBCMsAccessConnection {
protected static Connection getAccessConnection() throws Exception {
String url = "jdbc dbc:data";
String uid = "";
String pwd = "";
Connection conn = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection (url,uid,pwd);
} catch(Exception e) {
System.err.print("Exception: Occurred while Establishing Connection ---> method getAccessConnection() " + e);
System.err.println(e.getMessage());
throw e;
} catch(ClassNotFoundException e) {
System.err.print("ClassNotFoundException: Occurred while Establishing Connection ---> method getAccessConnection() " + e);
System.err.println(e.getMessage());
throw e;
} catch(SQLException sqlex) {
System.err.print("Exception: Occurred while performing Database Operations ---> method getAccessConnection() " + sqlex);
System.err.println(sqlex.getMessage());
throw sqlex;
}
return conn;
}
protected void closeAll(Statement stmt, ResultSet rs, Connection conn) {
try {
stmt.close();
rs.close();
conn.close();
} catch(SQLException e){
System.out.println("Exception Occurred while Closing ---> method closeAll() " + e);
}
}
public static void main (String args[]) {
JDBCMsAccessConnection joc = new JDBCMsAccessConnection();
try {
Connection con = joc.getAccessConnection();
Statement stmt = con.createStatement ();
String query = "Select * from coffees";
ResultSet rs = stmt.executeQuery (query);
while (rs.next()) {
System.out.println(rs.getString("cofname") + " " + rs.getInt(2) + " " +
rs.getFloat(3)+ " " + rs.getInt(4) + " " + rs.getInt(5));
} // ** End of While
joc.closeAll();
} catch(Exception e) {
System.err.print("Exception: Occurred while Establishing Connection ---> method Main() " + e);
System.err.println(e.getMessage());
} catch(ClassNotFoundException e) {
System.err.print("ClassNotFoundException: Occurred while Establishing Connection ---> method getAccessConnection() " + e);
System.err.println(e.getMessage());
} catch(SQLException sqlex) {
System.err.print("Exception: Occurred while performing Database Operations ---> method Main() " + sqlex);
System.err.println(sqlex.getMessage());
}
} // ** End of Main
} // ** End of Class


Love you Hamesha
rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
The Exception class is pretty high in the Throwable class hierarchy. So in addition to the ClassNotFoundException, SQLException in your catch statements, it (Exception) will catch numerous other types. According to the compiler if you are able to catch all the eception in ur first catch statemnt then other catch statements are never reached.
So your catch statement
catch(Exception e)
should be the last one in the list of catch statements.
Generally speaking, your exception handlers should be more specialized. Handlers that can catch most or all exceptions are typically useless for error recovery because the handler has to determine what type of exception occurred anyway to determine the best recovery strategy.


Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
smitha rai
Greenhorn

Joined: Jun 29, 2001
Posts: 18
Thanks Prathima, I got it.
Smitha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Database Connection