按照官方的文档上说,运行了execute(),假如是增删改的语句,可以通过getUpdateCount()获取受影响的行数。但实际测试,获取的一直是匹配行数。一直不解,麻烦各位也试试,看下能不能找到问题的答案,在这里先谢谢各位了。源代码:
public static void main(String[] args) {
Connection conn = null;
PreparedStatement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver"); System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS); System.out.println("Creating statement...");
String sql;
sql = "UPDATE user SET nickname = \'father\'";
stmt = conn.prepareStatement(sql);
stmt.execute();
System.out.println("更新1:"+stmt.getUpdateCount());
stmt.execute();
System.out.println("更新2:"+stmt.getUpdateCount());
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("There are so thing wrong!");
}
public static void main(String[] args) {
Connection conn = null;
PreparedStatement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver"); System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS); System.out.println("Creating statement...");
String sql;
sql = "UPDATE user SET nickname = \'father\'";
stmt = conn.prepareStatement(sql);
stmt.execute();
System.out.println("更新1:"+stmt.getUpdateCount());
stmt.execute();
System.out.println("更新2:"+stmt.getUpdateCount());
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("There are so thing wrong!");
}
解决方案 »
- 韩顺平的网上商城,跟着视频做的,出现了空指针,请各位大牛解决一下,小弟在此谢过了
- 帮我看看这个错误信息,谢谢各位啦
- (org.openqa.selenium.TestSuiteBuilder)这属于哪个包
- Struts1.2的验证框架中的问题
- 异常:java.lang.reflect.InvocationTargetException 快折磨死我了!
- axis客户端调用web service服务器端方法,返回对象数组,出现问题!!!(急,大家帮帮忙)
- 启动JBOSS时报错 Document root element "web-app", must match DOCTYPE root "null",如何解决
- Tomcat在重新加载后Session内容会自动清掉的问题
- !!!!过年,散分,送给和我一样还没回家的!!!
- 请问哪里有中文版的struts书下载?
- 启动tomcat时卡在Initializing Spring FrameworkServlet 'springMVC'
- 问下,现在微信朋友圈还能自动跳转到浏览器么?
System.out.println("更新1:"+stmt.getUpdateCount());
stmt.execute();
执行两次什么鬼 你可以创建两个pre执行同一条看效果。
你的sql 没where 子句,会更新所有的记录
那个返回的就是更新(或删除)的记录条数 Retrieves the current result as an update count
比如 现在批量删除10个用户 传了个List<Integer> 进来 跑了sql后,if(getCount()==List.Size() ){删除成功} else{回滚}。
你的sql 没where 子句,会更新所有的记录
那个返回的就是更新(或删除)的记录条数 Retrieves the current result as an update count
比如 现在批量删除10个用户 传了个List<Integer> 进来 跑了sql后,if(getCount()==List.Size() ){删除成功} else{回滚}。
我没看懂你在说什么,这是实际不关有没有where的事,我之前就弄了where了,update无论多少次返回都是1。理论运行一次真实修改之后,后面都是返回0才是对的。
这是5.1开发指南里写的运行execute之后调用getUpdateCount方法获得受影响的行数。我在mysql的控制台里运行上面同样的sql也是返回0,而不是2。你用jdbc运行两次一样的update的sql你就知道我明白的是什么了。
If you do not know ahead of time whether the SQL statement will be a SELECT or an UPDATE/INSERT, then you can use the execute(String SQL) method. This method will return true if the SQL query was a SELECT, or false if it was an UPDATE, INSERT, or DELETE statement. If the statement was a SELECT query, you can retrieve the results by calling the getResultSet() method. If the statement was an UPDATE, INSERT, or DELETE statement, you can by calling getUpdateCount() on the Statement instance.
你的sql 没where 子句,会更新所有的记录
那个返回的就是更新(或删除)的记录条数 Retrieves the current result as an update count
比如 现在批量删除10个用户 传了个List<Integer> 进来 跑了sql后,if(getCount()==List.Size() ){删除成功} else{回滚}。
其实就简单点说,就是同样的update的sql,我用jdbc运行了2次,都是返回一样的值。可明明第二次运行应该返回的是0才是正常的。
你的sql 没where 子句,会更新所有的记录
那个返回的就是更新(或删除)的记录条数 Retrieves the current result as an update count
比如 现在批量删除10个用户 传了个List<Integer> 进来 跑了sql后,if(getCount()==List.Size() ){删除成功} else{回滚}。
其实就简单点说,就是同样的update的sql,我用jdbc运行了2次,都是返回一样的值。可明明第二次运行应该返回的是0才是正常的。
为什么?两次都更新了,只不过第二次更后的数据与原来的数据相同而已。
(我猜的。你执行两次的操作本来就有问题,在这个基础上揣测程序的结果意义不大。你感兴趣可以深入点jdbc,数据库什么的。http://blog.csdn.net/mssql_dba/article/details/39554345)
尝试第一次update后手动commit,观察第二次的update结果。
你的sql 没where 子句,会更新所有的记录
那个返回的就是更新(或删除)的记录条数 Retrieves the current result as an update count
比如 现在批量删除10个用户 传了个List<Integer> 进来 跑了sql后,if(getCount()==List.Size() ){删除成功} else{回滚}。
其实就简单点说,就是同样的update的sql,我用jdbc运行了2次,都是返回一样的值。可明明第二次运行应该返回的是0才是正常的。
为什么?两次都更新了,只不过第二次更后的数据与原来的数据相同而已。
(我猜的。你执行两次的操作本来就有问题,在这个基础上揣测程序的结果意义不大。你感兴趣可以深入点jdbc,数据库什么的。http://blog.csdn.net/mssql_dba/article/details/39554345)
实际不是两次的问题,因为我测试了用mysql命令行执行sql返回的是0,而且上面的例子我也改成执行1次,也还是返回2。 现在主要就是mysql的文档是这么写的,实际情况却不是文档上面写的那样,我就只想知道为什么不同的原因。
事实是不管你值是否变化,都算更新的
有点疑惑你为什么质疑这个
下面是官方为这个参数的解释:
useAffectedRowsDon't set the CLIENT_FOUND_ROWS flag when connecting to the server (not JDBC-compliant, will break most applications that rely on "found" rows vs. "affected rows" for DML statements), but does cause "correct" update counts from "INSERT ... ON DUPLICATE KEY UPDATE" statements to be returned by the server.Default: falseSince version: 5.1.7