|
|
@ -29,10 +29,10 @@ public class TableStructureExportUtil { |
|
|
|
// private static final String JDBC_PASSWORD = "Cjy@1303"; // 替换为你的数据库密码
|
|
|
|
// private static final String JDBC_PASSWORD = "Cjy@1303"; // 替换为你的数据库密码
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // 产监用
|
|
|
|
// 产监用
|
|
|
|
// private static final String JDBC_URL = "jdbc:mysql://192.168.130.16:3306/ly_ddc_xa"; // 替换为你的数据库URL
|
|
|
|
private static final String JDBC_URL = "jdbc:mysql://192.168.130.16:3306/ly_ddc_xa"; // 替换为你的数据库URL
|
|
|
|
// private static final String JDBC_USER = "root"; // 替换为你的数据库用户名
|
|
|
|
private static final String JDBC_USER = "root"; // 替换为你的数据库用户名
|
|
|
|
// private static final String JDBC_PASSWORD = "Cjy@1303"; // 替换为你的数据库密码
|
|
|
|
private static final String JDBC_PASSWORD = "Cjy@1303"; // 替换为你的数据库密码
|
|
|
|
|
|
|
|
|
|
|
|
// 旅游报表
|
|
|
|
// 旅游报表
|
|
|
|
// private static final String JDBC_URL = "jdbc:mysql://192.168.130.16:3306/lvyoubaobiao_xa"; // 替换为你的数据库URL
|
|
|
|
// private static final String JDBC_URL = "jdbc:mysql://192.168.130.16:3306/lvyoubaobiao_xa"; // 替换为你的数据库URL
|
|
|
@ -40,29 +40,44 @@ public class TableStructureExportUtil { |
|
|
|
// private static final String JDBC_PASSWORD = "Cjy@1303"; // 替换为你的数据库密码
|
|
|
|
// private static final String JDBC_PASSWORD = "Cjy@1303"; // 替换为你的数据库密码
|
|
|
|
|
|
|
|
|
|
|
|
// // 产监表
|
|
|
|
// // 产监表
|
|
|
|
private static final String JDBC_URL = "jdbc:mysql://192.168.130.16:3306/hb_lycyjcpt_xa"; // 替换为你的数据库URL
|
|
|
|
// private static final String JDBC_URL = "jdbc:mysql://192.168.130.16:3306/hb_lycyjcpt_xa"; // 替换为你的数据库URL
|
|
|
|
private static final String JDBC_USER = "root"; // 替换为你的数据库用户名
|
|
|
|
// private static final String JDBC_USER = "root"; // 替换为你的数据库用户名
|
|
|
|
private static final String JDBC_PASSWORD = "Cjy@1303"; // 替换为你的数据库密码
|
|
|
|
// private static final String JDBC_PASSWORD = "Cjy@1303"; // 替换为你的数据库密码
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
public static void main(String[] args) { |
|
|
|
try (Connection conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD)) { |
|
|
|
try (Connection conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD)) { |
|
|
|
// 获取所有表名 TODO 这里修改表明
|
|
|
|
// 获取所有表名 TODO 这里修改表明
|
|
|
|
String getTablesSql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'hb_lycyjcpt_xa';"; |
|
|
|
String getTablesSql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'ly_ddc_xa';"; |
|
|
|
try (Statement stmt = conn.createStatement(); |
|
|
|
try (Statement stmt = conn.createStatement(); |
|
|
|
ResultSet rs = stmt.executeQuery(getTablesSql)) { |
|
|
|
ResultSet rs = stmt.executeQuery(getTablesSql)) { |
|
|
|
List<String> tableNames = new ArrayList<>(); |
|
|
|
List<String> tableNames = new ArrayList<>(); |
|
|
|
while (rs.next()) { |
|
|
|
while (rs.next()) { |
|
|
|
tableNames.add(rs.getString("table_name")); |
|
|
|
tableNames.add(rs.getString("table_name")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
int count = 0; // 有数据的表数量
|
|
|
|
List<TableInfo> list = new ArrayList<>(); |
|
|
|
List<TableInfo> list = new ArrayList<>(); |
|
|
|
// 遍历表名并获取每张表的元数据
|
|
|
|
// 遍历表名并获取每张表的元数据
|
|
|
|
for (String tableName : tableNames) { |
|
|
|
for (String tableName : tableNames) { |
|
|
|
|
|
|
|
// 查询每个表的行数
|
|
|
|
|
|
|
|
String countSql = "SELECT COUNT(*) AS row_count FROM " + tableName; |
|
|
|
|
|
|
|
try (Statement countStmt = conn.createStatement(); |
|
|
|
|
|
|
|
ResultSet countRs = countStmt.executeQuery(countSql)) { |
|
|
|
|
|
|
|
if (countRs.next()) { |
|
|
|
|
|
|
|
int rowCount = countRs.getInt("row_count"); |
|
|
|
|
|
|
|
if (rowCount > 0) { |
|
|
|
|
|
|
|
count++; // 有数据表数量递增
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
List<TableInfo> newList = getTableMetadata(conn, tableName); |
|
|
|
List<TableInfo> newList = getTableMetadata(conn, tableName); |
|
|
|
if (newList != null && !newList.isEmpty()) { |
|
|
|
if (newList != null && !newList.isEmpty()) { |
|
|
|
list.addAll(newList); |
|
|
|
list.addAll(newList); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
System.out.println("有数据的表数量:" + count); |
|
|
|
// 导出到 Excel
|
|
|
|
// 导出到 Excel
|
|
|
|
exportToExcel(list); |
|
|
|
exportToExcel(list); |
|
|
|
} |
|
|
|
} |
|
|
@ -116,6 +131,7 @@ public class TableStructureExportUtil { |
|
|
|
|
|
|
|
|
|
|
|
// 获取表元数据并打印
|
|
|
|
// 获取表元数据并打印
|
|
|
|
private static List<TableInfo> getTableMetadata(Connection conn, String tableName) { |
|
|
|
private static List<TableInfo> getTableMetadata(Connection conn, String tableName) { |
|
|
|
|
|
|
|
|
|
|
|
String sql = "SELECT " + |
|
|
|
String sql = "SELECT " + |
|
|
|
" c.TABLE_SCHEMA AS `DatabaseName`, " + |
|
|
|
" c.TABLE_SCHEMA AS `DatabaseName`, " + |
|
|
|
" c.TABLE_NAME AS `TableName`, " + |
|
|
|
" c.TABLE_NAME AS `TableName`, " + |
|
|
@ -142,7 +158,7 @@ public class TableStructureExportUtil { |
|
|
|
|
|
|
|
|
|
|
|
try (PreparedStatement stmt = conn.prepareStatement(sql)) { |
|
|
|
try (PreparedStatement stmt = conn.prepareStatement(sql)) { |
|
|
|
// 设置查询参数 TODO 这里需要修改
|
|
|
|
// 设置查询参数 TODO 这里需要修改
|
|
|
|
stmt.setString(1, "hb_lycyjcpt_xa"); // 替换为你的数据库名称
|
|
|
|
stmt.setString(1, "ly_ddc_xa"); // 替换为你的数据库名称
|
|
|
|
stmt.setString(2, tableName); |
|
|
|
stmt.setString(2, tableName); |
|
|
|
|
|
|
|
|
|
|
|
try (ResultSet rs = stmt.executeQuery()) { |
|
|
|
try (ResultSet rs = stmt.executeQuery()) { |
|
|
@ -166,10 +182,12 @@ public class TableStructureExportUtil { |
|
|
|
if (rowCount > 0) { |
|
|
|
if (rowCount > 0) { |
|
|
|
tableInfo.setDataQuantity(String.valueOf(rowCount)); |
|
|
|
tableInfo.setDataQuantity(String.valueOf(rowCount)); |
|
|
|
tableInfos.add(tableInfo); |
|
|
|
tableInfos.add(tableInfo); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return tableInfos; |
|
|
|
return tableInfos; |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
} catch (SQLException e) { |
|
|
|