Spring Boot集成Access DB实现数据导入和解析

1.什么是Access DB?

microsoft office access是由微软发布的关联式数据库管理系统。它结合了 microsoft jet database engine 和 图形用户界面两项特点,是一种关系数据库工具。它在很多地方得到广泛使用,例如小型企业,大公司的部门,和喜爱编程的开发人员专门利用它来制作处理数据的桌面系统。它也常被用来开发简单的web应用程序.

优点:

  • 存储方式单一:access管理的对象有表、查询、窗体、报表、页、宏和模块,以上对象都存放在后缀为(.mdb)的数据库文件种,便于用户的操作和管理。

  • 面向对象:access是一个面向对象的开发工具。它将一个应用系统当作是由一系列对象组成的,通过对象的方法、属性完成数据库的操作和管理,极大地简化了开发工作。同时,这种基于面向对象的开发方式,使得开发应用程序更为简便。

  • 界面友好、易操作

  • access是一个可视化工具,用户想要生成对象并应用,只要使用鼠标进行拖放即可,非常直观方便。系统还提供了表生成器、查询生成器、报表设计器以及数据库向导、表向导、查询向导、窗体向导、报表向导等工具,使得操作简便,容易使用和掌握。

  • access可以在一个数据表中嵌入位图、声音、excel表格、word文档,还可以建立动态的数据库报表和窗体等。access还可以将程序应用于网络,并与网络上的动态数据相联接,轻松生成网页。

缺点:

access是小型数据库,既然是小型就有它根本的局限性:access数据库不支持并发处理、数据库易被下载存在安全隐患、数据存储量相对较小等。而且在以下几种情况下数据库基本上会吃不消:

  1. 数据库过大,一般access数据库达到50m左右的时候性能会急剧下降。
  2. 网站访问频繁,经常达到100人左右的在线。
  3. 记录数过多,一般记录数达到10万条左右的时候性能就会急剧下降。

2.数据准备

测试数据库下载地址:

3.代码工程

实验目标

实现access 数据库导入

pom.xml

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-demo</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>accessDB</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.sf.ucanaccess</groupId>
            <artifactId>ucanaccess</artifactId>
            <version>5.0.1</version>
        </dependency>

    </dependencies>
</project>

controller

主要步骤如下

  • Saving the uploaded file.

  • Connecting to the Access database.

  • Querying data.

  • Processing and storing results in a list.

    package com.demo.controller;

    import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;

    @RestController @RequestMapping("/api/access") public class AccessDatabaseController {

    // Upload Access database file and query data @PostMapping("/upload") public List<Map<String, Object>> uploadAndQuery(@RequestParam("file") MultipartFile file, @RequestParam("tableName") String tableName) { List<Map<String, Object>> results = new ArrayList<>(); Connection connection = null; try { // Save the uploaded file to the server's temporary directory File tempFile = File.createTempFile("upload-", ".accdb"); file.transferTo(tempFile);

    ini 复制代码
         // Connect to the Access database
         String dbUrl = "jdbc:ucanaccess://" + tempFile.getAbsolutePath();
         connection = DriverManager.getConnection(dbUrl);
    
         // Query the specified table in the database
         Statement statement = connection.createStatement();
         String query = "SELECT * FROM " + tableName;
         ResultSet resultSet = statement.executeQuery(query);
    
         // Store query results in a List<Map<String, Object>>
         while (resultSet.next()) {
            Map<String, Object> row = new HashMap<>();
            int columnCount = resultSet.getMetaData().getColumnCount();
            for (int i = 1; i <= columnCount; i++) {
               String columnName = resultSet.getMetaData().getColumnName(i);
               row.put(columnName, resultSet.getObject(i));
            }
            results.add(row);
         }
    
         // Close the ResultSet and Statement
         resultSet.close();
         statement.close();
    
         // Mark temporary file for deletion upon JVM exit
         tempFile.deleteOnExit();
    
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         // Close the database connection
         if (connection != null) {
            try {
               connection.close();
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      }
      return results;

    } }

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

4.测试

5.引用

相关推荐
Chen-Edward1 分钟前
有了Spring为什么还有要Spring Boot?
java·spring boot·spring
magic3341656320 分钟前
Springboot整合MinIO文件服务(windows版本)
windows·spring boot·后端·minio·文件对象存储
开心-开心急了30 分钟前
Flask入门教程——李辉 第一、二章关键知识梳理(更新一次)
后端·python·flask
掘金码甲哥41 分钟前
调试grpc的哼哈二将,你值得拥有
后端
陈小桔1 小时前
idea中重新加载所有maven项目失败,但maven compile成功
java·maven
小学鸡!1 小时前
Spring Boot实现日志链路追踪
java·spring boot·后端
xiaogg36781 小时前
阿里云k8s1.33部署yaml和dockerfile配置文件
java·linux·kubernetes
逆光的July1 小时前
Hikari连接池
java
微风粼粼2 小时前
eclipse 导入javaweb项目,以及配置教程(傻瓜式教学)
java·ide·eclipse
番茄Salad2 小时前
Spring Boot临时解决循环依赖注入问题
java·spring boot·spring cloud