Thursday, March 11, 2010

Returning multiple result sets from stored procedure

Handling multiple result sets from stored procedure as follows

My Procedure in Mysql



DELIMITER $$
DROP PROCEDURE IF EXISTS `mms`.`get_items`$$

CREATE DEFINER=`root`@`%` PROCEDURE `get_items`(IN pi_id int)
BEGIN
    /*    Call get_items(0); */
       
    /* ----------Result Set 1 ---------- */
    select     i_id, i_no,  date_format(i_date,'%d/%m/%Y') as i_date,comments,i_status
            from bindent    where     i_id = pi_id ;

        /* ----------Result Set 2 ---------- */
    select     i_id, line_no, item_no,(select item_name from b_items where item_no = b_i_items.item_no )
                 as item_name,floor(req_qty) as req_qty, uom, app_qty, date_format(priority_date,'%d/%m/%Y') as
                  priority_date , wm_approved, vp_approved, comments,
                 (select uom_desc from b_uom where uom = b_i_items.uom) as uom_desc
             from b_i_items where i_id = pi_id ;
    END$$

DELIMITER ;




My Java Code function is

public static InVO getItemDetails(String id)
{
        List IndentDetList = null;
        Connection connection = null;
        CallableStatement callStatement = null;
        ResultSet rs = null, rsItems = null;
        Boolean queryResult = false;
        InVO invo = new InVO();
        try {
            // get the connection to the DB

            connection = MDB.getConnection();
            IndentDetList = new ArrayList();
      

            String query = "{Call get_items(?)}";
            callStatement = connection.prepareCall(query);

            callStatement.setString(1, id);
            rs = callStatement.executeQuery();

            while (rs.next()) {
                invo.setIndentid(rs.getString(1));
                invo.setIndentno(rs.getString(2));
                invo.setSysDate(rs.getString(3));
                invo.setIndentComment(rs.getString(4));
                invo.setStatus(rs.getString(5));
            }

            ItemVO itemobject = null;

            if (callStatement.getMoreResults())
        {
                rsItems = callStatement.getResultSet();

                 while (rsItems.next())
                {
                    itemobject = new ItemVO();
                    itemobject.setLineno(rsItems.getInt(2));
                    itemobject.setItemId(rsItems.getString(3));
                    itemobject.setItemName(rsItems.getString(4));
                    itemobject.setUnits(rsItems.getString(5));
                    itemobject.setUom(rsItems.getString(6));
                    itemobject.setPrioritydate(rsItems.getString("priority_date"));
                    itemobject.setUomDesc(rsItems.getString("uom_desc"));
                    IndentDetList.add(itemobject);
                }
                invo.setItemsList(IndentDetList);

            }


        } catch (Exception e) {
            indentvo = null;
            if (logger.isEnabledFor(Level.ERROR)) {
                logger.error("Error while getting Connection from DB: " + e.getMessage());
                                                                         }
                                        }
           finally {
                        MDB.close(connection);
                    }

        return indentvo;


    }// function closed

1 comment:

  1. hey check this new website www.countcode.com. It's a social network made for programmers, where you can download,share or upload source codes, where you can count your own code lines for free. You have access to the web forum and the web chatroom. we are happy to have you joined to our community!

    ReplyDelete