package net.xuele.member.base.service; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class A { private static String path = "/Users/zhengtao/data/sql"; static List<String> tables=new ArrayList<>(); public static void main(String[] args) throws SQLException, FileNotFoundException { test1(); System.setOut(new PrintStream(new FileOutputStream(new File("/Users/zhengtao/data/表结构.csv")))); test(); } public static void test1() throws SQLException { Connection conn = DbConnectionUtil.begin(); ResultSet rs1 = conn.createStatement().executeQuery("show tables"); while (rs1.next()) { String tablename = rs1.getString(1); tables.add(tablename); } } public static void test() throws SQLException { Connection conn = DbConnectionUtil.begin(true); for (String tablename : tables) { try { String comment = getCommentByTableName(tablename); System.out.println("\n" + tablename + "," + comment); ResultSet rs = null; rs = conn.createStatement().executeQuery( String.format("show full columns from %s", tablename)); while (rs.next()) { System.out.println(rs.getString("field") + "," + rs.getString("type") + "," + rs.getString("comment")); } } catch (SQLException throwables) { throwables.printStackTrace(); System.err.println(tablename); continue; } } } public static String getCommentByTableName(String tableName) throws SQLException { Connection conn = DbConnectionUtil.begin(true); Statement stmt = conn.createStatement(); ResultSet rs = null; rs = stmt.executeQuery("SHOW CREATE TABLE " + tableName); rs.next(); String createDDL = rs.getString(2); String comment = parse(createDDL); rs = stmt.executeQuery("explain select * from " + tableName); rs.next(); if (rs.getString(1).equals("defaultdn")) { return comment + "," + "单片"; } if (rs.next()) { return comment + "," + "分片"; } return comment + "," + "全片"; } public static String parse(String all) { String comment = null; int index = all.indexOf("COMMENT='"); if (index < 0) { return ""; } comment = all.substring(index + 9); comment = comment.substring(0, comment.length() - 1); return comment; } }
mysql表结构批量拉取转excel
最新推荐文章于 2024-04-14 01:14:45 发布