import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class OracleProcedureWithRetVal {
/**
* Oracle帶有輸出參數的存儲過程
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//加載驅動
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection con = null;
CallableStatement cs = null;
try {
//得到連接
con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","tiger");
//創建CallableStatement對象
cs = con.prepareCall("{call fly_pro5(?,?)}");
//對參數賦值
cs.setInt(1, 7369);
cs.registerOutParameter(2,oracle.jdbc.OracleTypes.VARCHAR);
//執行存儲過程調用
cs.execute();
String ename = cs.getString(2);
System.out.println("7369的姓名是" + ename);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//關閉資源
try {
cs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}