package com.example11.demoly.search; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.sql.*; import java.util.*; public class PhoneNumberExtractor { public static void main(String[] args) { // MySQL数据库连接信息 String url = "jdbc:mysql://localhost:3306/db_name"; String username = "root"; String password = "root"; // 连接数据库 try (Connection conn = DriverManager.getConnection(url, username, password)) { DatabaseMetaData metaData = conn.getMetaData(); ResultSet tables = metaData.getTables(null, null, "%", new String[]{"TABLE"}); // 遍历每个表 while (tables.next()) { // 存储电话号码的 Map Map<String, List<String>> phoneNumberMap = new HashMap<>(); String tableName = tables.getString("TABLE_NAME"); System.out.println("Processing table: " + tableName); // 查询每个表的前100条数据 String query = "SELECT * FROM " + tableName + " LIMIT 100"; try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(query)) { ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); // 遍历结果集中的每一行 while (rs.next()) { // 遍历每一列 for (int i = 1; i <= columnCount; i++) { String columnName = rsmd.getColumnName(i); String columnValue = rs.getString(i); // 使用电话号码的正则表达式进行匹配 String regex = "\\b(?:\\+?\\d{1,3}[\\s-]?)?(?:\\(\\d{1,4}\\)[\\s-]?)?\\d{6,14}\\b"; if (columnValue != null && columnValue.matches(regex)) { // 如果该字段名在 Map 中不存在,创建一个新的 List phoneNumberMap.putIfAbsent(columnName, new ArrayList<>()); try (Scanner scanner = new Scanner(columnValue)) { while (scanner.hasNextLine()) { String line = scanner.nextLine().trim(); if (line.matches(regex)) { // 将电话号码添加到对应字段名的 List 中 phoneNumberMap.get(columnName).add(line); } } } } } } // 输出 Map 中的数据到文件 writeDataToFile(phoneNumberMap, tableName); System.out.println("Data extraction for table " + tableName + " completed."); } catch (SQLException ex) { ex.printStackTrace(); } } } catch (SQLException ex) { ex.printStackTrace(); } } private static void writeDataToFile(Map<String, List<String>> phoneNumberMap, String tableName) { // 输出到文件 String outputFile = "C:\\Users\\output\\Desktop\\7月\\" + tableName + ".txt"; try (FileWriter fileWriter = new FileWriter(outputFile); PrintWriter printWriter = new PrintWriter(fileWriter)) { // 遍历 Map,将数据写入文件 for (Map.Entry<String, List<String>> entry : phoneNumberMap.entrySet()) { String fieldName = entry.getKey(); List<String> phoneNumbers = entry.getValue(); printWriter.println("Field: " + fieldName); int count = 0; for (String phoneNumber : phoneNumbers) { if (count > 10) { break; } printWriter.println(phoneNumber); count++; } printWriter.println(); // 空行分隔不同字段的电话号码 } System.out.println("Phone numbers extracted and saved to " + outputFile); } catch (IOException ex) { ex.printStackTrace(); } } }
- Map 存储:引入一个
Map<String, List<String>> phoneNumberMap
,其中键为字段名,值为存储电话号码的List<String>
。 - 存储过程:在找到匹配的电话号码时,将号码添加到相应字段名的 List 中。
- 输出到文件:添加了一个
writeDataToFile
方法,负责将存储在phoneNumberMap
中的数据输出到文件。
输出结果
Field: number
13********16
136*****456
123******41
Field: numbercpoy
13**********48
136********56
1***********51