tags:

views:

41

answers:

2

I am able to connect to Oracle 10g (using ojdbc14.jar driver) with java. But when I use the same code in a servlet or file with .jsp extension, I am getting class not found exception. I am not able to understand why this is happening. Do we have different connection strings for JDBC in java and jsp? This is what I use to connect to oracle iwith both, java and jsp:

Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:xe";
//Xe being the database name
String usr = "username";
String pwd = "pwd";

Works fine with java but gives error with jsp.

+1  A: 

There should be no real difference between the two. Is the driver jar in your WEB-INF/lib/ subdirectory? A class not found exception typically means your jar wasn't found on the classpath.

wds
The drivers jar is included in the class path. If I am creating a java file in same project, it works fine. Problem is with jsp only.
Logan
Which server are you using? And what path within the server folder have you placed the ojdbc14.jar ?
JoseK
@Logan - that's not what he asked you. Is the JAR in WEB-INF/lib? If you're using a system CLASSPATH, you need to know that your app server ignores it.
duffymo
@Logan: are you running the application server by launching from your IDE somehow? Perhaps it is a configuration problem.
wds
I have imported the jar in project and am not using system classpath.
Logan
@wds: yes the jar is in the Web-Inf/lib/folder
Logan
@Logan: just to make sure, that's WEB-INF upper case. It sometimes matters. Run `jar tf` on the war you made and check whether it really does include everything correctly.
wds
+2  A: 

If you're using a CLASSPATH environment variable, your app server ignores it. That's probably why it "works" with Java but not with JSPs.

I'd advise you to not rely on CLASSPATH that way. Learn how to set it properly for every situation.

I'd also advise against putting scriptlet code in JSPs. This will grow to be a maintenance nightmare in a short time.

If you must put database calls in a JSP, learn how to use JSTL and its <sql> tags.

duffymo