0
我是java中的新手。我尝试在SQL SERVER中创建存储过程(SP)。 的SP是:Java获得输出CallableStatement JDBC
go
create procedure sp_calculate
@value1 int = 0,
@value2 int = 0,
@sum int OUTPUT,
@multiply int OUTPUT
as
begin
set nocount on
set @sum = @value1 + @value2
set @multiply = @value1 * @value2
end
我做traint以下Getting the Return Value from JDBC MSSQL。所以,我这样做是在Java中如下:
public static void main(String[] args) {
try (Connection conn = ConnectionUtil.getConnection()) {
CallableStatement cst = conn.prepareCall("{ (?, ?) = call sp_calculate (@value1 =?, @value2=?) }");
cst.setInt(3, 4);//try some value
cst.setInt(4, 10);//try some value
cst.registerOutParameter(1, java.sql.Types.INTEGER);
cst.registerOutParameter(2, java.sql.Types.INTEGER);
cst.executeUpdate();
int sum = cst.getInt(1);
int multiply = cst.getInt(2);
System.out.println("sum = " + sum + " - product = " + multiply);
}
catch (SQLException ex) {
Logger.getLogger(StoredProcedured.class.getName()).log(Level.SEVERE, null, ex);
}
}
但结果是"com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '{'."
任何人都可以告诉我,我创造什么错误?
最后,我的工作它指出:
public static void main(String[] args) {
try (Connection conn = ConnectionUtil.getConnection()) {
CallableStatement cst = conn.prepareCall("{ call sp_calculate (?,?,?,?) }");
cst.setInt(1, 10);//try some value
cst.setInt(2, 4);//try some value
cst.registerOutParameter(3, java.sql.Types.INTEGER);
cst.registerOutParameter(4, java.sql.Types.INTEGER);
cst.executeUpdate();
int sum = cst.getInt(3);
int product = cst.getInt(4);
System.out.println("sum = " + sum + " - product = " + product);
}
catch (SQLException ex) {
Logger.getLogger(StoredProcedured.class.getName()).log(Level.SEVERE, null, ex);
}
}
不,我敢肯定,我让它正确的。我试着用一些存储过程之前工作好吧 – haind
@haind anwer更新,检查出来。 – Sajmon
是的。我这样做。非常感谢你 – haind