【问题标题】:Use the output parameter of an insert query as input parameter for another insert query in the same stored procedure in java使用插入查询的输出参数作为java中同一存储过程中另一个插入查询的输入参数
【发布时间】:2013-12-29 04:32:15
【问题描述】:

我创建了一个存储过程,它在 NewOrder 表中插入一个新订单,并使用订单的新信息更新 OrderedProduct 表。我正在使用@@IDENTITY 获取 OrderedProduct 表的 OrderID。

    create proc sp_insert_new_order(
                                    @oValue float,
                                    @newID int OUTPUT,
                                    @productID int,
                                    @price float,
                                    @qty int)
   as
   begin
   insert into NewOrder values(GETDATE(),@oValue)
   set @newID = @@IDENTITY
   insert into OrderedProduct values(@productID,@newID,@qty,@price)
   end

我的问题是如何从 java 调用这个存储过程? 这是我到目前为止尝试过的

 public static void addNewOrderToDB(ArrayList<Product> list){
        Connection connection = null;
        CallableStatement statement = null;
        float orderValue = 0;
        //calculate orderValue

        for(Product p : list){
            orderValue = orderValue + (p.getPrice() * p.getQty());              
        }
        System.out.println(orderValue);

        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            connection = DriverManager.getConnection(url);
            statement = connection.prepareCall("{ ? = CALL sp_insert_new_order(?,?,?,?)}");
            statement.setFloat(3, orderValue);
            statement.registerOutParameter(1, Types.INTEGER);
            //statement.execute();

            int uniqueID = statement.getInt(1);
            System.out.println(uniqueID);
            for(Product p : list){
                statement.setInt(1, p.getProductId());
                statement.setInt(2,uniqueID);
                statement.setInt(3, p.getQty());
                statement.setFloat(4, p.getPrice());
            }               
            statement.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }  finally{
            if(statement != null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    System.err.println("SQLException: " + e.getMessage());
                }
            }
            if(connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    System.err.println("SQLException: " + e.getMessage());
                }
            }           

        }
 }

还有其他方法吗?

【问题讨论】:

    标签: java mysql sql sql-server stored-procedures


    【解决方案1】:

    1/ 将身份添加为您的 proc 的返回值:

    create proc sp_insert_new_order(@oValue float,
                                    @newID int,
                                    @productID int,
                                    @price float,
                                    @qty int)
    as
    begin
    insert into NewOrder values(CURRENT_TIMESTAMP,@oValue)
    select @newID = SCOPE_IDENTITY()
    insert into OrderedProduct values(@productID,@newID,@qty,@price)
    
    return @newID
    end
    

    2/ 然后: Getting the Return Value from JDBC MSSQL

    编辑

    或者你可以随意使用输出参数:

    create proc sp_insert_new_order(@oValue float,
                                    @newID int OUTPUT,
                                    @productID int,
                                    @price float,
                                    @qty int)
    as
    begin
    insert into NewOrder values(CURRENT_TIMESTAMP,@oValue)
    select @newID = SCOPE_IDENTITY()
    insert into OrderedProduct values(@productID,@newID,@qty,@price)
    
    end
    

    在您的代码中,使用 registerOutParameter 方法:http://technet.microsoft.com/en-us/library/ms378596.aspx

    【讨论】:

    • 按照您的建议,要获取我的新订单的 id,我应该分别执行 2 个查询。
    • 对不起,我没有看到输出参数。我会编辑答案
    • 要使用statement.registerOutParameter(1, Types.INTEGER);,我必须调用statement.execute();才能使用这样的输出参数:int uniqueID = statement.getInt(1); for(Product p : list){ statement.setInt(1, p.getProductId()); statement.setInt(2,uniqueID); statement.setInt(3, p.getQty()); statement.setFloat(4, p.getPrice()); } ,或者至少这是我从你的建议中得到的
    • 要取回 SCOPE_IDENTITY() 值,您需要先执行查询,是的(如果我理解您的要求)。然后你做一个getInt() 来取回价值。
    猜你喜欢
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多