月球大数据地理空间分析展示平台-【后端】-月球后台服务
1
13693261870
2024-11-13 024e90554d19c2342f27a26f91bbea378f84da82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.moon.server.helper;
 
import com.moon.server.entity.all.StaticData;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
import java.sql.*;
import java.util.*;
 
@SuppressWarnings("ALL")
public class AccessHelper {
    private final static Log log = LogFactory.getLog(AccessHelper.class);
 
    public static List<Map<String, Object>> readMdb(String mdbPath, String username, String password) {
        List<Map<String, Object>> list = new ArrayList<>();
 
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet tables = null;
        ResultSet rs = null;
 
        try {
            Properties prop = new Properties();
            prop.put("charSet", "UTF-8");
            prop.put("user", username);
            prop.put("password", password);
 
            String dbUrl = "jdbc:ucanaccess://" + mdbPath;
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver").newInstance();
 
            conn = DriverManager.getConnection(dbUrl, prop);
            tables = conn.getMetaData().getTables(mdbPath, null, null, new String[]{"TABLE"});
            while (tables.next()) {
                Map<String, Object> tableMap = new HashMap<>(4);
                Set<String> columnList = new HashSet<>();
                List<Map<String, String>> dataList = new ArrayList<>();
 
                String tableName = tables.getString(3);
                ps = conn.prepareStatement("select * from " + tableName);
                rs = ps.executeQuery();
 
                ResultSetMetaData data = rs.getMetaData();
                while (rs.next()) {
                    Map<String, String> map = new HashMap<>(5);
                    for (int i = 1; i <= data.getColumnCount(); i++) {
                        String columnName = data.getColumnName(i);
                        if (StaticData.MDB_EXCLUDE_FIELDS.contains(columnName)) {
                            continue;
                        }
 
                        map.put(columnName, rs.getString(i));
                        columnList.add(columnName);
                    }
                    dataList.add(map);
                }
 
                tableMap.put("name", tableName);
                tableMap.put("column", columnList);
                tableMap.put("data", dataList);
                list.add(tableMap);
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        } finally {
            closeA1l(conn, ps, tables, rs);
        }
 
        return list;
    }
 
    private static void closeA1l(Connection conn, PreparedStatement ps, ResultSet tables, ResultSet rs) {
        try {
            if (null != rs) {
                rs.close();
            }
            if (null != tables) {
                tables.close();
            }
            if (null != ps) {
                ps.close();
            }
            if (null != conn) {
                conn.close();
            }
        } catch (Exception ex) {
            //
        }
    }
}