package cn.yunhe.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Test;
public class OracleTest {
private final String DRIVER = "oracle.jdbc.driver.OracleDriver";
private final String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
private final String USER_NAME = "scott";
private final String PASSWORD = "tiger";
@Test
public void query(){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 注册驱动
Class.forName(DRIVER);
// 获取连接地址
conn = DriverManager.getConnection(URL,USER_NAME,PASSWORD);
// 编写sql语句
String sql ="select * from (select deptno ,dname,loc,rownum r from dept)deptr where deptr.r>3 and deptr.r <=6";
// String sql = "select * from dept";
// 获取预编译对象
ps = conn.prepareStatement(sql);
// 获取结果集
rs = ps.executeQuery();
// 遍历输出结果集
while(rs.next()){
System.out.println("部门编号:"+rs.getInt(1)+"\t"+"部门名称:"+rs.getString(2)+"\t"+"部门地址:"+rs.getString(3));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(rs!=null){
rs.close();
}
if(ps != null){
ps.close();
}
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
使用 JDBC 连接 oracle 数据库 详细步骤 —— Java学习
最新推荐文章于 2024-07-19 20:18:03 发布