public static void main(String[] args) {
        String jdbcURL = "jdbc:mysql://localhost:3306/jxxx?useUnicode=true&characterEncoding=utf8&useSSL=false";
        String username = "root";
        String password = "root1234";

        try {
            // 连接到数据库
            Connection connection = DriverManager.getConnection(jdbcURL, username, password);

            // 获取所有表名
            Statement statement = connection.createStatement();
            String getTablesSQL = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'jxxx'";
            ResultSet tablesResultSet = statement.executeQuery(getTablesSQL);

            List<String> tables = new ArrayList<>();
            while (tablesResultSet.next()) {
                tables.add(tablesResultSet.getString("table_name"));
            }

            // 遍历每个表进行查询
            for (String tableName : tables) {
                // 获取表的列名
                String getColumnsSQL = "SELECT column_name FROM information_schema.columns WHERE table_schema = 'jxxx' AND table_name = '" + tableName + "'";
                ResultSet columnsResultSet = statement.executeQuery(getColumnsSQL);

                List<String> columns = new ArrayList<>();
                while (columnsResultSet.next()) {

                    columns.add(columnsResultSet.getString("column_name"));
                }

                // 遍历列名构建查询
                for (String columnName : columns) {
                		//CAST("+columnName+" AS CHAR)" 将数据值转换为char 方便使用like查询
                    //%%中间填写需要模糊查询的值
                    String query = "SELECT * FROM `" + tableName + "` WHERE CAST("+columnName+" AS CHAR)" + " like '%%'";

                    PreparedStatement preparedStatement = connection.prepareStatement(query);

                    ResultSet resultSet = preparedStatement.executeQuery();
                    // 检查是否有结果
                    if (resultSet.next()) {
                        System.out.println("Data found in table: " + tableName + ", column: " + columnName);
                        break; // 找到数据后跳出列循环
                    }
                    resultSet.close();
                    preparedStatement.close();
                }

                columnsResultSet.close();
            }

            // 关闭连接
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.