PreparedStatement,批量添加数据

PreparedStatement,批量添加数据

package com.javakc.test2;

import com.javakc.db.Utils;
import com.javakc.vo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//批量添加
public class testInsert {
    public static void insert(User u) throws SQLException {
        Connection connection = null;
        try {
            //获取连接
            connection = Utils.getConnection();
            //拼接带有占位符的sql字符串,将传入的数据使用占位符编写
            String sql = "insert into user(id,name)values(?,?)";
            //创建能处理预编译的sql语句的对象PreparedStatement
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            //使用参数对象替换预编SQL语句中的占位符
            preparedStatement.setInt(1, u.getId());
            preparedStatement.setString(2, u.getName());
            //执行sql,返回操作的条数,如果新增一条数据返回1
            int rows = preparedStatement.executeUpdate();
            preparedStatement.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            //关闭连接
            if (connection != null) {
                connection.close();
            }
        }
    }

    //批量添加数据
    //每次循环都要创建新的数据库连接,造成执行速度慢
    public static void batchInsert(List<User> list) throws SQLException {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            insert(u);
        }
    }

    public static void main(String[] args) throws SQLException {
        //批量添加数据,一共10条
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User u = new User();
            u.setId(i + 1);
            u.setName("a");
            list.add(u);
        }
        long l1 = System.currentTimeMillis();
        batchInsert(list);
        long l2 = System.currentTimeMillis();
        System.out.println(l2 - l1);
    }
}

addBatch,executeBatch,批量添加数据

package com.javakc.test2;

import com.javakc.db.Utils;
import com.javakc.vo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class testInsert1 {
    //批量添加数据
    //封装了很多条数据
    public static void batchInsert(List<User> list) throws SQLException {
        Connection connection = null;
        try {
            //获取连接
            connection = Utils.getConnection();
            //拼接带有占位符的sql字符串,将传入的数据使用占位符编写
            String sql = "insert into user (id,name)values(?,?)";
            //创建能处理预编译的sql语句的对象PreparedStatement
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            //循环
            for (int i = 0; i < list.size(); i++) {
                User u = list.get(i);
                //使用参数对象替换预编SQL语句中的占位符
                preparedStatement.setInt(1, u.getId());
                preparedStatement.setString(2, u.getName());
                //循环一次就会生成一条sql语句
                preparedStatement.addBatch();
                //每100条插入数据库一次
                if (i % 100 == 0) {
                    //将生成的所有数据插入到数据库里
                    preparedStatement.executeBatch();
                }
            }
            preparedStatement.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    public static void main(String[] args) throws SQLException {
        //批量添加数据,一共10条
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User u = new User();
            u.setId(i + 1);
            u.setName("x");
            list.add(u);
        }
        long l1 = System.currentTimeMillis();
        batchInsert(list);
        long l2 = System.currentTimeMillis();
        System.out.println(l2 - l1);
    }
}

你可能感兴趣的:(JDBC,java)