gpt4 book ai didi

java - ClassNotFoundException oracle.jdbc.driver.OracleDriver 仅在 servlet 中,使用 Eclipse

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:31:16 24 4
gpt4 key购买 nike

下面的代码失败了:

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

错误:

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

两个println打印:

Wed_Jun_22_11:18:51_PDT_2005
false

这让我觉得这个类存在并且可以找到。同样,这个完全相同的类也适用于非 servlet 应用程序。

我已多次重新启动一切并多次重新生成应用程序/servlet。所有值都经过硬编码以使其简单和简短。

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");
}

失败的完整 servlet:

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();
}
}

}

此应用程序有效:

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();
}
}

}

我正在使用:

  • Eclipse JavaEE 1.4.2
  • Tomcat 7
  • jdk1.7
  • 甲骨文 11g R2
  • Windows 7 64 位

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com