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

时间:2014-09-05 05:47:52

标签: java sql-server-2008

我在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个为什么不对可调用语句?

1 个答案:

答案 0 :(得分:2)

我在其他网站上发现了这个:

  

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

希望它有所帮助。

相关问题