[英]how i get a value from a return statement of sqlserver stored procedure to java?
i'am trying to get into a variable called valorReturn the return statement in my stored procedure of sql server... The sqlserver stored procedure is working fine, but the problem is java.. 我正在尝试进入一个名为valorReturn的变量,该变量在我的sql server存储过程中... sqlserver存储过程工作正常,但问题是java ..
create procedure login
@cliente varchar(10),@pw varchar(10)
as begin
if exists(select cliente from seg where cliente=@cliente)
begin
if(select pw from seg where cliente=@cliente)=@pw
begin
if(select privilegio from seg where cliente=@cliente)=0 begin
print 0
return 0
end
end
exec 'xd','xd'
here i get a message from PRINT line: 0
在这里,我从打印行收到一条消息:0
so, i should be able get the value and set it into variable valorReturn with this JAVA code 因此,我应该能够使用该JAVA代码获取值并将其设置为变量valorReturn
String consulta="exec login '"+usr+"','"+pw+"'";
CallableStatement st=con.prepareCall(consulta);
st.setInt(1,10);
st.registerOutParameter(1, Types.INTEGER);
st.execute();
int valorReturn=st.getInt(1);
this is the exception printed in console 这是控制台中打印的异常
com.microsoft.sqlserver.jdbc.SQLServerException: index 1 is out of bound.
com.microsoft.sqlserver.jdbc.SQLServerException:索引1超出范围。
thanks, i appreciate any help! 谢谢,我感谢任何帮助! :)
:)
SQL needs to provide an output parameter to bind onto. SQL需要提供一个绑定的输出参数。
create procedure login
@cliente varchar(10), @pw varchar(10), @result INT = -1 OUTPUT
as begin
if exists(select cliente from seg where cliente=@cliente)
begin
if(select pw from seg where cliente=@cliente)=@pw
begin
if(select privilegio from seg where cliente=@cliente)=0 begin
print 0
SET @result = 0 -- new parameter
return 0
end
end
String consulta="exec login ?,?,?"; //moved out to parameters to avoid SQL injection.
CallableStatement st= con.prepareCall(consulta);
st.setString(1,usr);
st.setString(2,pw);
st.registerOutParameter(3, Types.INTEGER);
st.execute();
int valorReturn=st.getInt(3);
More information can be found at https://docs.microsoft.com/en-us/sql/connect/jdbc/using-a-stored-procedure-with-output-parameters 可以在https://docs.microsoft.com/zh-cn/sql/connect/jdbc/using-a-stored-procedure-with-output-parameters中找到更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.