以下代码在行上失败:
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();
}
}
}
我正在使用:
答案 0 :(得分:11)
可能您没有在应用程序中部署oracle驱动程序。
您有几种选择:
WEB-INF/lib
文件夹Build Path
- > Configure Build Path...
- > Order and Export
- >检查驱动程序。答案 1 :(得分:7)
您必须在项目的部署程序集中包含ojdbc6.jar文件...
选择包含jsp文件的Web项目...
在Eclipse的菜单栏中选择项目标签
在下拉菜单中选择属性
选择部署程序集
在其中添加ojdbc6.jar文件。
答案 2 :(得分:0)
试试这个,将oracle.jdbc.driver.OracleTypes
更改为oracle.jdbc.OracleTypes