具有从java调用的输入和输出参数的多个存储过程
我想使用以下存储过程在表-具有从java调用的输入和输出参数的多个存储过程,java,sql,sql-server,stored-procedures,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表示以下存储
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());
}
}
}
}
我如何修复此问题?如果您这样更改,是否有效
String q1 = "{CALL insert_new_order(?, ?)}";
更符合您的创建过程
String q1 = "{CALL insert_new_order(?, ?)}";