简体   繁体   中英

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

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?

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.

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

我遇到了同样的问题,但修复了它右键单击项目 - >属性并在部署程序集中添加jar(ojdbc6或14)文件然后它将工作...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM