码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2016-03-26 18:53:48      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

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>

 

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

标签:

原文地址:http://www.cnblogs.com/tianhengblogs/p/5323612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!