您的位置:首页 > 编程语言 > Java开发

MyEclipse------executeBatch()使用方法

2016-03-26 17:56 686 查看
executeBatch()方法用于成批地执行SQL语句,但不能执行返回值是ResultSet结果集的SQL语句,而是直接执行stmt.executeBatch();

辅助方法:

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: