在这里插入图片描述

场景

<font color='red'>在只知道表名,不知道表包含哪些字段情况下,查询该表信息的场景</font>

解决方案

@Test
    public void test() {
        Connection connection;
        String DB_URL = "jdbc:mysql://192.168.20.75:9950/geespace_bd_platform_dev?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&";

        List<Map<String, Object>> data = new ArrayList<>();
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");

            connection = DriverManager.getConnection(DB_URL, "geespace", "gee123456");
            stmt = connection.createStatement();
            // 获取sql

//            rs = stmt.executeQuery("SELECT nth_interval_of_temperature1,number_of_temperature1 FROM 114_interval_statistical_table GROUP BY nth_interval_of_temperature1,number_of_temperature1 order BY nth_interval_of_temperature1 asc");
            rs = stmt.executeQuery("SELECT * from ge_drag_spark_task");
            ResultSetMetaData rsmd = rs.getMetaData();
            while (rs.next()) {
                for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                    Map<String, Object> map = new HashMap<>(1);
                    map.put(rsmd.getColumnName(i), rs.getObject(i));
                    data.add(map);
                }
            }
            for (Map<String, Object> map: data) {
                log.info(" map:{}", map);
            }
        } catch (SQLException e) {
            log.error("[getColumnData Exception] --> the exception message is:{}", e.getMessage());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        finally {
            JdbcUtils.close(rs);
            JdbcUtils.close(stmt);
        }
    }

![上传中...]()


刘大猫
6 声望1 粉丝

如果有天突然发现路的尽头还是路的话,希望你还没错过太多沿路的风景和眼前珍惜的人。