请教preparedstatement的executeBatch()返回的数组内容

dennyjava 2007-06-25 09:51:16
各位大虾:
sql="delete from tbl_test where mytype=?";
PreparedStatement ps= conn.prepareStatement(sql);
ps.setString(1,"abc");
ps.addBatch();

int a= ps.executeBatch();

我的delete from tbl_test where mytype='abc'应该有5条数据被删除,可为何
此处 a.length 的值确为 1
executeBatch()返回的不是实际执行语句的结果吗?

谢谢

...全文
1937 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
dnsfyd860702 2010-12-21
  • 打赏
  • 举报
回复
用oracle数据库返回值就是-2 不管成功与否
huhuGang 2007-07-04
  • 打赏
  • 举报
回复
http://mail-archives.apache.org/mod_mbox/ibatis-dev/200606.mbox/%3c23769093.1149675270958.JavaMail.jira@brutus%3e

The JDBC spec allows vendors to skip certain functionality in executeBatch and this is what
Oracle does, they return -2 for updates in executeBatch which iBATIS translates to 0. Oracle
just doesn't return the number of rows being updated for prepared statements.

>From the Oracle 9i documentation: "For a prepared statement batch, it is not possible
to know the number of rows affected in the database by each individual statement in the batch.
Therefore, all array elements have a value of -2. According to the JDBC 2.0 specification,
a value of -2 indicates that the operation was successful but the number of rows affected
is unknown."
huhuGang 2007-07-04
  • 打赏
  • 举报
回复
oracle就是這樣
可以看這里http://blog.csdn.net/elimago/archive/2007/07/03/1676516.aspx
skywoodsky 2007-06-25
  • 打赏
  • 举报
回复
代码没错了,单独执行下看看吧
dennyjava 2007-06-25
  • 打赏
  • 举报
回复
是批量,可总是返回-2,晕倒
skywoodsky 2007-06-25
  • 打赏
  • 举报
回复
不是批量sql的话,用executeUpdate吧
dennyjava 2007-06-25
  • 打赏
  • 举报
回复
skywoodsky() :a[0] 等于 -2
怎么回事?
skywoodsky 2007-06-25
  • 打赏
  • 举报
回复
int[] a= ps.executeupdate();
System.out.println(a[0]);
dennyjava 2007-06-25
  • 打赏
  • 举报
回复

用executeBatch()是不是没有办法获得?只能用executeupdate()?
lv810 2007-06-25
  • 打赏
  • 举报
回复
int a= ps.executeupdate();
dennyjava 2007-06-25
  • 打赏
  • 举报
回复
如何取得我删除的5条记录数ne
?
dennyjava 2007-06-25
  • 打赏
  • 举报
回复
to skywoodsky() :
用a[0]取出是-2?
如何取得我删除的记录数?
谢谢
skywoodsky 2007-06-25
  • 打赏
  • 举报
回复
executeBatch
int[] executeBatch()
throws SQLException
Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts. The int elements of the array that is returned are ordered to correspond to the commands in the batch, which are ordered according to the order in which they were added to the batch. The elements in the array returned by the method executeBatch may be one of the following:
A number greater than or equal to zero -- indicates that the command was processed successfully and is an update count giving the number of rows in the database that were affected by the command's execution
A value of SUCCESS_NO_INFO -- indicates that the command was processed successfully but that the number of rows affected is unknown
If one of the commands in a batch update fails to execute properly, this method throws a BatchUpdateException, and a JDBC driver may or may not continue to process the remaining commands in the batch. However, the driver's behavior must be consistent with a particular DBMS, either always continuing to process commands or never continuing to process commands. If the driver continues processing after a failure, the array returned by the method BatchUpdateException.getUpdateCounts will contain as many elements as there are commands in the batch, and at least one of the elements will be the following:


A value of EXECUTE_FAILED -- indicates that the command failed to execute successfully and occurs only if a driver continues to process commands after a command fails
A driver is not required to implement this method. The possible implementations and return values have been modified in the Java 2 SDK, Standard Edition, version 1.3 to accommodate the option of continuing to proccess commands in a batch update after a BatchUpdateException obejct has been thrown.


Returns:
an array of update counts containing one element for each command in the batch. The elements of the array are ordered according to the order in which commands were added to the batch.
Throws:
SQLException - if a database access error occurs or the driver does not support batch statements. Throws BatchUpdateException (a subclass of SQLException) if one of the commands sent to the database fails to execute properly or attempts to return a result set.
Since:
1.3
skywoodsky 2007-06-25
  • 打赏
  • 举报
回复
你只执行了一句sql,数组长度,自然是1
skywoodsky 2007-06-25
  • 打赏
  • 举报
回复
executeBatch 返回的是一个数组
每个元素才是对应命令update记录 的数量
skywoodsky 2007-06-25
  • 打赏
  • 举报
回复
那。。我也不知道了,等待其他高手吧
dennyjava 2007-06-25
  • 打赏
  • 举报
回复
我单独又做了,每次都是返回-2,我用的oracle数据库

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