Results 1 to 6 of 6

Thread: Stored Procedure help

  1. #1

    Default Stored Procedure help

    Hi,

    I think I'm almost there but the last few steps ahve got me stumped!!!

    I am writing an inner class that returns a Map of key/value pairs. Here's my code so far:

    Code:
    public Map getBranches() {
            logger.info("Entering method QueryDaoImpl.getBranches");
    
            Map branches = new HashMap();
            branches.put("1", "London");
            branches.put("2", "Madrid");
            branches.put("3", "Barcelona");
            branches.put("4", "New York");
            logger.info("Exiting method QueryDaoImpl.getBranches");
    
            return branches;
        }
    
        private class GetBranchesSP extends StoredProcedure {
            private static final String SQL = "PKG_PBTV_UILOOKUP.F_PBTVBRANCHESGET";
    
            public GetBranchesSP(DataSource ds) {
                setDataSource(ds);
                setSql(SQL);
                setFunction(true);
    
                declareParameter(new SqlOutParameter("cursor", OracleTypes.CURSOR,new RowMapper(){
    
                    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                        logger.info("Entering method .mapRow");
                        // TODO implement method
                        logger.info("Exiting method .mapRow");
                        return null;
                    }
                    
                }));
                
                compile();
            }
    
            public Map execute() {
                Map results = execute(new HashMap());
    
                return results;
            }
        }
    the first method getBranches() doesnt call the inner class yet and instead constructs the map manually. Its this map I want to create in the inner class. I've seen a lot of examples of using RowMapper but they all write to System.out. How can I return a map like I want to?

    The cursor will hold a variable number of rows and each row has 2 strings (id and value - for populating a html drop down).

    Thanks in advance,

    Rakesh

  2. #2
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    In your execute method you should have something like:

    List cursor = results.get("cursor");

    where cursor is then the list of objects you generated in your mapRow() method. You can iterate that list and transform it into the map you need.

  3. #3

    Default

    Hi,

    well i experimented a bit and came up with this approach and it works:

    Code:
        public Map getBranches() {
            logger.info("Entering method QueryDaoImpl.getBranches");
    
            GetBranchesSP sp = new GetBranchesSP(dataSource);
    
            logger.info("Exiting method QueryDaoImpl.getBranches");
    
            return sp.execute();
        }
    
        private class GetBranchesSP extends StoredProcedure {
            private static final String SQL = "PKG_PBTV_UILOOKUP.F_PBTVBRANCHESGET";
    
            public GetBranchesSP(DataSource ds) {
                setDataSource(ds);
                setSql(SQL);
                setFunction(true);
    
                declareParameter(new SqlOutParameter("cursor", OracleTypes.CURSOR,
                        new ResultSetExtractor() {
                        public Object extractData(ResultSet rs)
                            throws SQLException, DataAccessException {
                            logger.info("Entering method .extractData");
    
                            Map resultsMap = new HashMap();
    
                            while (rs.next()) {
                                resultsMap.put(rs.getString(1), rs.getString(2));
                            }
    
                            logger.info("Exiting method .extractData");
    
                            return resultsMap;
                        }
                    }));
    
                compile();
            }
    
            public Map execute() {
                Map results = execute(new HashMap());
    
                return (Map) results.get("cursor");
            }
        }
    I guess there was nothing I wanted to do per-row so I changed to the ResultSetExtractor and created my Map.

    Is this the most common/optimal way to do this?

    Thanks

    Rakesh

  4. #4
    Join Date
    Aug 2004
    Posts
    1,104

    Default

    I would move the creation of the GetBranchesSP class to a place where it's just initialized once rather than creating a new one each time the getBranches is called. You might only call this method once in your code though so my point might be moot.
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  5. #5
    Join Date
    Feb 2005
    Location
    Boston, MA
    Posts
    1,142

    Default

    Quote Originally Posted by trisberg
    I would move the creation of the GetBranchesSP class to a place where it's just initialized once rather than creating a new one each time the getBranches is called. You might only call this method once in your code though so my point might be moot.
    Using the JdbcDaoSupport class as a superclass for your object works well. You initialize the stored procedure in the initDao method:

    Code:
    private GetBranchesSP getBranchesSP;
    protected void initDao() {
      getBranchesSP = new GetBranchesSP(getJdbcTemplate());
    }
    I prefer initializing my stored procedures with a JdbcTemplate because JdbcDaoSupport allows you to set a DataSource and exception translator for the JdbcTemplate it creates internally.

  6. #6
    Join Date
    Oct 2007
    Location
    NY, USA
    Posts
    38

    Default

    Hi

    I have similar requirement where I need to pass 2 IN parameters and it returns Ref-Cursor as OUT parameter.

    How do we modify the above code to meet my req.

    If I use JDBCTemplate, how do I get data source? Is it from app-context file or other way? Please respond.

    Thanks in advance !

    Thanks
    Sudhakar

Similar Threads

  1. Replies: 7
    Last Post: Jan 7th, 2013, 02:58 PM
  2. Replies: 5
    Last Post: Oct 1st, 2010, 08:00 AM
  3. Informix stored procedure & spring
    By miha in forum Data
    Replies: 8
    Last Post: Dec 5th, 2006, 10:44 PM
  4. Replies: 0
    Last Post: May 11th, 2005, 06:11 AM
  5. Replies: 1
    Last Post: Nov 25th, 2004, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •