如何在 JDBC 的单个连接中检索 MySQL 临时表元数据?

发布于 2024-10-13 18:56:27 字数 1461 浏览 7 评论 0原文

我再一次发现了一个我不知道如何解决的问题。假设我们有以下测试代码:

private static final String CREATE_TEMPORARY_TABLE =
        "CREATE TEMPORARY TABLE T1 (\n" +
        "\tA FLOAT(4, 1),\n" +
        "\tB FLOAT(5, 2),\n" +
        "\tC FLOAT,\n" +
        "\tD INTEGER\n" +
        ") ENGINE = MEMORY;";

private final String[] SHOW_TABLE_TYPES = new String[] {
        //"TABLE",
        "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM"
};

private void createTemporaryTable(Connection connection) throws SQLException {
    final PreparedStatement statement = connection.prepareStatement(CREATE_TEMPORARY_TABLE);
    statement.execute();
    statement.close();
}

private void showTables(Connection connection) throws SQLException {
    final ResultSet set = connection.getMetaData().getTables(null, null, null, SHOW_TABLE_TYPES);
    while ( set.next() ) {
        out.println(format("%s %s %s %s %s",
                set.getString("TABLE_CAT"),
                set.getString("TABLE_SCHEM"),
                set.getString("TABLE_NAME"),
                set.getString("TABLE_TYPE"),
                set.getString("REMARKS")
        ));
    }
    set.close();
}

@Override
public void test(Connection connection) throws SQLException {
    createTemporaryTable(connection);
    showTables(connection);
}

预期结果是将 T1 表元数据写入 out 流。但没有任何反应,并且 getTables() 似乎没有考虑临时表。不知道如何解决它...有解决方法吗?非常感谢您的帮助。预先非常感谢。

And once again I have found an issue that I don't know how to fight with. Let's assume we have the following testing code:

private static final String CREATE_TEMPORARY_TABLE =
        "CREATE TEMPORARY TABLE T1 (\n" +
        "\tA FLOAT(4, 1),\n" +
        "\tB FLOAT(5, 2),\n" +
        "\tC FLOAT,\n" +
        "\tD INTEGER\n" +
        ") ENGINE = MEMORY;";

private final String[] SHOW_TABLE_TYPES = new String[] {
        //"TABLE",
        "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM"
};

private void createTemporaryTable(Connection connection) throws SQLException {
    final PreparedStatement statement = connection.prepareStatement(CREATE_TEMPORARY_TABLE);
    statement.execute();
    statement.close();
}

private void showTables(Connection connection) throws SQLException {
    final ResultSet set = connection.getMetaData().getTables(null, null, null, SHOW_TABLE_TYPES);
    while ( set.next() ) {
        out.println(format("%s %s %s %s %s",
                set.getString("TABLE_CAT"),
                set.getString("TABLE_SCHEM"),
                set.getString("TABLE_NAME"),
                set.getString("TABLE_TYPE"),
                set.getString("REMARKS")
        ));
    }
    set.close();
}

@Override
public void test(Connection connection) throws SQLException {
    createTemporaryTable(connection);
    showTables(connection);
}

Expected result is writing the T1 table meta data into the out stream. But nothing happens, and it seems that getTables() does not take into account the temporary tables. Don't know how I can resolve it... Is a work-around there? Your help is really very appreciated. Thanks a lot in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪推晚风 2024-10-20 18:56:27

MySQL 有时甚至不提供对愚蠢的事情的支持。该问题没有解决方案。关闭。

MySQL sometimes does not provide support even for stupid things. There is no solution for the issue. Closed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文