Commit 05a80e61 by 宋珺琪

添加数据表统计功能

parent 8f746f16
package com.founder.commonutils.util;
import java.sql.*;
public class JDBCUtil {
private static final String DRIVER = "com.mysql.cj.jdbc.Driver";//驱动类类名
private static final String DBURL = "jdbc:mysql://47.92.48.137:3900/skyp?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";//连接URL,javaweb为数据库名
private static final String DBUSER = "root";//数据库用户
private static final String DBPASSWORD = "gkptCcYy123";//数据库密码
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName(DRIVER);
connection = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
public static void close(Connection connection, Statement ps, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
......@@ -14,7 +14,11 @@
</modules>
<artifactId>service</artifactId>
<dependencies>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.1</version>
</dependency>
<dependency>
<groupId>com.founder</groupId>
<artifactId>service_base</artifactId>
......
package com.founder.publicapi.controller.Database;
import com.founder.commonutils.model.newPublicEntity.MapRestResult;
import com.founder.commonutils.util.JDBCUtil;
import com.founder.commonutils.util.StringUtil;
import com.founder.servicebase.logs.OperLog;
import com.founder.servicebase.logs.OperationType;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
/**
* 数据表统计功能
*/
@RestController
@RequestMapping("/database")
@Api(tags = "数据表统计功能")
public class DatabaseController {
@OperLog(message = "表名模糊搜索",operation = OperationType.QUERY)
@PostMapping("/selecttable")
@ApiOperation(value = "表名模糊搜索")
public MapRestResult selecttable(@RequestParam(required = false) String table,
@RequestParam(name = "page", defaultValue = "1") Integer page,
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
/* int i = pageSize * (page - 1);
int j = pageSize;*/
List database = new ArrayList();
/* try
{
Class.forName("com.mysql.cj.jdbc.Driver"); //加载MYSQL JDBC驱动程序
System.out.println("Success loading Mysql Driver!");
}
catch (Exception e) {
e.printStackTrace();
return new MapRestResult().error("Error loading Mysql Driver!");
}*/
Connection connect = JDBCUtil.getConnection();
Statement stmt =null;
ResultSet rs = null;
try
{
// Connection connect = DriverManager.getConnection("jdbc:mysql://47.92.48.137:3900/skyp?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai", "root","gkptCcYy123");
// 连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码
stmt = connect.createStatement();
if (StringUtil.isEmpty(table)){
rs = stmt.executeQuery
("select TABLE_NAME '表名称',table_comment '表说明',CREATE_TIME '创建时间' from information_schema.tables where table_schema='skyp' and (TABLE_NAME like 'sk%' or TABLE_NAME like 'ksh%') ");//limit " + page + ","+pageSize+"
}else {
rs = stmt.executeQuery
("select TABLE_NAME '表名称',table_comment '表说明',CREATE_TIME '创建时间' from information_schema.tables where table_schema='skyp' and TABLE_NAME like '%"+table+"%' and (TABLE_NAME like 'sk%' or TABLE_NAME like 'ksh%')");
}
while (rs.next())
{
Map<Object, Object> map = new HashMap<>();
map.put("TABLE_NAME",rs.getString("表名称"));
map.put("TABLE_COMMENT",rs.getString("表说明"));
map.put("CREATE_TIME",rs.getString("创建时间"));
database.add(map);
}
int total = database.size();
if (total > pageSize) {
int toIndex = pageSize * page;
if (toIndex > total) {
toIndex = total;
}
database = database.subList(pageSize * (page - 1), toIndex);
}
Page<Map> page1 = new Page<>(page, pageSize);
page1.addAll(database);
page1.setPages((total + pageSize - 1) / pageSize);
page1.setTotal(total);
PageInfo<Map> pageInfo = new PageInfo(page1);
List<Map> list = pageInfo.getList();
return new MapRestResult(200,"ok",total,list);
}
catch (Exception e)
{
e.printStackTrace();
return new MapRestResult().error("get data error!");
}finally {
JDBCUtil.close(connect, stmt, rs);
}
}
@OperLog(message = "表详情",operation = OperationType.QUERY)
@PostMapping("/details")
@ApiOperation(value = "表详情")
public MapRestResult details(String table){
List<Map<Object, Object>> database = new ArrayList<>();
/* try
{
Class.forName("com.mysql.cj.jdbc.Driver"); //加载MYSQL JDBC驱动程序
System.out.println("Success loading Mysql Driver!");
}
catch (Exception e) {
e.printStackTrace();
return new MapRestResult().error("Error loading Mysql Driver!");
}*/
Connection connect = JDBCUtil.getConnection();
Statement stmt =null;
ResultSet rs = null;
try
{
// Connection connect = DriverManager.getConnection("jdbc:mysql://47.92.48.137:3900/skyp?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai", "root","gkptCcYy123");
// 连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码
stmt = connect.createStatement();
rs = stmt.executeQuery
("select COLUMN_NAME '列名',COLUMN_TYPE '类型' ,COLUMN_COMMENT '字段说明' from information_schema.COLUMNS where TABLE_NAME='"+table+"'");
while (rs.next())
{
Map<Object, Object> map = new HashMap<>();
map.put("COLUMN_NAME",rs.getString("列名"));
map.put("COLUMN_TYPE",rs.getString("类型"));
// map.put("DATA_TYPE",rs.getString("类型"));
map.put("COLUMN_COMMENT",rs.getString("字段说明"));
database.add(map);
}
}
catch (Exception e)
{
e.printStackTrace();
return new MapRestResult().error("get data error!");
}finally {
JDBCUtil.close(connect, stmt, rs);
}
return new MapRestResult(200,"ok",database.size(),database);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment