将插入查询的输出参数用作java中同一存储过程中另一个插入查询的输入参数

时间:2013-12-11 09:20:19

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

我创建了一个存储过程,它在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());
                }
            }           

        }
 }

还有其他办法吗?

1 个答案:

答案 0 :(得分:0)

1 /添加标识作为proc 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