java servlet 调用oracle数据库存储过程

ervlet首先建立数据库连接类ConnectFactory:
  1. import java.sql.Connection;  
  2. import java.sql.DriverManager;  
  3. import java.sql.SQLException;  
  4. import javax.naming.InitialContext;  
  5. import javax.sql.DataSource;  
  6.   
  7. public class ConnectFactory {  
  8.     private  Connection conn;  
  9.     private  DataSource connnectionPool;  
  10.           
  11.         //链接中间件的连接池  
  12.     public ConnectFactory(){  
  13.         try {  
  14.           InitialContext ctx = new InitialContext();  
  15.           connnectionPool = (DataSource) ctx.lookup("PaymentDS");  
  16.         }  
  17.         catch (Exception e) {  
  18.             e.printStackTrace();  
  19.         }  
  20.     }   
  21.       
  22.         //获得连接池的链接对象  
  23.     public  Connection getConn() throws Exception {  
  24.         try {  
  25.           conn=connnectionPool.getConnection();  
  26.           return conn;  
  27.         }  
  28.         catch (SQLException sqle) {  
  29.           throw new Exception();  
  30.         }  
  31.       }  
  32.       
  33.     static{  
  34.         try{  
  35.             Class.forName("oracle.jdbc.driver.OracleDriver");  
  36.         }catch(ClassNotFoundException e){  
  37.             throw new ExceptionInInitializerError(e);  
  38.         }  
  39.     }  
  40.       
  41.         //获得手写链接字符串的链接  
  42.     public static Connection getConnection() throws SQLException{  
  43.         return DriverManager.getConnection("jdbc:oracle:thin:@192.168.2.37:1521:sdecp","eca","errors");  
  44.     }  
  45.   
  46. }  
外部调用链接:
  1. public Map getBusinessB2ADeal(String b2aNo) throws Exception{  
  2.     Map res = null;  
  3.     Connection con = ConnectFactory.getConnection();  
  4. /       Connection con = new ConnectFactory().getConn();  
  5.     CallableStatement cs = con.prepareCall("{call proc_business_b2aNo(?,?,?,?,?,?)}");  
  6.     cs.setString("b2aNo", b2aNo);  
  7.     cs.registerOutParameter("b2aBank", Types.VARCHAR);  
  8.     cs.registerOutParameter("b2aAmount", Types.DOUBLE);  
  9.     cs.registerOutParameter("b2aType", Types.VARCHAR);  
  10.     cs.registerOutParameter("b2aNotify", Types.VARCHAR);  
  11.     cs.registerOutParameter("b2aBill", Types.VARCHAR);  
  12.     cs.execute();  
  13.       
  14.     String b2aBank = cs.getString("b2aBank");  
  15.         String b2aAmount = cs.getString("b2aAmount");  
  16.     String b2aType = cs.getString("b2aType");  
  17.     String b2aNotify = cs.getString("b2aNotify");  
  18.     String b2aBill = cs.getString("b2aBill");  
  19.       
  20.     if(!StringUtils.isEmpty(b2aBank)){  
  21.         res = new HashMap();  
  22.         res.put("bankId", b2aBank);  
  23.         res.put("amount", b2aAmount);  
  24.         res.put("b2aType", b2aType);  
  25.         res.put("b2aNotify", b2aNotify);  
  26.         res.put("b2aBill", b2aBill);  
  27.     }  
  28.     cs.close();  
  29.     con.close();  
  30.     return res;  
  31. }  
存储过程:
  1. CREATE OR REPLACE PROCEDURE proc_business_b2aNo(  
  2.   b2aNo in varchar2,  
  3.   b2aBank out varchar2,  
  4.   b2aAmount out NUMBER,  
  5.   b2aType out varchar2,  
  6.   b2aNotify out varchar2,  
  7.   b2aBill out varchar2  
  8. )  
  9. IS  
  10. BEGIN  
  11.   SELECT tb2a.b2a_bank,tb2a.b2a_amount,tb2a.b2a_Type,tb2a.b2a_NOTIFY,tb2a.b2a_Bill  
  12.        INTO b2aBank,b2aAmount,b2aType,b2aNotify,b2aBill  
  13.   FROM t_b2a tb2a WHERE tb2a.b2a_no = b2aNo;  
  14.    EXCEPTION  
  15.      WHEN NO_DATA_FOUND THEN  
  16.        NULL;  
  17.      WHEN OTHERS THEN  
  18.        -- Consider logging the error and then re-raise  
  19.        RAISE;  
  20. END proc_business_b2aNo; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值