public static void main(String[] args) {
String jdbcUrl = "jdbc:mysql://127.0.0.1:9999/test?characterEncoding=utf-8&useSSL=false";
String username = "test";
String password = "test";
try (Connection connection = DriverManager.getConnection(jdbcUrl, username, password)) {
// 查询需要删除的表名
PreparedStatement ps = connection.prepareStatement("SELECT table_name FROM information_schema.tables WHERE table_name REGEXP ?");
// ps.setString(1, "^test_[0-9]{4}$");
ps.setString(1, "^test_[0-9]{4}$");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String tableName = rs.getString("table_name");
System.out.println("DROP TABLE IF EXISTS " + tableName + ";");
executeDropTable(connection, tableName);
}
// 如果你想把 SQL 语句写入文件
// writeToFile("drop_tables.sql");
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void writeToFile(String filePath) throws SQLException {
// 实现文件写入逻辑
// 这里只是一个简单的示例,你需要添加具体的文件写入代码
System.out.println("Writing to file: " + filePath);
}
private static void executeDropTable(Connection connection, String tableName) throws SQLException {
try (Statement statement = connection.createStatement()) {
String dropTableSql = "DROP TABLE IF EXISTS " + tableName;
int result = statement.executeUpdate(dropTableSql);
if (result == 0) {
System.out.println("Table " + tableName + " dropped successfully.");
} else {
System.err.println("Error dropping table " + tableName);
}
}
}
08-06
2224
07-11
1万+
12-27
2341
04-01