• PreparedStatement 和 Statement 实现基本的批处理


    批处理:若需要对数据库进行多步操作,则就没必要每次都和数据库进行一次通信,这样很消耗资源和时间。则需要将操作进行批处理;
        Statement方式来实现批处理
            优点:
                可以包含结构不同的sql语句
            缺点:
                不能防止sql注入攻击
                没有预编译机制, 效率低下
                如果发送的sql语句主干部分相同, 主干部分每次都需要写.
            
        PreparedStatement方式实现批处理
            优点:
                可以防止sql注入攻击
                采用预编译机制, 效率高
                如果发送的sql语句主干部分相同, 主干部分只需要写一次, 每次发送的只是参数部分.
            缺点:
                包含的sql语句的主干部分必须相同

    PreparedStatement 实现基本的批处理:

     1 public static void main(String[] args) {
     2         Connection conn = null;
     3         Statement stat = null;
     4         PreparedStatement ps = null;
     5         
     6         try {
     7             Class.forName("com.mysql.jdbc.Driver");
     8             conn = DriverManager.getConnection("jdbc:mysql:///mydb5","root","admin");
     9             //开始事务
    10             conn.setAutoCommit(false);
    11             String sql = "insert into tb_batch values (null,?)";
    12             ps = conn.prepareStatement(sql);
    13             for(int i=2000;i<3000;i++){
    14                 ps.setString(1, "tong"+i);
    15                 ps.addBatch();
    16             }
    17             ps.executeBatch();
    18             //提交事务
    19             conn.commit();
    20             System.out.println("完成???????????");
    21             
    22         } catch (Exception e) {
    23             e.printStackTrace();
    24         }finally{
    25             JDBCutils.closeResou(conn, ps, null);
    26         }
    27         
    28         
    29     }

     Statement 实现基本的批处理:

     1 public static void main(String[] args) {
     2         
     3         Connection conn = null;
     4         Statement stat = null;
     5         //注册驱动和连接数据库
     6         conn = JDBCutils.getConn();
     7         try {
     8             stat = conn.createStatement();
     9             stat.addBatch("drop database if exists mydb5");
    10             stat.addBatch("create database mydb5");
    11             stat.addBatch("use mydb5");
    12             stat.addBatch("create table tb_batch(id int primary key auto_increment, name varchar(20))");
    13             stat.addBatch("insert into tb_batch values(null,'a')");
    14             stat.addBatch("insert into tb_batch values(null,'b')");
    15             stat.addBatch("insert into tb_batch values(null,'c')");
    16             stat.executeBatch();
    17             System.out.println("完成");
    18             
    19         } catch (Exception e) {
    20             e.printStackTrace();
    21         }finally{
    22             JDBCutils.closeResou(conn, stat, null);
    23         }
    24         
    25         
    26     }

    注:JDBCutils.closeResou这个是自定义的一个JDBC工具类。主要是用来关闭资源和建立连接。

    http://www.cnblogs.com/tongxuping/p/6880315.html    ---> JDBCutils自定义的工具类包

  • 相关阅读:
    014
    013
    012
    011
    009
    009
    008
    适用于可迭代对象的通用函数
    ubuntu中将py3设置为默认的python
    linux系统下安装gtk
  • 原文地址:https://www.cnblogs.com/tongxuping/p/6880314.html
Copyright © 2020-2023  润新知