|
|
@@ -0,0 +1,284 @@
|
|
|
+package com.siwei.apply.controller.cadastre;
|
|
|
+import com.siwei.common.core.utils.StringUtils;
|
|
|
+import com.siwei.common.core.web.controller.BaseController;
|
|
|
+import org.json.simple.JSONObject;
|
|
|
+import org.json.simple.parser.JSONParser;
|
|
|
+import org.json.simple.parser.ParseException;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.sql.*;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Controller
|
|
|
+@RequestMapping("/mdb")
|
|
|
+public class MdbController extends BaseController {
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * 获取mdb下的属性表
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ @PostMapping("/gettables")
|
|
|
+ public void gettables(HttpServletRequest request, HttpServletResponse response) {
|
|
|
+ try {
|
|
|
+ response.setCharacterEncoding("UTF-8"); //告诉服务器用UTF-8进行编码解析
|
|
|
+ //告诉浏览器使用什么编码去解析
|
|
|
+ //response.setHeader("content-type", "text/html; charset=UTF-8");
|
|
|
+ String folderURL = null;
|
|
|
+ JSONObject layerJson = null;
|
|
|
+ String strContentType = request.getContentType();
|
|
|
+ if (strContentType != null && strContentType.equalsIgnoreCase("application/json")) {
|
|
|
+ JSONObject reqJson = (JSONObject) new JSONParser().parse(request.getReader());
|
|
|
+ if (reqJson != null) {
|
|
|
+ folderURL = (String) reqJson.get("DATAURI");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(folderURL)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<Map<String, Object>> res = MdbTables(folderURL, null, null);
|
|
|
+ JSONObject respJSON = new JSONObject();
|
|
|
+ respJSON.put("data", res);
|
|
|
+ respJSON.put("code", 200);
|
|
|
+ respJSON.put("msg", "获取成功");
|
|
|
+ respJSON.writeJSONString(response.getWriter());
|
|
|
+ response.setContentType("application/json;charset=UTF-8");
|
|
|
+ response.setStatus(200);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ JSONObject respJSON = new JSONObject();
|
|
|
+ respJSON.put("data", null);
|
|
|
+ respJSON.put("msg", "获取失败");
|
|
|
+ respJSON.put("code", 500);
|
|
|
+ try {
|
|
|
+ respJSON.writeJSONString(response.getWriter());
|
|
|
+ } catch (IOException e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ response.setContentType("application/json;charset=UTF-8");
|
|
|
+ response.setStatus(500);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取mdb下的指定表
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ @PostMapping("/gettable")
|
|
|
+ public void gettable(HttpServletRequest request, HttpServletResponse response) {
|
|
|
+ try {
|
|
|
+ response.setCharacterEncoding("UTF-8"); //告诉服务器用UTF-8进行编码解析
|
|
|
+ String folderURL = null;
|
|
|
+ String tablename = null;
|
|
|
+ Integer page = 1;
|
|
|
+ Integer limit = 20;
|
|
|
+ String strContentType = request.getContentType();
|
|
|
+ if (strContentType != null && strContentType.equalsIgnoreCase("application/json")) {
|
|
|
+ JSONObject reqJson = (JSONObject) new JSONParser().parse(request.getReader());
|
|
|
+ if (reqJson != null) {
|
|
|
+ folderURL = reqJson.get("DATAURI").toString();
|
|
|
+ tablename = reqJson.get("TABLENAME").toString();
|
|
|
+ if (reqJson.get("PAGE") != null) {
|
|
|
+ page = Long.valueOf(reqJson.get("PAGE").toString()).intValue();
|
|
|
+ }
|
|
|
+ if (reqJson.get("LIMIT") != null) {
|
|
|
+ limit = Long.valueOf(reqJson.get("LIMIT").toString()).intValue();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(folderURL)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Map<String, Object> res = MdbTableContent(folderURL, tablename, null, null, page, limit);
|
|
|
+ JSONObject respJSON = new JSONObject();
|
|
|
+ respJSON.put("data", res);
|
|
|
+ respJSON.put("code", 200);
|
|
|
+ respJSON.put("msg", "获取成功");
|
|
|
+ respJSON.writeJSONString(response.getWriter());
|
|
|
+ response.setContentType("application/json;charset=UTF-8");
|
|
|
+ response.setStatus(200);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ JSONObject respJSON = new JSONObject();
|
|
|
+ respJSON.put("data", null);
|
|
|
+ respJSON.put("msg", "获取失败");
|
|
|
+ respJSON.put("code", 500);
|
|
|
+ try {
|
|
|
+ respJSON.writeJSONString(response.getWriter());
|
|
|
+ } catch (IOException e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ response.setContentType("application/json;charset=UTF-8");
|
|
|
+ response.setStatus(500);
|
|
|
+ }
|
|
|
+ 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));
|
|
|
+ }
|
|
|
+ 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) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|