java实现excel导入数据库

时间: 2023-11-18 20:48:25 浏览: 34
以下是一个示例代码,可以将Excel文件中的数据导入到MySQL数据库中: ```java import java.io.File; import java.io.FileInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Iterator; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; public class ExcelToDatabase { public static void main(String[] args) { String jdbcURL = "jdbc:mysql://localhost:3306/mydb"; String username = "root"; String password = "password"; String excelFilePath = "data.xlsx"; int batchSize = 20; Connection connection = null; try { long start = System.currentTimeMillis(); FileInputStream inputStream = new FileInputStream(new File(excelFilePath)); Workbook workbook = WorkbookFactory.create(inputStream); Sheet sheet = workbook.getSheetAt(0); Iterator<Row> rowIterator = sheet.iterator(); connection = DriverManager.getConnection(jdbcURL, username, password); connection.setAutoCommit(false); String sql = "INSERT INTO users (name, email, country_code) VALUES (?, ?, ?)"; PreparedStatement statement = connection.prepareStatement(sql); int count = 0; rowIterator.next(); // skip the header row while (rowIterator.hasNext()) { Row nextRow = rowIterator.next(); Iterator<Cell> cellIterator = nextRow.cellIterator(); while (cellIterator.hasNext()) { Cell nextCell = cellIterator.next(); int columnIndex = nextCell.getColumnIndex(); switch (columnIndex) { case 0: String name = nextCell.getStringCellValue(); statement.setString(1, name); break; case 1: String email = nextCell.getStringCellValue(); statement.setString(2, email); break; case 2: String countryCode = nextCell.getStringCellValue(); statement.setString(3, countryCode); break; } } statement.addBatch(); if (count % batchSize == 0) { statement.executeBatch(); } } workbook.close(); // execute the remaining queries statement.executeBatch(); connection.commit(); connection.close(); long end = System.currentTimeMillis(); System.out.printf("Import done in %d ms\n", (end - start)); } catch (Exception e) { e.printStackTrace(); try { connection.rollback(); } catch (SQLException e1) { System.out.println("Rollback failed " + e1.getMessage()); } } } } ``` 这个例子使用了Apache POI库来读取Excel文件。在这个例子中,我们假设Excel文件的第一行是标题行,所以我们调用了`rowIterator.next()`来跳过它。然后,我们循环遍历每一行,并使用`cellIterator`来遍历每一列。我们使用`PreparedStatement`来构造SQL语句,然后使用`addBatch()`将其添加到批处理中。我们使用`executeBatch()`方法在提交之前执行批处理。最后,我们在`catch`块中回滚事务,以确保数据的一致性。

相关推荐

最新推荐

recommend-type

Java实现Excel导入导出数据库的方法示例

主要介绍了Java实现Excel导入导出数据库的方法,结合实例形式分析了java针对Excel的读写及数据库操作相关实现技巧,需要的朋友可以参考下
recommend-type

Java实现excel大数据量导入

主要为大家详细介绍了Java实现excel大数据量导入,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Java将excel中的数据导入到mysql中

我们在实际工作中的一些时候会需要将excel中的数据导入数据库,如果你的数据量成百上千甚至更多,相信一点点ctrlc、ctrlv也不是办法,这里我们以mysql数据库为例,将excel中的数据存入数据库。 我的思路是:先将...
recommend-type

大数据Excel通过POI导入数据库通用设计方案

考虑到Excel导入在项目中应用较多,随后其他项目中都会用到,为了减少重复工作,不必因为excel的样式、数据等变化而重新编码,重复劳动,因此必须抽取通用的东西出来,封装共同点,暴露个性点,使用时只需要关注相关...
recommend-type

基于三层感知机实现手写数字识别-内含源码和说明书.zip

基于三层感知机实现手写数字识别-内含源码和说明书.zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。