请教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()返回的不是实际执行语句的结果吗?

谢谢

...全文
2003 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数据库
内容概要:本文围绕不确定环境下的多式联运路径优化问题展开研究,提出并实现了基于AFO算法、遗传算法(GA)和粒子群优化算法(PSO)的三种智能优化方法,并借助Matlab平台完成算法编程与仿真。研究构建了考虑时间、成本、转运风险等多重不确定因素的路径优化模型,系统比较了AFO、GA、PSO三种算法在收敛速度、全局寻优能力和稳定性方面的表现,同时引入Matlab自带的全局优化搜索器作为基准对照,深入分析各算法在复杂物流网络中的适用边界与性能差异。研究表明,AFO算法在解决此类组合优化问题时展现出更快的收敛效率和更强的局部规避能力。; 适合人群:具备一定Matlab编程基础与运筹优化知识,从事物流工程、交通运输规划、智能算法开发等相关领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于多式联运、综合货运网络中的路径决策支持系统构建;②为不确定性条件下复杂路径规划问题提供智能算法选型依据与技术实现方案;③支持科研人员复现主流优化算法并开展横向性能对比实验,推动算法改进与实际落地。; 阅读建议:建议读者结合提供的Matlab代码逐模块分析算法实现流程,重点理解目标函数设计、约束条件处理及参数敏感性分析部分,可通过调整问题规模与算法参数进行对比实验,进一步拓展至动态路径规划或大规模网络优化等延伸场景。
内容概要:本文研究了基于QLearning自适应强化学习的PID控制器在自主水下航行器(AUV)运动控制中的应用,通过Matlab代码实现了控制算法的仿真验证。该方法融合强化学习的在线自适应能力与传统PID控制的稳定性优势,利用QLearning算法动态优化PID控制器的比例、积分、微分参数,以应对水下复杂流体环境、模型不确定性及外部干扰等挑战,从而提升AUV轨迹跟踪的精度、鲁棒性与动态响应性能。文中系统阐述了AUV的六自由度非线性动力学建模过程、QLearning算法的状态空间与动作空间设计、奖励函数构造及训练机制,并详细说明了PID参数自整定的闭环控制架构。仿真结果表明,相较于传统固定参数PID控制器,该智能控制策略在多种工况下均展现出更优的控制效果,有效抑制了超调,加快了响应速度,并增强了抗干扰能力。; 适合人群:具备自动控制理论、强化学习基础及Matlab/Simulink仿真能力,从事水下机器人、智能控制、海洋工程、自动化等领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于AUV、UUV等无人水下平台的高精度自主导航与运动控制;②为解决非线性、强耦合、时变系统的控制器参数自适应整定问题提供智能化解决方案;③作为强化学习与经典控制理论深度融合的技术范例,推动智能控制算法在海洋装备中的工程化应用。; 阅读建议:建议读者结合提供的Matlab代码深入理解算法实现细节,重点剖析QLearning的状态-动作-奖励机制设计、PID参数更新逻辑及仿真对比实验结果,有条件者可在更复杂的动力学模型或实际硬件平台上进一步验证与优化算法性能。

62,622

社区成员

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

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