2013-12-11 69 views
0

我想在表中添加一個新行 - NewOrder,其中列ID, OrderDate, OrderValue使用以下存儲過程。從java中調用輸入和輸出參數的多個存儲過程

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

@newID代表用於插入一個新行中,其具有列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 

我打電話這樣這些2個存儲過程的表OrderedProduct以下存儲過程的輸入參數中的一個:

 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. 

我該如何解決這個問題?

回答

0

如果你這樣改變,它工作嗎?

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

與您的create proc更匹配。

相關問題