mysql JDBC批量操作

来源:互联网 发布:北京java工程师待遇 编辑:程序博客网 时间:2024/05/28 22:10
private static void insertData() throws SQLException {        DruidDataSource dataSource = new DruidDataSource();   dataSource.setUrl("jdbc:mysql://ip:3306/cms?rewriteBatchedStatements=true");        dataSource.setUsername("root");        dataSource.setPassword("root");        dataSource.init();        Connection connection = dataSource.getConnection();        PreparedStatement preparedStatement = connection.prepareStatement("insert into Student(user_name,sex,age) values (?,?,?)");        List<String> list = new ArrayList();        for (int i = 0; i < list.size; i++) {            preparedStatement.setString(1, list.get(i));            preparedStatement.addBatch();            if((i+1) % batchSize == 0) {                preparedStatement.executeBatch();            }        }        preparedStatement.executeBatch();        connection.close();           dataSource.close();    }
0 0
原创粉丝点击