8

The code below fails on the line:

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

with the error:

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

The two printlns print:

Wed_Jun_22_11:18:51_PDT_2005
false

This makes me think the class exists and can be found. Also this exact same class works in an a non-servlet application.

I have rebooted everything multiple times and regenerated the application/servlet multiple times. All values have been hard coded to make it simple and short.

private static Connection getDBConnection() throws Exception {
    System.out.println(oracle.jdbc.driver.OracleDriver.BUILD_DATE);
    System.out.println(Class.class.desiredAssertionStatus());
    //load the driver
    Class.forName("oracle.jdbc.driver.OracleDriver");

    return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "SYSTEM", "pass");
}

full servlet that fails:

package servletClass_3;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class OneMoreBookStore
 */
@WebServlet("/OneMoreBookStore")
public class OneMoreBookStore extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    private static Connection getDBConnection() throws Exception {

        System.out.println(oracle.jdbc.driver.OracleDriver.BUILD_DATE);
        System.out.println(Class.class.desiredAssertionStatus());

        //load the driver
        Class.forName("oracle.jdbc.driver.OracleDriver");
        return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "SYSTEM", "pass");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try
        {
            Connection con = getDBConnection();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

}

This application works:

package servletClass_3;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnect {

    private static Connection getDBConnection() throws Exception {
        System.out.println(oracle.jdbc.driver.OracleDriver.BUILD_DATE);
        System.out.println(Class.class.desiredAssertionStatus());

        //load the driver
        Class.forName("oracle.jdbc.driver.OracleDriver");
        return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "SYSTEM", "pass");
    }
    public static void main(String[] args) {
        try
        {
            Connection con = getDBConnection();
            System.out.println("connection worked");
            con.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

}

I'm using:

  • Eclipse JavaEE 1.4.2
  • Tomcat 7
  • jdk1.7
  • Oracle 11g R2
  • Windows 7 64bit
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Geoff
  • 83
  • 1
  • 1
  • 3
  • 2
    Are you sure that you have your oracle-xx.jar in your web application's /WEB-INF/lib directory or ${tomcat.install.dir}/lib directory? It seems like the oracle driver jar is missing from the classpath. – Paulius Matulionis Jun 14 '12 at 21:19

3 Answers3

12

Probably you aren't deploying the oracle driver with your application.

You have several options:

  • You can place the driver jars in your WEB-INF/lib folder
  • You export it with your application. -> Right Click on Project -> Build Path-> Configure Build Path... -> Order and Export -> Check the drivers.
  • Place the driver jars in a shared or library extension folder of your application server. (You should go with option one or two though.)
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Udo Held
  • 12,314
  • 11
  • 67
  • 93
  • 2
    Placing the ojdbc14.jar file in the WEB-INF/lib folder fixed the problem! I still don't understand why the oracle.jdbc.driver.OracleDriver.BUILD_DATE worked, but on the next line the class was not found. Thanks – Geoff Jun 14 '12 at 23:15
  • I think I figured out why one call to the OracleDriver works and the other doesn't. The first call is compiled into the application and ojdbc14.jar is in the build path, so it works. The second call is made at run time so it needs to be in the path when java runs. – Geoff Jun 14 '12 at 23:52
9

You must include the ojdbc6.jar file in the Deployment Assembly of the Project...

  1. select the web project which contains the jsp file...

  2. select Project tab in the menu bar in Eclipse

  3. select properties in the drop down menu

  4. select Deployment Assembly

  5. Add your ojdbc6.jar file in it.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
sri
  • 91
  • 1
  • 2
0

Try this, change the oracle.jdbc.driver.OracleTypes to oracle.jdbc.OracleTypes

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
J_K
  • 93
  • 1
  • 7