欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解java调用存储过程并封装成map

程序员文章站 2024-02-27 19:38:33
详解java调用存储过程并封装成map            本文代码...

详解java调用存储过程并封装成map

           本文代码中注释写的比较清楚不在单独说明,希望能帮助到大家,

实例代码:

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; 
       } 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!