首页 AI百科文章正文

java如何实现将excel表数据导入数据库

AI百科 2025年11月19日 18:36 239 admin

Java 实现 Excel 表数据导入数据库的详细步骤

在现代软件开发中,将Excel表格中的数据导入数据库是一个常见且重要的任务,本文将介绍如何使用Java编程语言来实现这一功能,我们将使用Apache POI库来处理Excel文件,以及JDBC来连接和操作数据库,以下是详细的步骤和代码示例。

  1. 添加依赖项 我们需要在项目中添加Apache POI库和JDBC驱动的依赖项,如果你使用的是Maven构建工具,可以在pom.xml文件中添加以下依赖:

    java如何实现将excel表数据导入数据库

    <dependencies>
     <!-- Apache POI for handling Excel files -->
     <dependency>
         <groupId>org.apache.poi</groupId>
         <artifactId>poi-ooxml</artifactId>
         <version>4.1.2</version>
     </dependency>
     <!-- MySQL JDBC Driver (或其他数据库的JDBC驱动) -->
     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>8.0.23</version>
     </dependency>
    </dependencies>

  2. 读取Excel文件 使用Apache POI库读取Excel文件中的数据,假设我们有一个名为"data.xlsx"的Excel文件,其中包含一个名为"Sheet1"的工作表,并且该工作表中有一列名为"Name"和一列名为"Age"的数据,以下是读取Excel文件的代码示例:

    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream; import java.util.Iterator;

public class ExcelReader { public static void main(String[] args) { try (FileInputStream fis = new FileInputStream("data.xlsx"); Workbook workbook = new XSSFWorkbook(fis)) { Sheet sheet = workbook.getSheet("Sheet1"); for (Row row : sheet) { Cell nameCell = row.getCell(0); Cell ageCell = row.getCell(1); String name = nameCell.getStringCellValue(); int age = (int) ageCell.getNumericCellValue(); System.out.println("Name: " + name + ", Age: " + age); // 这里可以将数据插入到数据库中 } } catch (Exception e) { e.printStackTrace(); } } }

java如何实现将excel表数据导入数据库


3. 连接到数据库并插入数据
使用JDBC连接到目标数据库(例如MySQL),并将从Excel文件中读取的数据插入到数据库表中,以下是连接到MySQL数据库并插入数据的代码示例:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DatabaseWriter {
    private static final String URL = "jdbc:mysql://localhost:3306/yourdatabase";
    private static final String USER = "yourusername";
    private static final String PASSWORD = "yourpassword";
    public static void main(String[] args) {
        try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
            String insertSQL = "INSERT INTO yourtable (name, age) VALUES (?, ?)";
            try (PreparedStatement statement = connection.prepareStatement(insertSQL)) {
                for (Row row : ExcelReader.getRows()) { // 假设ExcelReader类有一个方法返回所有行
                    String name = row.getCell(0).getStringCellValue();
                    int age = (int) row.getCell(1).getNumericCellValue();
                    statement.setString(1, name);
                    statement.setInt(2, age);
                    statement.executeUpdate();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

上述代码中的"yourdatabase"、"yourusername"和"yourpassword"需要替换为实际的数据库名称、用户名和密码。

标签: 数据导入

发表评论

丫丫技术百科 备案号:新ICP备2024010732号-62