• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Database Connection

 
smitha rai
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
rani bedi
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
smitha rai
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Prathima, I got it.
Smitha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic