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();
}
mysql JDBC批量操作
最新推荐文章于 2025-02-20 16:39:12 发布