在jdbc中一次执行多个过程调用

时间:2018-07-26 08:26:41

标签: java mysql sql jdbc

FYI, None of the solutions mentioned in this answer have worked for me。我打算在一个sql查询中执行多个过程调用。 Mysql代码是:

SET @SYSTEM_ID = (SELECT `id` FROM `users_admin` WHERE `username`='my_username');
SET @PAYMENT_MODE = 0;
CALL payment_mode_add(@SYSTEM_ID, 'TEST', TRUE, @PAYMENT_MODE);
CALL payment_add(@SYSTEM_ID, @PAYMENT_MODE, 
'receipt/00',1000.50,@TEMP_ID);

它应该工作的方式是,过程“ payment_mode_add”设置一个输出参数,该过程应该被过程“ payment_add”用作输入参数。我知道在Java中无法一次执行多个查询,但是我打算在此处使用的方法在PHP之类的语言中效果很好。 Payment_mode_add的定义是:

# -- PAYMENT-MODE ADD
DELIMITER //
DROP PROCEDURE IF EXISTS `payment_mode_add` //
# -- remove above
CREATE PROCEDURE `payment_mode_add`(IN _author INT, IN _name VARCHAR(20), IN _active BOOLEAN, OUT _id INT)
BEGIN
    # -- declare
    IF NOT EXISTS (SELECT `id` FROM `users_admin` WHERE `id`=_author AND `active`=TRUE) THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Invalid Author'
    ELSEIF EXISTS (SELECT `id` FROM `gym_form_hhq` WHERE `name`=_name) THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '\'Payment Mode\' already exists'
    ELSE
        SET _active = IFNULL(_active,FALSE)
        INSERT INTO `payment_mode`(`name`, `active`, `author`) 
                        VALUES ( _name , _active , _author )
        SET _id = LAST_INSERT_ID()
        SELECT * FROM `payment_mode` WHERE `id`=_id
    END IF
END //
DELIMITER ;

“ payment_add”的定义:

# -- PAYMENT ADD
DELIMITER //
DROP PROCEDURE IF EXISTS `payment_add` //
# -- remove above
CREATE PROCEDURE `payment_add`(IN _author INT, IN _mode INT, IN _receipt VARCHAR(50), IN _amount FLOAT, OUT _id INT)
BEGIN
    # -- declare
    IF NOT EXISTS (SELECT `id` FROM `users_admin` WHERE `id`=_author AND `active`=TRUE) THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Invalid Author'
    ELSEIF NOT EXISTS (SELECT `id` FROM `payment_mode` WHERE `id`=_mode AND `active`=TRUE) THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Invalid Payment Mode'
    ELSE
        SET _receipt = IFNULL(_receipt, NOW())
        SET _amount = IFNULL(_amount, 0)
        INSERT INTO `payment`(`mode`, `reciept`, `amount`, `author`) 
                    VALUES   (_mode , _receipt , _amount , _author)
        SET _id = LAST_INSERT_ID()
        SELECT * FROM `payment` WHERE `id`=_id
    END IF
END

这只是我在JDBC中面临的一些更复杂问题的基本示例。是否可以通过JDBC或任何其他可以让我实现预期输出的方法一次执行所有这4个查询?

1 个答案:

答案 0 :(得分:0)

由于这个问题引起了很多反对,并且我找到了解决方案,所以我正在回答。

因此,您要做的就是编写一个方法,该方法将字符串sql查询数组作为其参数,一个接一个地执行所有查询(在数组中),然后返回最后一个ResultSet。示例:

public ResultSet runMutltipleQueries(String [] arrayOfQueries) throws SQLException{
    ResultSet rs = null;
    Statement stmnt= con.createStatement();
    boolean rsReturned=false;
    for(int i=0; i<arrayOfQueries.length;i++){
       rsReturned = stmnt.execute(arrayOfQueries[i]);
    }
    if(rsReturned){
        rs= stmnt.getResultSet();
    }

    return rs;

}

在调用方法时,

queries= new String [] {"SET @SYSTEM_ID = (SELECT `id` FROM `users_admin` WHERE `username`='"+model.getUserName()+"');",
                 "SET @TRANSACTION_ID = (select `user_data`.`transaction` from `user_data` where id= "+idText.getText()+");",
                 "SET @FREEZE_ID = NULL;",
                 // set payment id to 0 for now, update it in pay operation.
                 "SET @PAYMENT_ID = NULL;",
                 "CALL user_freeze_add(@SYSTEM_ID, @TRANSACTION_ID, '"+FreezeStartDate.getValue()+"', '"+FreezeStartDate.getValue().plusDays(Integer.parseInt(freezeAvailabletext.getText()))+"', @PAYMENT_ID, @FREEZE_ID);"
};

ResultSet rs= DatabaseHandler.getInstance().runMutltipleQueries(queries);