Tomcat not connecting to Oracle

I can connect to a remote Oracle 9i database with no problems using JSP Scriptlet with JRun on my Windows Web

Server IIS:Class.forName("oracle.jdbc.driver.OracleDriver");

Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@abcdef:1234:xyz","scott","tiger");

....

On the same Web server, I have Tomcat 4.1.27 and I use the same connection code in a scriptlet in a JSP page but it does not connect.

It gives me errors such as

exception:

org.apache.jasper.JasperException

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)

javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

root cause:

java.lang.NullPointerException

org.apache.jsp.mypage_jsp._jspService(mypage_jsp.java:79)

org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)

javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)

javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

Please advise.

[1542 byte] By [teser2a] at [2007-11-26 19:52:08]
# 1

That code will either throw an exception (NOT a null pointer exception) or work. Show us the scriptlet please.

Better yet, also extract the file mypage_jsp.java from the Tomcat work directory and show us what's on line 79.

My bet would be that you're "handling" exceptions, but failing to correctly deal with the eventuality that the driver could not be loaded or the connection could not be established - in which case doubtless this is a matter of not having copied the JDBC driver into the common/lib directory.

dcmintera at 2007-7-9 22:42:43 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Thanks,

Here is what I have that gives the error messages I posted earlier. It seems to give the same message if I comment or uncomment out the Statement and ResultSet object references:

<%@ page import="java.sql.*"%>

<%

try

{

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@abcdef:1234:xyz", "scott","tiger");

//Statement statement = connection.createStatement();

//ResultSet resultset = statement.executeQuery("Select * from myTable");

}

catch (SQLException se)

{

out.println("Error: " + se);

}

catch (Exception e)

{

out.println("Error: " + e);

}

finally

{

//statement.close();

//resultset.close();

connection.close();

}

%>

If I do the below it gives me an error saying: java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

<%@ page import="java.sql.*"%>

<%

try

{

Class.forName("oracle.jdbc.driver.OracleDriver");

}

catch (Exception e)

{

out.println("Error: " + e);

}

%>

Please advise if I should tell the Server Admin to add the Oracle JDBC driver to common/lib directory (and set the classpath) or am I doing something wrong?

I originally thought it would fetch the Oracle JDBC driver from JRun because it was on the same server.

teser2a at 2007-7-9 22:42:43 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

The exception shows, that the Oracle driver is not in the classpath.

If you don't want to rely on the administrator, include the Jar with the Oracle sql classes in the lib-directory (under WEB-INF) of your application. It is than automatically in the classpath and jsp and classes can access it. (a restart of the server might be necessary)

g_magossa at 2007-7-9 22:42:43 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

The JSP you posted won't compile. The connection reference in the finally block is out of scope. I'm extremely skeptical that that's producing a NullPointerException instead of a Jsp compilation error.

The second example fails because, as I suspected, you haven't got the JAR in the classpath. The best place to put it is in the common/lib directory, but as the other poster indicated you can put it into WEB-INF/lib instead since you're creating the connection directly (a better practice is to use a DataSource but I think you have enough problems to be going on with).

D.

dcmintera at 2007-7-9 22:42:44 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

Thanks!

I am working with Server Admins to get it in the common/lib.

Why is the finally block out of scope? Is it because the try attempt (with Class.forName and Connection class) did not work and the object reference never got created in the heap?

try

{

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@abcdef:1234:xyz", "scott","tiger");

//Statement statement = connection.createStatement();

//ResultSet resultset = statement.executeQuery("Select * from myTable");

}

........

finally

{

//statement.close();

//resultset.close();

connection.close();

}

To avoid that I would do it like this?

Connection connection = null;

//Statement statement = null;

//ResultSet resultset = null;

try

{

Class.forName("oracle.jdbc.driver.OracleDriver");

connection = DriverManager.getConnection("jdbc:oracle:thin:@abcdef:1234:xyz", "scott","tiger");

//statement = connection.createStatement();

//resultset = statement.executeQuery("Select * from myTable");

}

........

finally

{

//statement.close();

//resultset.close();

connection.close();

}

teser2a at 2007-7-9 22:42:44 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

> Thanks!

> I am working with Server Admins to get it in the

> common/lib.

>

> Why is the finally block out of scope?

That makes no sense. The block is defined by its scoping operators; it can't be "out of scope". It's the connection reference that I'm talking about; it is created in the try block's scope - as soon as execution reaches the end of the try block's scope the reference is no longer available (and anything it pointed to will be a candidate for garbage collection if nothing else points to it.

Scoping operators (curly braces) define the extent of the scopes, and they can be nested. For example:

public void foo() {

// A

try {

// B

} catch(Exception e) {

// C

} finally {

// D

}

}

Any variables declared in scope A are available to scopes B, C, and D. Variables declared within these scopes are not available elsewhere. Variables declared (as with the Exception reference e) within the header to a catch block or within the header to a for block are available within the following scope (C in this case) and not elsewhere.

> Is it because

> the try attempt (with Class.forName and Connection

> class) did not work and the object reference never

> got created in the heap?

No. The reference (connection) exists on the stack. The reference is created when execution reaches the local variable declaration of the try block. It will be removed from the stack when execution reaches the end of the try block, and it is therefore no longer available when execution reaches the finally block.

The code you post is more correct, however, because the connection reference is created in the enclosing scope (A in the example above) and is therefore available within the finally block.

On the other hand it's still broken; a failure to create the Connection object will cause an exception, leaving the connection reference null and causing a null pointer exeception in your finally block.

Needless to say, posting code that can't compile (your first example) doesn't help us to diagnose your problems.

Anyway, your finally block should look like this:

finally {

// Need to close statements and connections similarly!

if( connection != null ) connection.close();

}

dcmintera at 2007-7-9 22:42:44 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7
Wow, thanks for all your time!I appreciate you sharing all your valuable knowledge.
teser2a at 2007-7-9 22:42:44 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...