|
|
@@ -1,4 +1,5 @@
|
|
|
package com.siwei.apply.controller.cadastre;
|
|
|
+import com.siwei.apply.utils.MdbUtil;
|
|
|
import com.siwei.common.core.utils.StringUtils;
|
|
|
import com.siwei.common.core.web.controller.BaseController;
|
|
|
import org.json.simple.JSONObject;
|
|
|
@@ -20,16 +21,15 @@ import java.util.*;
|
|
|
public class MdbController extends BaseController {
|
|
|
|
|
|
|
|
|
-
|
|
|
@GetMapping("/gettablesMdb")
|
|
|
public void gettablesMdb() throws Exception {
|
|
|
String folderURL = "C:\\Users\\Administrator\\Desktop\\06\\XZQData.mdb";
|
|
|
- List<Map<String, Object>> resList = MdbTables(folderURL, null, null);
|
|
|
+ List<Map<String, Object>> resList = MdbUtil.MdbTables(folderURL, null, null);
|
|
|
|
|
|
Map<String, Object> map = resList.get(0);
|
|
|
String tablename = map.get("name").toString();
|
|
|
|
|
|
- Map<String, Object> resMap = MdbTableContent(folderURL,tablename, null, null, 1, 10);
|
|
|
+ Map<String, Object> resMap = MdbUtil.MdbTableContent(folderURL,tablename, null, null, 1, 10);
|
|
|
System.out.println(resMap);
|
|
|
|
|
|
System.out.println(resMap);
|
|
|
@@ -64,7 +64,7 @@ public class MdbController extends BaseController {
|
|
|
if (StringUtils.isEmpty(folderURL)) {
|
|
|
return;
|
|
|
}
|
|
|
- List<Map<String, Object>> res = MdbTables(folderURL, null, null);
|
|
|
+ List<Map<String, Object>> res = MdbUtil.MdbTables(folderURL, null, null);
|
|
|
JSONObject respJSON = new JSONObject();
|
|
|
respJSON.put("data", res);
|
|
|
respJSON.put("code", 200);
|
|
|
@@ -122,7 +122,7 @@ public class MdbController extends BaseController {
|
|
|
if (StringUtils.isEmpty(folderURL)) {
|
|
|
return;
|
|
|
}
|
|
|
- Map<String, Object> res = MdbTableContent(folderURL, tablename, null, null, page, limit);
|
|
|
+ Map<String, Object> res = MdbUtil.MdbTableContent(folderURL, tablename, null, null, page, limit);
|
|
|
JSONObject respJSON = new JSONObject();
|
|
|
respJSON.put("data", res);
|
|
|
respJSON.put("code", 200);
|
|
|
@@ -147,163 +147,5 @@ public class MdbController extends BaseController {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 读取.mdb文件下面的所有表名
|
|
|
- *
|
|
|
- * @param mdbPath mdb文件地址
|
|
|
- * @param username 用户名
|
|
|
- * @param password 密码
|
|
|
- */
|
|
|
- public List<Map<String, Object>> MdbTables(String mdbPath, String username, String password) throws Exception {
|
|
|
- List<Map<String, Object>> list = new ArrayList<>();
|
|
|
- Properties Prop = new Properties();
|
|
|
- //设置编码
|
|
|
- Prop.put("charSet", "UTF-8");
|
|
|
- if (!StringUtils.isEmpty(username)) {
|
|
|
- Prop.put("user", username);
|
|
|
- }
|
|
|
- if (!StringUtils.isEmpty(password)) {
|
|
|
- Prop.put("password", password);
|
|
|
- }
|
|
|
- //数据地址
|
|
|
- String dbUrl = "jdbc:ucanaccess://" + mdbPath;
|
|
|
- //引入驱动
|
|
|
- Class.forName("net.ucanaccess.jdbc.UcanaccessDriver").newInstance();
|
|
|
- Connection conn = null;
|
|
|
- ResultSet tables = null;
|
|
|
- PreparedStatement preparedStatement = null;
|
|
|
- ResultSet rs = null;
|
|
|
- try {
|
|
|
- //连接数据库资源
|
|
|
- conn = DriverManager.getConnection(dbUrl, Prop);
|
|
|
- tables = conn.getMetaData().getTables(mdbPath, null, null, new String[]{"TABLE"});
|
|
|
- //遍历表
|
|
|
- while (tables.next()) {
|
|
|
- Map<String, Object> tableMap = new HashMap<>();
|
|
|
- String tableName = tables.getString(3);
|
|
|
- String tableAlias = tables.getString(4);
|
|
|
- tableMap.put("name", tableName);
|
|
|
- tableMap.put("alias", tableName);
|
|
|
- if (!tableName.contains("GDB_")) {
|
|
|
- list.add(tableMap);
|
|
|
- }
|
|
|
- String cur = "";
|
|
|
- for (int u = 0; u < tables.getMetaData().getColumnCount(); u++) {
|
|
|
- String key = tables.getMetaData().getColumnLabel(u + 1);
|
|
|
- cur += ";" + key + ":" + tables.getString(key);
|
|
|
- }
|
|
|
- System.out.println(cur);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- System.out.println("resolverMdb is error!!!");
|
|
|
- } finally {
|
|
|
- closeA1l(conn, preparedStatement, tables, rs);
|
|
|
- }
|
|
|
- return list;
|
|
|
- }
|
|
|
|
|
|
-
|
|
|
- /**
|
|
|
- * 读取.mdb文件中指定表
|
|
|
- *
|
|
|
- * @param mdbPath mdb文件地址
|
|
|
- * @param username 用户名
|
|
|
- * @param password 密码
|
|
|
- */
|
|
|
- public Map<String, Object> MdbTableContent(String mdbPath, String tablename, String username, String password, Integer page, Integer limit) throws Exception {
|
|
|
- Map<String, Object> tableMap = new HashMap<>(16);
|
|
|
- Properties Prop = new Properties();
|
|
|
- //设置编码
|
|
|
- Prop.put("charSet", "UTF-8");
|
|
|
- if (!StringUtils.isEmpty(username)) {
|
|
|
- Prop.put("user", username);
|
|
|
- }
|
|
|
- if (!StringUtils.isEmpty(password)) {
|
|
|
- Prop.put("password", password);
|
|
|
- }
|
|
|
- //数据地址
|
|
|
- String dbUrl = "jdbc:ucanaccess://" + mdbPath;
|
|
|
- //引入驱动
|
|
|
- Class.forName("net.ucanaccess.jdbc.UcanaccessDriver").newInstance();
|
|
|
- Connection conn = null;
|
|
|
- ResultSet tables = null;
|
|
|
- PreparedStatement preparedStatement = null;
|
|
|
- ResultSet rs = null;
|
|
|
- PreparedStatement countPreparedStatement = null;
|
|
|
- ResultSet currs = null;
|
|
|
- try {
|
|
|
- //连接数据库资源
|
|
|
- conn = DriverManager.getConnection(dbUrl, Prop);
|
|
|
- Integer RowStart = (page - 1) * limit;
|
|
|
- Integer RowEnd = (page - 0) * limit;
|
|
|
- preparedStatement = conn.prepareStatement("select * from " + tablename + " t limit " + RowStart + "," + limit);
|
|
|
- List<String> columnList = new ArrayList<>();
|
|
|
- List<Map<String, String>> dataList = new ArrayList<>();
|
|
|
- rs = preparedStatement.executeQuery();
|
|
|
- ResultSetMetaData data = rs.getMetaData();
|
|
|
- for (int i = 1; i <= data.getColumnCount(); i++) {
|
|
|
- //列名
|
|
|
- String columnName = data.getColumnName(i);
|
|
|
- columnList.add(columnName);
|
|
|
- }
|
|
|
- while (rs.next()) {
|
|
|
- Map<String, String> map = new HashMap<>();
|
|
|
- for (int i = 1; i <= data.getColumnCount(); i++) {
|
|
|
- //列名
|
|
|
- String columnName = data.getColumnName(i);
|
|
|
- //map.put(columnName, rs.getString(i));
|
|
|
- Object value = rs.getObject(i);
|
|
|
- if (value instanceof Blob) {
|
|
|
- map.put(columnName, "[BLOB]"); //todo 这里先简单处理一下blob类型,后续可以根据实际需求进行优化
|
|
|
- } else {
|
|
|
- map.put(columnName, String.valueOf(value));
|
|
|
- }
|
|
|
- }
|
|
|
- dataList.add(map);
|
|
|
- }
|
|
|
- //查询总记录数
|
|
|
- countPreparedStatement = conn.prepareStatement("select count(1) from " + tablename + " t ");
|
|
|
- currs = countPreparedStatement.executeQuery();
|
|
|
- while (currs.next()) {
|
|
|
- tableMap.put("count", Integer.valueOf(currs.getString(1)));
|
|
|
- }
|
|
|
- tableMap.put("name", tablename);
|
|
|
- tableMap.put("column", columnList);
|
|
|
- tableMap.put("rows", dataList);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- System.out.println("resolverMdb is error!!!");
|
|
|
- } finally {
|
|
|
- closeA1l(conn, preparedStatement, tables, rs);
|
|
|
- closeA1l(conn, countPreparedStatement, null, currs);
|
|
|
- }
|
|
|
- return tableMap;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 关闭所有的资源链接
|
|
|
- *
|
|
|
- * @param conn 连接
|
|
|
- * @param preparedStatement 初始化 执行语句
|
|
|
- * @param tables 结果集
|
|
|
- * @param rs 结果集
|
|
|
- */
|
|
|
- public void closeA1l(Connection conn, PreparedStatement preparedStatement, ResultSet tables, ResultSet rs) {
|
|
|
- try {
|
|
|
- if (null != rs) {
|
|
|
- rs.close();
|
|
|
- }
|
|
|
- if (null != tables) {
|
|
|
- tables.close();
|
|
|
- }
|
|
|
- if (null != preparedStatement) {
|
|
|
- preparedStatement.close();
|
|
|
- }
|
|
|
- if (null != conn) {
|
|
|
- conn.close();
|
|
|
- }
|
|
|
- } catch (Exception ignore) {
|
|
|
- }
|
|
|
- }
|
|
|
}
|