MyEclipse------executeBatch()使用方法
2016-03-26 17:56
686 查看
executeBatch()方法用于成批地执行SQL语句,但不能执行返回值是ResultSet结果集的SQL语句,而是直接执行stmt.executeBatch();
辅助方法:
addBatch();向批处理中加入一个更新语句。
clearBatch():清空批处理中的更新语句
testExecuteBatch.jsp
辅助方法:
addBatch();向批处理中加入一个更新语句。
clearBatch():清空批处理中的更新语句
testExecuteBatch.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <%@page import="java.sql.*" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'executeBatch.jsp' starting page</title> </head> <body> <% String url="jdbc:mysql://localhost:3306/student?useSSL=true"; String useName="root"; String password="2277092"; Connection conn=null; Statement stmt=null; try{ Class.forName("com.mysql.jdbc.Driver"); //out.print("加载驱动类成功"); } catch(Exception e){ out.print(e); } try{ conn=DriverManager.getConnection(url,useName,password); stmt=conn.createStatement(); //使用addBatch()添加SQL语句 stmt.addBatch("insert into classinfo values(4,'石兰','女','网络工程','2277092','五邑大学','20','90');"); stmt.addBatch("update classinfo set phone='18814182472' where no=3113002421"); //使用executeBatch()执行批量更新语句 stmt.executeBatch(); //统计更新计数数组 /* int affectRowCounts[]=stmt.executeBatch(); for(int i=0;i<affectRowCounts.length;i++){ out.print("第"+(i+1)+"个更新语句影响的数据行数为:"+affectRowCounts[i]+"<br>"); } */ stmt.close(); out.print("更新成功"); } catch(SQLException e){ out.print(e); } finally{ try{ if(conn!=null) conn.close(); } catch(SQLException e){ out.print("关闭数据库连接出现异常"); } } %> </body> </html>
相关文章推荐
- 初识RxJava(入门篇)
- 分析Java堆
- java集合
- spring基础
- spring+springMVC,声明式事务失效,原因以及解决办法
- spring中的定时调度实现TimerFactoryBean引起的隐患
- java并发编程——ConcurrentHashMap(1.7)
- Java多线程之并发容器:CopyOnWrite到底干啥用的
- java并发编程—— 线程池原理 详解 ThreadPoolExecutor
- 用Spring MVC实现自定义404页面
- J2SE基础-构造方法总结
- Java经典设计模式(1):五大创建型模式(附实例和详解)
- java并发编程—— 七 Thread理解与死锁分析(哲学家进餐演示)
- 20145240 《Java程序设计》第四周学习总结
- 解决eclipse中egit中的cannot open git-upload-pack问题
- MAC下安装多版本JDK和切换几种方式
- Java移位操作符
- Java关键字总结
- java如何操作字符串取得绝对路径中的文件名及文件夹名
- java io 流解析