Java导入Excel存储到数据库中
在开发过程中,我们经常会遇到需要将Excel文件中的数据导入到数据库中的需求。本文将介绍如何使用Java编程语言实现这个功能。
准备工作
在开始之前,我们需要准备以下环境和工具:
- Java开发环境(JDK)
- Maven构建工具
- Apache POI库(用于读取Excel文件)
- 数据库(本文以MySQL为例)
导入Excel数据到数据库的流程
首先,我们来看一下整个流程的示意图:
flowchart TD
A[准备Excel文件] --> B[使用Apache POI读取Excel文件]
B --> C[解析Excel数据]
C --> D[建立数据库连接]
D --> E[插入数据到数据库]
E --> F[关闭连接]
F --> G[完成]
代码示例
导入Excel文件
首先,我们需要准备一个Excel文件,里面包含要导入的数据。假设我们有一个名为"employees.xlsx"的文件,它包含了员工的姓名、年龄和职位信息。
接下来,我们使用Apache POI库来读取Excel文件。首先,我们需要在pom.xml
文件中添加POI的依赖项:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
然后,我们可以使用以下代码读取Excel文件:
import org.apache.poi.ss.usermodel.*;
public class ExcelReader {
public static void readExcel(String filePath) throws Exception {
Workbook workbook = WorkbookFactory.create(new File(filePath));
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
String name = row.getCell(0).getStringCellValue();
int age = (int) row.getCell(1).getNumericCellValue();
String position = row.getCell(2).getStringCellValue();
// 将数据插入到数据库中
insertIntoDatabase(name, age, position);
}
workbook.close();
}
private static void insertIntoDatabase(String name, int age, String position) {
// 实现将数据插入到数据库的逻辑
}
}
插入数据到数据库
在上述代码中,我们使用insertIntoDatabase
方法将数据插入到数据库中。这个方法的具体实现因数据库而异,这里以MySQL为例:
import java.sql.*;
public class DatabaseConnector {
private static final String URL = "jdbc:mysql://localhost:3306/mydatabase";
private static final String USER = "root";
private static final String PASSWORD = "password";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
public static void insertData(String name, int age, String position) throws SQLException {
Connection connection = getConnection();
String sql = "INSERT INTO employees (name, age, position) VALUES (?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, name);
statement.setInt(2, age);
statement.setString(3, position);
statement.executeUpdate();
statement.close();
connection.close();
}
}
完整示例
下面是将上述代码片段整合在一起的完整示例:
import org.apache.poi.ss.usermodel.*;
import java.io.File;
public class ExcelReader {
public static void main(String[] args) {
try {
readExcel("employees.xlsx");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void readExcel(String filePath) throws Exception {
Workbook workbook = WorkbookFactory.create(new File(filePath));
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
String name = row.getCell(0).getStringCellValue();
int age = (int) row.getCell(1).getNumericCellValue();
String position = row.getCell(2).getStringCellValue();
insertIntoDatabase(name, age, position);
}
workbook.close();
}
private static void insertIntoDatabase(String name, int age, String position) {
try {
Connection connection = DatabaseConnector.getConnection();
String sql = "INSERT INTO employees (name, age, position) VALUES (?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, name);
statement.setInt(2, age);
statement.setString(3, position);
statement.executeUpdate();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import java.sql.*;
public class Database