前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >使用java在未知表字段情况下通过sql查询信息

使用java在未知表字段情况下通过sql查询信息

原创
作者头像
刘大猫
发布2024-11-27 20:45:41
发布2024-11-27 20:45:41
540
举报
文章被收录于专栏:JAVA相关JAVA相关

场景

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

解决方案

代码语言:java
复制
@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);
        }
    }

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 场景
  • 解决方案
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档