代码如下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("关闭连接时发生异常");
}
}
}