why executeUpdate() method returns -1 with callabl

2019-09-18 06:12发布

I am calling a simple stored procedure simple2 in my java program i.e

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();
    }
}

Here the Return Value rs should be 1 but it returns -1 why?

My stored procedure is

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

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

GO

But in case of PrepareStaement it returns correct update count i.e 1 incase of my query why not with respect to callable statement?

1条回答
看我几分像从前
2楼-- · 2019-09-18 06:44

I found this on other site:

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.

Hope it helps.

查看更多
登录 后发表回答