1

MySql Stored Procedure

DELIMITER $$

USE `RMS`$$

DROP PROCEDURE IF EXISTS `posHdrTrxEntry`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `posHdrTrxEntry`(

IN mbrID INT,
IN usrID INT,
IN unqID VARCHAR(50),

OUT trxID INT
)
BEGIN
DECLARE trxID INT;

INSERT INTO `RMS`.`transaction`
(`MemberID`,`UserID`,`UID`)
VALUES (mbrID,usrID,unqID);

IF  ROW_COUNT() > 0 THEN
SET trxID=(SELECT ID FROM `transaction` WHERE `transaction`.`UID`=unqID);
END IF;

IF trxID > 0 THEN
UPDATE `transaction` SET `transaction`.`UID`=0 WHERE `transaction`.`ID`=trxID;
END IF;

END$$

DELIMITER ;

Java --Code Snippet

Connection connection = null;
    int trxID;
    try {
        connection = new DbService().getConnection();
 CallableStatement cs = connection.prepareCall("{ CALL posHdrTrxEntry(?,?,?,?) }");
        cs.setString(1, null);
        cs.setInt(2, item.getUserID());
        cs.setString(3, item.getUid());
        cs.registerOutParameter(4, java.sql.Types.INTEGER);
        cs.execute();
        trxID=cs.getInt(4);
        cs.close();

Stored Procedure always returning out parameter value = 0, even stored procedure last update statement execute successfully

IF trxID > 0 THEN
UPDATE `transaction` SET `transaction`.`UID`=0 WHERE `transaction`.`ID`=trxID;
END IF;

Its means that TrxID is greater than 0, why I get value only 0 in java?

Edit (Picture View) enter image description here

4

1 回答 1

3

Dear just remove the first statement after BEIGN

Remove the Following:

DECLARE trxID INT;

Because this is out parameter , u can just use SET to assign the value.

I think It will work fine!

于 2012-06-10T07:02:07.780 回答