【问题标题】:Multiple stored procedures with input and output parameters called from java从java调用的具有输入和输出参数的多个存储过程
【发布时间】:2013-12-11 16:25:37
【问题描述】:

我想使用以下存储过程在表中添加一个新行 - 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.

我该如何解决这个问题?

【问题讨论】:

    标签: java sql sql-server stored-procedures


    【解决方案1】:

    这样改行不行?

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

    这更符合您的create proc

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多