关于java程序调用oracle数据库中存储过程和function的解决办法

最近由于业务需要,程序在处理大批量数据的时候,处理较慢,最终的解决办法是在数据库中编写存储过程或者function,这样当查询的数据量较大时,就能很好的解决问题,但是,我们作为程序员要做的就是在代码中去调用编好的存储过程或function。以下是我最近写程序的解决办法。。。。

存储过程:
public void QueryStoredProcedure(String id,String procedure){
Connection conn = pcServiceTemplate.getConnection();
//String driver=”oracle.jdbc.driver.OracleDriver”;
//String strUrl=”jdbc:oracle:thin:@192.168.27.5:1521:ORCL”;
//PreparedStatement stmt = null;
//ResultSet rs = null;
//Connection conn = null;
CallableStatement cstmt = null;
try {
cstmt = conn.prepareCall(“{call “+procedure+”(?)}”);
//设置存储过程 call为关键字. 一个参数 而且还是id
cstmt.setString(1,id); //第一个参数
cstmt.execute();//执行
} catch (SQLException e) {
e.printStackTrace();
} finally{
try {
if(cstmt!=null){
cstmt.close();
}if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
存储过程还是相对比较简单的,只需要简单的用jdbc调用一下就可以了。。但是function需要对返回的数据集做一个处理

function:
public List executeOraFucntion(String str1,String str2){
Connection conn = pcServiceTemplate.getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
CallableStatement stmt = null;
List list = new ArrayList();
try {
stmt = conn.prepareCall(“{? = call F_GET_YDFYTJ(?, ?)}”);
//pst = conn.prepareStatement(“SELECT F_GET_YDFYTJ(?, ?) from dual”);
stmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
stmt.setString(2,str1);
stmt.setString(3, str2);
stmt.executeUpdate();
rs = ((OracleCallableStatement) stmt).getCursor(1);
while(rs.next()){
DynaBean bean = new DynaBean(“SBGL_JWXGL_YDWXTJ”,true);
bean.set(“SY_JTGSID”,rs.getString(1));
bean.set(“SY_JTGSMC”,rs.getString(2));
bean.set(“YDWXTJ_GDGS”,rs.getInt(8));
bean.set(“YDWXTJ_GDWXFY”,rs.getDouble(9));
bean.set(“YDWXTJ_SJFSFY”,rs.getDouble(10));
bean.set(“SBGL_JWXGL_YDWXTJ_ID”,UUID.randomUUID().toString());
list.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(stmt!=null){
stmt.close();
}if(rs!=null){
rs.close();
}if(pst!=null){
pst.close();
}if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
最终我们可能需要将list,返回给前台,基于我们公司的需求,需要将list转为json,这就要用到以下转换:
import net.sf.json.JSONArray
JSONArray jsonArray = JSONArray.fromObject(list);
String strData = jsonArray.toString();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值