2014-09-05 29 views
0

我打电话我的Java程序中的简单的存储过程simple2即为什么executeUpdate()方法返回-1与可调用语句,而不是更新计数或0?

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柜面我查询,为什么不针对调用语句?

+0

http://stackoverflow.com/questions/1201358/how-can-i-get-the-number-of-records-affected-by-a-stored-procedure – guleryuz 2014-09-05 06:19:47

+0

http:// stackoverflow可能存在重复。 COM /问题/ 12400985 /什么,做-IT-均值时,语句的executeUpdate-回报-1 – BatScream 2014-09-05 06:50:35

回答

2

我发现这个在其它的网站:

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

希望它有帮助。

相关问题