【问题标题】:why executeUpdate() method returns -1 with callable statement instead of update count or 0?为什么 executeUpdate() 方法返回带有可调用语句的 -1 而不是更新计数或 0?
【发布时间】:2014-10-30 00:32:03
【问题描述】:

我在我的 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 在我的查询中,为什么不考虑可调用语句?

【问题讨论】:

标签: java sql-server-2008


【解决方案1】:

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

executeUpdate() 将仅返回 PreparedStatement 和 Statement 的 rowCount。尽管 CallableStatement 扩展了 Statement 接口的 executeUpdate(),但这不会返回 rowCount,即使对该过程的调用在数据库中进行了多次更新/插入。 callableStatement 上的 executeStatement() 如果在被调用过程中有 OUT 参数,则返回 1,否则返回 0。

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 2022-12-16
    • 1970-01-01
    • 2013-02-09
    • 2019-09-14
    • 2016-08-29
    相关资源
    最近更新 更多