gpt4 book ai didi

java - 为什么executeUpdate()方法使用可调用语句返回-1而不是更新计数或0?

转载 作者:行者123 更新时间:2023-12-02 05:29:36 25 4
gpt4 key购买 nike

我在我的 java 程序中调用一个简单的存储过程 simple2,即

public class Simple {

private static final String sqlSimple = " {call simple2(?,?)}";
Connection con = null;
CallableStatement cstmt=null;
public Connection getDbConnection(){
try{
Class.forName(StudentConst.getDbDriverName());
con = DriverManager.getConnection(StudentConst.getDbConnUrl(),StudentConst.getDbUser(),StudentConst.getDbPassword());
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
return con;
}

public void insertSimple(){

try{
con = getDbConnection();
cstmt = con.prepareCall(sqlSimple);
cstmt.setString(1,"naveen");
cstmt.setInt(2, 22);
int rs = cstmt.executeUpdate();
System.out.println("return val is "+rs);
}catch(SQLException e){
System.out.println("caught"+e);
}
}

public static void main(String[] args) {
Simple s = new Simple();
s.insertSimple();
}
}

这里返回值rs应该是1,但它返回-1,为什么?

我的存储过程是

CREATE PROC [dbo].[simple2]
@name varchar(50), @age int
AS
set nocount on

insert into Simple1
(name,age)
values
(@name, @age)

GO

但是在PrepareStaement的情况下,它返回正确的更新计数,即1,在我的查询中,为什么不考虑可调用语句呢?

最佳答案

我在其他网站上找到了这个:

The executeUpdate() would return the rowCount only for the PreparedStatement and Statement. Although the CallableStatement extends the executeUpdate() of the Statement Interface, this would not return a rowCount, even if the call to the procedure makes several updates/inserts in the database. The executeStatement() on the callableStatement would return 1 if there was an OUT parameter inside the called Procedure, else it would return 0.

希望有帮助。

关于java - 为什么executeUpdate()方法使用可调用语句返回-1而不是更新计数或0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25679356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com