aspose file tools
The moose likes JDBC and the fly likes Class.forName Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Class.forName Problem" Watch "Class.forName Problem" New topic
Author

Class.forName Problem

Dylan Stamatopolis
Greenhorn

Joined: Aug 04, 2003
Posts: 15
I've got a JSP page working perfectly here: /resin/project/
... and in my code, I load the Oracle and MySQL drivers like so:

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Class.forName("com.mysql.jdbc.Driver").newInstance();

However, I need to implement some of this code into a Servlet as well, located here: /resin/project/WEB-INF/classes/
... and did the same declarations like so:

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Class.forName("com.mysql.jdbc.Driver").newInstance();

... but got the following errors:
unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown:
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();


Yeah, I know... I should be catching that exception... BUT !... just wanted to clarify why this exception is being generated in the first place. Totally confused Ideas ?


/* the more you know, the less you need */
Dylan Stamatopolis
Greenhorn

Joined: Aug 04, 2003
Posts: 15
ps... here is my $CLASSPATH:

/root/resin/ts/lib/jsdk24.jar
/root/resin/ts/WEB-INF/lib/mysql-connector-java-3.0.9-stable-bin.jar
/root/resin/ts/WEB-INF/lib/ojdbc14.jar
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56185
    
  13

Yeah, I know... I should be catching that exception... BUT !... just wanted to clarify why this exception is being generated in the first place.


This is basic Java. If a method that you call declares that it throws an exception, you must either catch it or declare that your method throws it.

Since Class.forName() can throw ClassNotFoundException, you need to deal with it.

Btw, if you do catch it, be sure not to just eat the exception. In a servlet environment you should either let such an error propogate out to the container, or wrap it in a ServletException which propogates out. This way, the container can deal with the error as defined by the web application configuration.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Dylan Stamatopolis
Greenhorn

Joined: Aug 04, 2003
Posts: 15
Thanks Bear... I need some schooling

So, I had to pretty much catch every possible error from the Class.forName... my catches being:


Does this look correct !?
Dylan Stamatopolis
Greenhorn

Joined: Aug 04, 2003
Posts: 15
sorry for the lame post. the above "Catch" statements did work... but just inquiring if that is what the absolute solution is.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56185
    
  13

Except that you're doing exactly what I suggested not doing, which is "eating" the exception. You print out an error and then go merrily along your way as if nothing happened. Not good.
Dylan Stamatopolis
Greenhorn

Joined: Aug 04, 2003
Posts: 15
What would you suggest then !?

This class is running solo, and it will never be instantiated, extended... etc... so, I have no where to throw the exceptions to, and must handle them locally.

For instance, if an "InstantiationException" occurred... what should my lonely Servlet do with the exception other than print out the error ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56185
    
  13

See my previous response.

Servlets should never just "print out" and dismiss execptions. This is not an error like the user typing in invalid data. This type of exception means that something is severely wrong with your program or setup and your servlet should let the exception propogate outward so that it blows up spectacularly in your face.

By dismissing the exception you prevent any error handling set up in the web.xml file from handling the error in whatever way makes the most sense for the application.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class.forName Problem
 
Similar Threads
connecting error on jsp
Using jdbc from jsp with MySQL
Null pointer exception in con.createStatement(); (Struts 2 using netbeans6.1 and MYSQL)
Exception initiating Driver in Cron Job
connecting error on jsp