25

I wrote a Java Servlet program but when I run it, it was showing the Exception

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

My code is

package skypark;

import java.io.*;
import javax.servlet.*;
import java.text.*;
import javax.servlet.http.*;
import java.sql.*;
import java.sql.Date;

public class Registration extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public static Connection prepareConnection() throws ClassNotFoundException, SQLException {
        String dcn = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@JamesPJ-PC:1521:skypark";
        String usname = "system";
        String pass = "tiger";
        Class.forName(dcn);
        return DriverManager.getConnection(url, usname, pass);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();

        try {
            String phone1, dofb, date1, month, year, uname, fname, lname, address, city, state, country;
            String pin, email, password, gender, lang, qual, relegion, privacy, hobbies, fav;

            uname = req.getParameter("uname");
            fname = req.getParameter("fname");
            lname = req.getParameter("lname");
            date1 = req.getParameter("date");
            month = req.getParameter("month");
            year = req.getParameter("year");
            address = req.getParameter("address");
            city = req.getParameter("city");
            state = req.getParameter("state");
            country = req.getParameter("country");
            pin = req.getParameter("pin");
            email = req.getParameter("email");
            password = req.getParameter("password");
            gender = req.getParameter("gender");
            phone1 = req.getParameter("phone");

            lang = "";
            qual = "";
            relegion = "";
            privacy = "";
            hobbies = "";
            fav = "";

            dofb = date1 + "-" + month + "-" + year;
            int phone = Integer.parseInt(phone1);
            DateFormat formatter;
            java.util.Date dob;
            formatter = new SimpleDateFormat("dd-MM-yy");
            dob = formatter.parse(dofb);

            Connection con = prepareConnection();
            String Query = "Insert into regdetails values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement ps = con.prepareStatement(Query);

            ps.setString(1, uname);
            ps.setString(2, fname);
            ps.setString(3, lname);
            ps.setDate(4, (Date) dob);
            ps.setString(5, address);
            ps.setString(6, city);
            ps.setString(7, state);
            ps.setString(8, country);
            ps.setString(9, pin);
            ps.setString(10, lang);
            ps.setString(11, qual);
            ps.setString(12, relegion);
            ps.setString(13, privacy);
            ps.setString(14, hobbies);
            ps.setString(15, fav);
            ps.setString(16, gender);

            int c = ps.executeUpdate();

            String query = "insert into passmanager values(?,?,?,?)";
            PreparedStatement ps1 = con.prepareStatement(query);

            ps1.setString(1, uname);
            ps1.setString(2, password);
            ps1.setString(3, email);
            ps1.setInt(4, phone);

            int i = ps1.executeUpdate();

            if (c == 1 || c == Statement.SUCCESS_NO_INFO && i == 1 || i == Statement.SUCCESS_NO_INFO) {
                out.println("<html><head><title>Login</title></head><body>");
                out.println("<center><h2>Skypark.com</h2>");
                out.println("<table border=0><tr>");
                out.println("<td>UserName/E-Mail</td>");
                out.println("<form action=login method=post");
                out.println("<td><input type=text name=uname></td>");
                out.println("</tr><tr><td>Password</td>");
                out.println("<td><input type=password name=pass></td></tr></table>");
                out.println("<input type=submit value=Login>");
                out.println("</form></body></html>");
            } else {
                out.println("<html><head><title>Error!</title></head><body>");
                out.println("<center><b>Given details are incorrect</b>");
                out.println(" Please try again</center></body></html>");
                RequestDispatcher rd = req.getRequestDispatcher("registration.html");
                rd.include(req, resp);
                return;
            }
        } catch (ClassNotFoundException cnfe) {
            out.println("<html><head><title>Error!</title><body>");
            out.println("<b><i>Class not found " + cnfe + "</i></b>");
            out.println("</body></html>");
        } catch (SQLException sqle) {
            out.println("<html><head><title>Error!</title><body>");
            out.println("<b><i>Unable to process try after some time Sql error</i></b>");
            out.println("</body></html>");
        } catch (ParseException e) {
            out.println("<html><head><title>Error!</title><body>");
            out.println("<b><i>Unable to process Parseint exc " + e + "</i></b>");
            out.println("</body></html>");
        }

        out.flush();
        out.close();
    }
}

My class path is :

C: \Windows\ system32 > echo % classpath %
  E: \app\ JamesPJ\ product\ 11.2.0\ dbhome_1\ oui\ jlib\ classes12.jar;
E: \app\ JamesPJ\ product 11.2.0\ dbhome_1\ jlib\ orai18n.jar;
E: \app\ JamesPJ\ product\ 11.2.0\ dbhome_1\ jdbc\ lib\ ojdc6_g.jar;
E: \app\ JamesPJ\ product\ 11.2.0\ dbhome_1\ BIN;
C: \Program Files\ Java\ jdk1.7.0_09\ bin;
C: \Users\ JamesPJ\ Documents;
E: \app\ JamesPJ\ product\ 11.2.0\ dbhome_1\ jdbc\ lib;
E: \app\ JamesPJ\ product\ 11.2.0\ dbhome_1\ jlib;
E: \app\ JamesPJ\ product\ 11.2.0\ dbhome_1\ jdbc\ lib\ ojdbc6.jar;
E: \app\ JamesPJ\ product\ 11.2.0\ dbhome_1\ oc4j\ jdbc\ lib\ orai18n.jar;
E: \app\ JamesPJ\ product\ 11.2.0\ dbhome_1\ oc4j\ jdbc\ lib\ ocrs12.jar;
E: \app\ JamesPJ\ product\ 11.2.0\ dbhome_1\ owb\ wf\ lib\ ojdbc14.jar;
C: \Program Files\ Apache Software Foundation\ Tomcat 7.0\ lib\ servlet - api.jar

