当前位置: 代码迷 >> Eclipse >> jdbc连oracle出现ClassNotFoundException错误,咋回事
  详细解决方案

jdbc连oracle出现ClassNotFoundException错误,咋回事

热度:545   发布时间:2016-04-23 14:20:23.0
jdbc连oracle出现ClassNotFoundException异常,怎么回事?
写了一个测试程序,不知道为什么连不上数据库
1.已将驱动程序classes12.jar复制到tomcat\common\lib下.(没有启动tomcat)
2.已经设置了CLASSPATH包含%TOMCAT_HOME%\common\lib
3.代码:
package test;

import java.sql.*;

public class ReadDB {
public static void main(String[] args) {
// TODO 自动生成方法存根
String url = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL";
String userName = "teacher";
String password = "teacher";
String sql = null;
Connection conn = null;
Statement stmt = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver"); ---->出错
System.out.println("数据库打开成功");
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException");
System.exit(1);
}
try {
conn = DriverManager.getConnection(url, userName, password);
stmt = conn.createStatement();
sql = "select * from student";
stmt.executeQuery(sql);
System.out.println("成功查询");
} catch (SQLException e) {
System.err.println("查询失败");
} finally {
try {
stmt.close();
conn.close();
} catch (SQLException e) {
System.err.println("Close SQLException");
}
}
}
}
运行出现
ClassNotFoundException

------解决方案--------------------
classes12.jar给复制到你当前工程的classpath下就可以了
  相关解决方案