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


    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>
  • 相关阅读:
    结构和联合
    字符串、字符和字节
    数组
    函数
    指针
    操作符和表达式
    语句
    数据
    TinyXML2 使用
    是否忘记了向源中添加“#include "StdAfx.h"”?
  • 原文地址:https://www.cnblogs.com/tianhengblogs/p/5323612.html
Copyright © 2020-2023  润新知