代码语言:javascript复制
import java.sql.*;
public class OracleViewInterface {
static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:xe";
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
// 注册JDBC驱动程序
Class.forName("oracle.jdbc.driver.OracleDriver");
// 打开连接
System.out.println("连接到数据库...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
// 创建声明
System.out.println("实例化声明...");
stmt = conn.createStatement();
// 执行查询
String sql;
sql = "SELECT * FROM your_view_name";
ResultSet rs = stmt.executeQuery(sql);
// 处理结果集
while(rs.next()){
// 通过列名获取数据
String column1 = rs.getString("column1");
String column2 = rs.getString("column2");
int column3 = rs.getInt("column3");
// 输出数据
System.out.print("Column 1: " column1);
System.out.print(", Column 2: " column2);
System.out.println(", Column 3: " column3);
}
// 释放资源
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
// 处理JDBC错误
se.printStackTrace();
}catch(Exception e){
// 处理Class.forName错误
e.printStackTrace();
}finally{
// 关闭声明和连接
try{
if(stmt!=null) stmt.close();
}catch(SQLException se2){
}// 什么都不做
try{
if(conn!=null) conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("程序完毕!");
}
}