When I give the

 java oracle.jdbc.driver.OracleDriver 

command in command prompt, it was showing following lines

Error: Main method not found in class oracle.jdbc.driver.OracleDriver, please define the main method as:
       public static void main(String[] args)
Tiny
  • 27,221
  • 105
  • 339
  • 599
  • 1
    OracleDriver does not contain a main method. You cannot start a servlet without a corresponding container (like tomcat)... calling `java oracle.jdbc.driver.OracleDriver` does not make any sense. – home Dec 16 '12 at 19:22

6 Answers6

30

Have you copied classes12.jar in lib folder of your web application and set the classpath in eclipse.

Right-click project in Package explorer Build path -> Add external archives...

Select your ojdbc6.jar archive

Press OK

Or

Go through this link and read and do carefully.

The library should be now referenced in the "Referenced Librairies" under the Package explorer. Now try to run your program again.

Ajay S
  • 48,003
  • 27
  • 91
  • 111
  • yes i copied classes12.jar ad ojdbc14.jar –  Dec 16 '12 at 18:18
  • which oracle version you are using either 8i or 10g – Ajay S Dec 16 '12 at 18:18
  • i am using oracle 11g R2 –  Dec 16 '12 at 18:20
  • Then use ojdbc6.jar and delete classes12.jar and ojdbc14.jar and Class.forName ("oracle.jdbc.OracleDriver"); and see this http://stackoverflow.com/questions/8007174/what-jdbc-jar-to-use-with-oracle-11g-jdk-1-6-and-how-to-connect-to-the-db-itse – Ajay S Dec 16 '12 at 18:24
  • i changed jar files in lib folder to ojdbc6 and class.forName to oracle.jdbc.OracleDriver now also it was showing java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver –  Dec 16 '12 at 18:31
  • empty the existing classpath and set this jar file in classpath – Ajay S Dec 16 '12 at 18:34
  • which ide you are using to compile this servlet. if you are compiling from cmd then give the what you have set in classpath and how you are compiling the servlet – Ajay S Dec 16 '12 at 18:40
  • Restart the server first and Download appropriate Oracle JDBC driver from here . If you are using Eclipse you need to add ojdbc6.jar which contains the OracleDriver class to your build path. It is usually located in: {ORACLE DRIVER INSTALL PATH}\jdbc\lib\ojdbc6.jar If you are not using an IDE you need to add the path to that JAR to your -classpath option. – Ajay S Dec 16 '12 at 18:47
  • Which jdk version you are using .then do these steps Right-click project in Package explorer Build path Add external archives... Select your ojdbc6.jar archive Press OK The library should be now referenced in the "Referenced Librairies" under the Package explorer. Now try to run your program again. – Ajay S Dec 16 '12 at 18:50
  • I think i'm having latest jdbc driver. resently i installed it. i having jdk1.7.0_09 –  Dec 16 '12 at 18:51
  • I didn't find Add external achives i'm newbie in eclipse.Please tell me in detail... –  Dec 16 '12 at 19:01
  • Go on project -> Right click on project - > Build Path - > Add External archives. – Ajay S Dec 16 '12 at 19:04
  • when i click on build path it was showing configure build path project -> Right click on project - > Build Path - >configure build path What to do for this –  Dec 16 '12 at 19:09
  • Go through this link and read and do carefully http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java) – Ajay S Dec 16 '12 at 19:12
  • Thank you sir That problem was resolved.You spent more time for me thank you very much. Now i got a new problem can you help for this The error was type Exception report message java.util.Date cannot be cast to java.sql.Date description The server encountered an internal error that prevented it from fulfilling this request. exception java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date skypark.Registration.doPost(Registration.java:67) javax.servlet.http.HttpServlet.service(HttpServlet.java:647) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) –  Dec 16 '12 at 19:34
22

Go through C:\apache-tomcat-7.0.47\lib path (this path may be differ based on where you installed the Tomcat server) then past ojdbc14.jar if its not contain.

Then restart the server in eclipse then run your app on server

Manuel
  • 3,828
  • 6
  • 33
  • 48
Ravikumar D G
  • 229
  • 2
  • 2
9

team! For execute SQL-query from your Servlet you should add JDBC jar library in folder

WEB-INF/lib

After this you could call driver, example :

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

Now Y can use connection to DB-server

==> 73!

Mikro Koder
  • 1,056
  • 10
  • 13
  • This worked for me. I am even referencing the ojdbc6.jar file from a different location in classpath, but this lib file must also have a copy of it. No need to restart. – maximusg Sep 02 '19 at 21:06
4

try to add ojdbc6.jar through the server lib "C:\apache-tomcat-7.0.47\lib",

Then restart the server in eclipse.

zedtimi
  • 306
  • 1
  • 6
0

I was getting same kinda error but after copying the ojdbc14.jar into lib folder, no more exception.(copy ojdbc14.jar from somewhere and paste it into lib folder inside WebContent.)

0

I had the same problem but was able to fix it by doing the following:

Right-click on the project -> Properties, then add the JAR (odjbc6 or 14) file in the deployment assembly.

Anthony Grist
  • 38,173
  • 8
  • 62
  • 76
raghu
  • 11
  • Your answer refers to a specific IDE and setup but I think the OP's is different. They also seem to have added this jar to their classpath. – Dave Morrissey Jul 14 '14 at 18:23