SpringBoot集成Access?DB實(shí)現(xiàn)數(shù)據(jù)導(dǎo)入和解析
1.什么是Access DB?
microsoft office access是由微軟發(fā)布的關(guān)聯(lián)式數(shù)據(jù)庫(kù)管理系統(tǒng)。它結(jié)合了 microsoft jet database engine 和 圖形用戶界面兩項(xiàng)特點(diǎn),是一種關(guān)系數(shù)據(jù)庫(kù)工具。它在很多地方得到廣泛使用,例如小型企業(yè),大公司的部門,和喜愛編程的開發(fā)人員專門利用它來制作處理數(shù)據(jù)的桌面系統(tǒng)。它也常被用來開發(fā)簡(jiǎn)單的web應(yīng)用程序.
優(yōu)點(diǎn):
存儲(chǔ)方式單一:access管理的對(duì)象有表、查詢、窗體、報(bào)表、頁(yè)、宏和模塊,以上對(duì)象都存放在后綴為(.mdb)的數(shù)據(jù)庫(kù)文件種,便于用戶的操作和管理。
面向?qū)ο螅篴ccess是一個(gè)面向?qū)ο蟮拈_發(fā)工具。它將一個(gè)應(yīng)用系統(tǒng)當(dāng)作是由一系列對(duì)象組成的,通過對(duì)象的方法、屬性完成數(shù)據(jù)庫(kù)的操作和管理,極大地簡(jiǎn)化了開發(fā)工作。同時(shí),這種基于面向?qū)ο蟮拈_發(fā)方式,使得開發(fā)應(yīng)用程序更為簡(jiǎn)便。
界面友好、易操作
access是一個(gè)可視化工具,用戶想要生成對(duì)象并應(yīng)用,只要使用鼠標(biāo)進(jìn)行拖放即可,非常直觀方便。系統(tǒng)還提供了表生成器、查詢生成器、報(bào)表設(shè)計(jì)器以及數(shù)據(jù)庫(kù)向?qū)?、表向?qū)?、查詢向?qū)А⒋绑w向?qū)?、?bào)表向?qū)У裙ぞ?,使得操作?jiǎn)便,容易使用和掌握。
access可以在一個(gè)數(shù)據(jù)表中嵌入位圖、聲音、excel表格、word文檔,還可以建立動(dòng)態(tài)的數(shù)據(jù)庫(kù)報(bào)表和窗體等。access還可以將程序應(yīng)用于網(wǎng)絡(luò),并與網(wǎng)絡(luò)上的動(dòng)態(tài)數(shù)據(jù)相聯(lián)接,輕松生成網(wǎng)頁(yè)。
缺點(diǎn):
access是小型數(shù)據(jù)庫(kù),既然是小型就有它根本的局限性:access數(shù)據(jù)庫(kù)不支持并發(fā)處理、數(shù)據(jù)庫(kù)易被下載存在安全隱患、數(shù)據(jù)存儲(chǔ)量相對(duì)較小等。而且在以下幾種情況下數(shù)據(jù)庫(kù)基本上會(huì)吃不消:
- 數(shù)據(jù)庫(kù)過大,一般access數(shù)據(jù)庫(kù)達(dá)到50m左右的時(shí)候性能會(huì)急劇下降。
- 網(wǎng)站訪問頻繁,經(jīng)常達(dá)到100人左右的在線。
- 記錄數(shù)過多,一般記錄數(shù)達(dá)到10萬條左右的時(shí)候性能就會(huì)急劇下降。
2.數(shù)據(jù)準(zhǔn)備
測(cè)試數(shù)據(jù)庫(kù)下載地址:
3.代碼工程
實(shí)驗(yàn)?zāi)繕?biāo)
實(shí)現(xiàn)access 數(shù)據(jù)庫(kù)導(dǎo)入
pom.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);
// 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;
以上只是一些關(guān)鍵代碼,所有代碼請(qǐng)參見下面代碼倉(cāng)庫(kù)
代碼倉(cāng)庫(kù)
- https://github.com/Harries/springboot-demo(Access DB)
4.測(cè)試
- 啟動(dòng)Spring Boot應(yīng)用
- postman訪問http://127.0.0.1:8088/api/access/upload
以上就是SpringBoot集成Access DB實(shí)現(xiàn)數(shù)據(jù)導(dǎo)入和解析的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot Access DB數(shù)據(jù)導(dǎo)入的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java代理模式(靜態(tài)代理、動(dòng)態(tài)代理、cglib代理)
代理(Proxy)是一種設(shè)計(jì)模式,提供了對(duì)目標(biāo)對(duì)象另外的訪問方式;這篇文章主要介紹了Java 中的三種代理模式,需要的朋友可以參考下,希望能給你帶來幫助2021-07-07Java?SE封裝、包、static關(guān)鍵字和代碼塊示例詳解
這篇文章主要給大家介紹了關(guān)于Java?SE封裝、包、static關(guān)鍵字和代碼塊的相關(guān)資料,需要的朋友可以參考下2023-11-11SpringBoot整合Mybatis實(shí)現(xiàn)高德地圖定位并將數(shù)據(jù)存入數(shù)據(jù)庫(kù)的步驟詳解
這篇文章主要介紹了SpringBoot整合Mybatis實(shí)現(xiàn)高德地圖定位并將數(shù)據(jù)存入數(shù)據(jù)庫(kù)的步驟詳解,本文分步驟通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01SpringBoot整合EasyCaptcha實(shí)現(xiàn)圖形驗(yàn)證碼功能
這篇文章主要介紹了SpringBoot整合EasyCaptcha實(shí)現(xiàn)圖形驗(yàn)證碼功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-02-02Java中構(gòu)造函數(shù),set/get方法和toString方法使用及注意說明
這篇文章主要介紹了Java中構(gòu)造函數(shù),set/get方法和toString方法的使用及注意說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01maven打包成第三方j(luò)ar包且把pom依賴包打入進(jìn)來的方法
這篇文章主要介紹了maven打包成第三方j(luò)ar包且把pom依賴包打入進(jìn)來的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11使用fileupload組件實(shí)現(xiàn)文件上傳功能
這篇文章主要為大家詳細(xì)介紹了使用fileupload實(shí)現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10