Detail java calls the stored procedure and wraps it as map

  • 2020-10-31 21:44:37
  • OfStack

Detail java calls the stored procedure and wraps it as map

The comments in the code in this article are more clear than a separate description, I hope to help you,

Example code:


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+ "(?,?,?) }"; // Assemble the call stored procedure string  
                  CallableStatement cstmt = con.prepareCall (procedure ); // Calling a stored procedure  
                  cstmt.setString (1,parameters [0 ]); // Set into the reference  
                  cstmt.setInt (2, Integer. parseInt( parameters[ 1])) ;// Set into the reference  
                  cstmt.registerOutParameter (3, oracle.jdbc.OracleTypes.CURSOR ); // Set the parameter  
                   
                  cstmt.execute (); // commit  
                  rs = (ResultSet ) cstmt.getObject (3 ); // To get the parameter, 3 Is the sequence number of parameters  
                  ResultSetMetaData rsm =rs.getMetaData (); // Get set  
                  Map< String, Object> map= null; 
                   int col = rsm.getColumnCount ();  // Get the number of columns  
                   String colName [] = new String[ col] ;// The column set  
                   for (int i = 0; i < col; i++) { 
                        colName [i ] = rsm.getColumnName (i+1 ); 
                   } 
                   while( rs.next()){ 
                         // Note that the access result set is from the index location 1 At the beginning, not at the beginning 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; 
       } 

If you have any questions, please leave a message or go to this site community exchange discussion, thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: