代码如下package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class FirstJDBCTest {
public static void main(String[] args) {
FirstJDBCTest dao = new FirstJDBCTest();
dao.findAll();
}
public void findAll() {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:tarena", "scott", "123456");
stmt = con.createStatement();
rs = stmt
.executeQuery("select empno, ename, sal, hiredate from emp");
while (rs.next()) {
System.out.println(rs.getInt("empno") + ","
+ rs.getString("ename") + ","
+ rs.getDouble("sal") + "," + rs.getDate("hiredate"));
}
} catch (ClassNotFoundException e) {
System.out.println("驱动类无法找到!");
throw new RuntimeException(e);
} catch (SQLException e) {
System.out.println("数据库访问异常!");
throw new RuntimeException(e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
System.out.println("关闭连接时发生异常");
}
}
}
确认你的库已经在classpath里
try to follow this:
The following are the steps that are given below to add ojdbc14.jar in eclipse:
1) Inside your project
2) Libraries
3) Right click on JRE System Library
4) Build Path
5) Select Configure Build Path
6) Click on Add external JARs...
7) C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib
8) Here you will get ojdbc14.jar
9) select here
10) open
11) ok
save and run the program you will get output.
try this
http://stackoverflow.com/questions/15598757/oracle-jdbc-driver-oracledriver-classnotfoundexception
jar包不对
JDBC连接oracle要用
classes14.jar
驱动类使用的不对,换个驱动oracle jar 包
没找到合适的驱动类
第一像他们说的找不到驱动类;第二看看数据库名,账号,密码是否正确(注意空格)