public class Testclass {
private static Connection conn;
static{
//加載驅動
try {
Class.forName("Oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","scott");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void testProcedure() throws SQLException{
CallableStatement cas=conn.prepareCall("{call emppackage.selectEmps(?)}");
int index=1;
cas.registerOutParameter(index++,oracle.jdbc.OracleTypes.CURSOR);
boolean flag=cas.execute();
System.out.println(flag);
//OracleCallableStatement 可使強制造型為oracle中的存儲過程的對象
//調用裡面的getCursor方法獲取返回的ResultSet的結果集
ResultSet rs=((OracleCallableStatement)cas).getCursor(1);
//遍歷輸出
while(rs.next()){
System.out.println(rs.getInt(1));
}
public static void functionTest2() throws SQLException{
CallableStatement cas =conn.prepareCall("{?=call mypackage.queryEmps}");
int index=1;
cas.registerOutParameter(index++, oracle.jdbc.OracleTypes.CURSOR);
boolean flag=cas.execute();
System.out.println(flag);
ResultSet rs=((OracleCallableStatement)cas).getCursor(1);
while(rs.next()){
System.out.println(rs.getInt(1));
}
}