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

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)

This question is related to java servlets jdbc tomcat7

The answer is


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.)


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.


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

Then restart the server in eclipse.


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.)


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


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!


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to servlets

Google Recaptcha v3 example demo Difference between request.getSession() and request.getSession(true) init-param and context-param java.lang.NoClassDefFoundError: org/json/JSONObject how to fix Cannot call sendRedirect() after the response has been committed? getting error HTTP Status 405 - HTTP method GET is not supported by this URL but not used `get` ever? Create a simple Login page using eclipse and mysql Spring get current ApplicationContext insert data into database using servlet and jsp in eclipse What is WEB-INF used for in a Java EE web application?

Examples related to jdbc

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver' Hibernate Error executing DDL via JDBC Statement Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] MySQL JDBC Driver 5.1.33 - Time Zone Issue Spring-Boot: How do I set JDBC pool properties like maximum number of connections? Where can I download mysql jdbc jar from? Print the data in ResultSet along with column names How to set up datasource with Spring for HikariCP? java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver Exception occurring. Why? java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbname

Examples related to tomcat7

Could not load the Tomcat server configuration INFO: No Spring WebApplicationInitializer types detected on classpath Name [jdbc/mydb] is not bound in this Context I cannot access tomcat admin console? Tomcat 7.0.43 "INFO: Error parsing HTTP request header" jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class Tomcat Server not starting with in 45 seconds HTTP Status 500 - Servlet.init() for servlet Dispatcher threw exception How to fix JSP compiler warning: one JAR was scanned for TLDs yet contained no TLDs? java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver