import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import java.util.StringJoiner; /** * 只读探针:导出某个库的结构(SHOW CREATE TABLE)或指定表的数据(INSERT),用于迁移前备份。 * *
用法: *
* java SchemaDump <jdbc-url> # 全部表的结构 * java SchemaDump <jdbc-url> --data=t1,t2 # 指定表的数据 **/ public class SchemaDump { public static void main(String[] args) throws Exception { String url = args[0]; String dataTables = null; for (String arg : args) { if (arg.startsWith("--data=")) dataTables = arg.substring("--data=".length()); } try (Connection connection = DriverManager.getConnection(url, "root", "123456")) { if (dataTables == null) { dumpSchema(connection); } else { for (String table : dataTables.split(",")) { dumpData(connection, table.trim()); } } } } private static void dumpSchema(Connection connection) throws Exception { System.out.println("-- 结构备份:" + connection.getCatalog() + ",导出时间 " + java.time.LocalDateTime.now()); System.out.println("-- 还原方式:在空库里按顺序执行本文件(表按名称排序,先建表后加外键由 MySQL 自行处理)。"); System.out.println("SET FOREIGN_KEY_CHECKS = 0;"); for (String table : tables(connection)) { try (Statement statement = connection.createStatement(); ResultSet rows = statement.executeQuery("show create table `" + table + "`")) { rows.next(); System.out.println(); System.out.println("-- " + table); System.out.println("DROP TABLE IF EXISTS `" + table + "`;"); System.out.println(rows.getString(2) + ";"); } } System.out.println(); System.out.println("SET FOREIGN_KEY_CHECKS = 1;"); } private static void dumpData(Connection connection, String table) throws Exception { System.out.println(); System.out.println("-- 数据备份 " + table + "(导出时间 " + java.time.LocalDateTime.now() + ")"); try (Statement statement = connection.createStatement(); ResultSet rows = statement.executeQuery("select * from `" + table + "`")) { int columns = rows.getMetaData().getColumnCount(); List