免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: linuxkumao
打印 上一主题 下一主题

请教Java中getUpdatecount方法总是返回-1的问题 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2007-11-28 09:18 |只看该作者
好的。

就现在我知道情况是这样的,对于Oracle,getUpdateCount是可以可以得到Update语句影响的记录数,但只能是直接的Update语句执行(如3楼的帖子),而不能是调用包含Update语句的存储过程(如1楼的帖子)。

如果既要调用含有Update语句的存储过程,又要得到该Update语句影响的记录数,就得用Out输出参数了。

下面是我对1楼和3楼的程序修改之后的情况:

SQL> select text from user_source where name='TMP_U';
TEXT
--------------------------------------------------------------------------------
procedure tmp_u(v_id tmp.id%type,v_rowcount out integer)
as
begin
update tmp set value = 'aa' where id >= v_id;
v_rowcount := sql%rowcount; ----- 得到前一条DML语句所影响的行数
end;

import java.sql.* ;

public class dbtest {

 public static void main(String[] args) throws SQLException {

        String className,url,uid,pwd;
        className = "oracle.jdbc.driver.OracleDriver";
        url = "jdbc:oracle:thin:@192.168.1.8:1521:TBTDB";
        uid = "scott";
        pwd = "tbtscott2006";
        
        Connection conn=null;
        CallableStatement proc1 = null;
        CallableStatement proc2 = null;
        
        int c=-999;
        int x=5;
        
        try
        {
            Class.forName(className);
            conn = DriverManager.getConnection(url,uid,pwd);
            
            proc1 = conn.prepareCall("{ call tmp_i() }");
            proc1.execute();
    proc1.close();
               
         proc2 = conn.prepareCall("{ call tmp_u(?,?)}");
         proc2.setInt(1,x);
         proc2.registerOutParameter(2,java.sql.Types.INTEGER);
         proc2.execute();
         c=proc2.getInt(2);
         proc2.close();
               
         System.out.println(c);
            System.out.println("OK");
            
        }
        catch (Exception e)
        {
                e.printStackTrace();
        System.out.println("ERROR");
        System.out.print(e.getMessage());
        }
        finally
        {
                conn.close();
        }
        
 }       

}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP