从java调用的带有输入和输出参数的多个存储过程

时间:2013-12-11 16:25:38

标签: java sql sql-server stored-procedures

我想在表格中添加一个新行 - NewOrder,其中列ID, OrderDate, OrderValue使用以下存储过程。

create proc [dbo].[insert_new_order](   @newID int OUTPUT,
                    @oValue float)
as
begin
insert into NewOrder values (GETDATE(),@oValue)
set @newID = @@IDENTITY
end

@newID表示以下存储过程的输入参数之一,该存储过程在表OrderedProduct中插入具有列ProductID, OrderID, Price, Quantity

的新行
   create proc [dbo].[insert_updates_in_ordered_product] (  @newID int,
                                                            @productID int,
                                                            @price float,   
                                                            @qty int)
    as
    begin
    insert into OrderedProduct values(@productID,@newID,@qty,@price)
    end

我正在调用这两个存储过程:

     public static void addNewOrderToDB(ArrayList<Product> list){
        Connection connection = null;
        CallableStatement statement1 = null;
        String q1 = "{? = CALL insert_new_order(?)}";
        String q2 = "{CALL insert_updates_in_ordered_product(?,?,?,?)}";
        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);
            statement1 = connection.prepareCall(q1);
            statement1.registerOutParameter(1, Types.INTEGER);
            statement1.setFloat(2, orderValue); 

            ResultSet rs = statement1.executeQuery();
            int uniqueID = rs.getInt(1);
            System.out.println(uniqueID);
            statement1 = connection.prepareCall(q2);

            for(Product p : list){
                statement1.setInt(1, p.getProductId());
                statement1.setInt(2,uniqueID);
                statement1.setInt(3, p.getQty());
                statement1.setFloat(4, p.getPrice());
            }               
            statement1.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }  finally{
            if(statement1 != null){
                try {
                    statement1.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());
                }
            }           

        }
 }

但是我收到了这个错误:

com.microsoft.sqlserver.jdbc.SQLServerException: Procedure or function 'insert_new_order' expects parameter '@oValue', which was not supplied.

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果你这样改变会有效吗?

String q1 = "{CALL insert_new_order(?, ?)}";

更好地匹配您的create proc