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.测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值