5

I want to connect to database in java web application.I wrote this code in servlet and i add related jar file(ojdbc) but when i run it,it gives me this error: java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

public class DBConnection extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public DBConnection() {
        super();
        // TODO Auto-generated constructor stub
    }

    public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        Connection con = null;  

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
              con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.101.84:1521:orcl","XXXX","XXXX");
              if(con!=null)
                 System. out.println("Connection Successful");
              else
                  System.  out.println("error");

        }
        catch (Exception e) {
            //System.out.println(e);
        }
...

what should i do?

5
  • 1
    Where did you put the JAR ? Put classes12.jar under WEB-INF/lib . Commented Jul 23, 2013 at 7:23
  • Do you have that jar in the class path ? Where do you put that jar ? Which application server you are using ? Commented Jul 23, 2013 at 7:24
  • 1
    Read this for more. Commented Jul 23, 2013 at 7:24
  • I put my jar in Referenced library,I right-click in my project-properties-java build path-add external... Commented Jul 23, 2013 at 7:25
  • You have to put the relevant jars in the server WEB-INF/lib folder too . It will be picked up from that location during runtime. Commented Jul 23, 2013 at 7:26

4 Answers 4

9

I was having the same issue. What you need is to include the ojdbc6.jar file in the Deployment Assembly of the Project:

  1. Right click on the project
  2. select "Properties"
  3. Select "Deployment Assembly" tab
  4. Add your ojdbc6.jar file in it..

...and the problem is solved.

Sign up to request clarification or add additional context in comments.

Comments

0

You can try this by adding your jar of oracle driver into classpath environment variable just for testing purpose. or you can also load it at runtime by supplying the library into endorsed directory of your server (TOMCAT) otherwise you can copy your library file into common lib of your application server . It's just related to the oracle driver library which is not loading at startup of your application..

Comments

0

I have faced the same problem but got it fixed Right click on the project->properties and add your jar(ojdbc6 or 14) file in the deployment assembly then it will work...

Comments

-1

I was having the same issue and what you need is

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

1)Right click on the project

2)select "Properties"

3)Select "Deployment Assembly" tab

4)Add your ojdbc6.jar file in it..

and the problem solve.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.