| | |
| | | package com.lf.server.helper; |
| | | |
| | | import com.lf.server.entity.all.StaticData; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | |
| | | import java.sql.*; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * Access帮助类 |
| | | * @author WWW |
| | | */ |
| | | public class AccessHelper { |
| | | // |
| | | } |
| | | private final static Log log = LogFactory.getLog(AccessHelper.class); |
| | | |
| | | /** |
| | | * 读取.mdb文件 |
| | | * |
| | | * @param mdbPath mdb文件地址 |
| | | * @param username 用户名 |
| | | * @param password 密码 |
| | | */ |
| | | 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<>(); |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 关闭所有的资源链接 |
| | | * |
| | | * @param conn 连接 |
| | | * @param ps 执行语句 |
| | | * @param tables 结果集 |
| | | * @param rs 结果集 |
| | | */ |
| | | 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) { |
| | | // |
| | | } |
| | | } |
| | | } |