• Java使用Mysql数据库实现批量添加数据


    EmployeeDao.java

    //批处理添加数据
    public int saveEmploeeBatch(){
    int row = 0;
    try{
    con = DBCon.getConn();
    String sql = "insert into tb_employee(name,age,sex,duty)values(?,?,?,?)";
    pstmt = (PreparedStatement) con.prepareStatement(sql);
    Random random = new Random();
    for(int i = 0; i < 10; i++){
    pstmt.setString(1 , "22"+i);
    pstmt.setInt(2, 1+i);
    pstmt.setString(3, i % 2 == 0?"男":"女");
    pstmt.setInt(4, random.nextInt(5)+10);
    pstmt.addBatch();
    }
    int[] rows = pstmt.executeBatch();
    row = rows.length;
    }catch(Exception e){
    e.printStackTrace();
    }finally{
    try{
    if(pstmt != null)
    pstmt.close();
    }catch(Exception e){
    e.printStackTrace();
    }
    try{
    if(con != null)
    con.close();
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    return row;
    }
     
    TestSql05.java
    package com.sql.test;
     
    import com.sql.dao.EmployeeDao;
     
    public class TestSql05 {
     
    public static void main(String[] args){
    int rows = EmployeeDao.getInstance().saveEmploeeBatch();
    System.out.println("批量添加信息的行数是:"+rows);
    }
    }
  • 相关阅读:
    [CF600E]Lomsat gelral
    [BZOJ3237]连通图
    [CF580D]Kefa and Dishes
    [BZOJ4726]Sabota?
    bzoj2120&&2453 -- 带修改莫队
    bzoj4726 [ POI2017 ] -- 树形DP
    bzoj2809 [ APIO2012 ] -- 主席树
    bzoj4216 -- 分块
    bzoj4173 -- 欧拉函数
    bzoj2982 -- Lucas定理
  • 原文地址:https://www.cnblogs.com/future-zmy/p/6231281.html
Copyright © 2020-2023  润新知