本文代碼中注釋寫的比較清楚不在單獨說明,希望能幫助到大家,
實例代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
public List<Map<String , Object>> doCallProcedure(String procedureString,String[] parameters) throws PersistentDataOperationException { if (!isReady ()) { throw new PersistentDataOperationException( "DAO is not ready." ); } ResultSet rs = null ; List<Map< String, Object>> list = new ArrayList<Map<String ,Object>>(); try { Connection con=session.connection(); String procedure = "{call " +procedureString+ "(?,?,?) }" ; //拼裝調用存儲過程字符串 CallableStatement cstmt = con.prepareCall (procedure ); //調用存儲過程 cstmt.setString ( 1 ,parameters [ 0 ]); //設置入參 cstmt.setInt ( 2 , Integer. parseInt( parameters[ 1 ])) ; //設置入參 cstmt.registerOutParameter ( 3 , oracle.jdbc.OracleTypes.CURSOR ); //設置出參 cstmt.execute (); //執行提交 rs = (ResultSet ) cstmt.getObject ( 3 ); //獲取出參,3為參數順序數 ResultSetMetaData rsm =rs.getMetaData (); //獲得列集 Map< String, Object> map= null ; int col = rsm.getColumnCount (); //獲得列的個數 String colName [] = new String[ col] ; //列名集合 for ( int i = 0 ; i < col; i++) { colName [i ] = rsm.getColumnName (i+ 1 ); } while ( rs.next()){ //注意訪問結果集是從索引位置1開始的,而不是0 map = new HashMap< String, Object> (); for ( int j = 0 ; j < colName.length; j++) { map.put (colName [j ], rs.getString (j+ 1 )); } list.add (map ); } session.flush (); } catch (HibernateException e) { throw new PersistentDataOperationException( e) ; } catch (SQLException e) { e.printStackTrace (); } return list; } |
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://wjch-111.iteye.com/blog/2332043