使用嵌入式 Derby,我可以插入临时表,但插入后不能查询

发布于 2024-10-02 21:34:22 字数 1540 浏览 2 评论 0原文

我有一个到嵌入式德比数据库的连接。

我想创建一个临时表,并了解到我必须使用 DECLARE GLOBAL TEMPORARY TABLE 并在 INSERT 语句中将 SESSION 前缀添加到表名中。

我尝试过在 DECLARE 和 SELECT 语句中使用和不使用 SESSION 前缀,但没有任何区别。

用于创建临时表的 DECLARE 语句和两个 INSERT 语句一样成功(每个语句返回计数 1)。

SELECT 语句无法返回任何结果。我也尝试过“SELECT * FROM...”但没有成功。

任何建议表示赞赏。

(注意:这里的conn是一个包装类,所以conn.executeQuery、conn.executeUpdate实际上得到一个Statement并使用Statement的executeQuery/Update)

    System.out.println("tt 1a" );
    conn.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.tempstats ( t1 int, t2 int, t3 int) NOT LOGGED "); 
    System.out.println("tt 1b" );
    //
    PreparedStatement pStmt = conn.prepareStatement( "INSERT INTO SESSION.tempstats VALUES ( ?, ?, ? )" );
    pStmt.setInt( 1, 1 );
    pStmt.setInt( 2, 1 );
    pStmt.setInt( 3, 1 );
    int count = pStmt.executeUpdate();
    System.out.println("tt 2, count " + count );
    pStmt.setInt( 1, 2 );
    pStmt.setInt( 2, 2 );
    pStmt.setInt( 3, 2 );
    count = pStmt.executeUpdate();
    System.out.println("tt 3, count " + count );
    ResultSet testRs = conn.executeQuery( "SELECT t1, t2 from SESSION.tempstats" );
    count = 0;
    while ( testRs.next() ) {
        log2 ("result of tempstats table is " + testRs.getInt("t1") + " " + testRs.getString("t2") );
        count++;
    }
    System.out.println("tt 4, query result count = " + count );
    testRs.close();    
    pStmt.close();

上面代码的结果是

tt 1a
tt 1b
tt 2, count 1
tt 3, count 1
tt 4, query result count = 0

I have a connection to an embedded derby database.

I want to create a temporary table and learned that I have to use DECLARE GLOBAL TEMPORARY TABLE and prefix SESSION to the table name in the INSERT statement.

I have tried using and not using the SESSION prefix in the DECLARE and SELECT statements but it has made no difference.

The DECLARE statement to create the temporary table is successful as is the two INSERT statements (each returns a count of 1).

The SELECT statement fails to return any results. I have tried "SELECT * FROM..." without success as well.

Any advice is appreciated.

(NOTE: the conn here is a wrapper class, so the conn.executeQuery, conn.executeUpdate actually get a Statement and use the Statement's executeQuery/Update)

    System.out.println("tt 1a" );
    conn.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.tempstats ( t1 int, t2 int, t3 int) NOT LOGGED "); 
    System.out.println("tt 1b" );
    //
    PreparedStatement pStmt = conn.prepareStatement( "INSERT INTO SESSION.tempstats VALUES ( ?, ?, ? )" );
    pStmt.setInt( 1, 1 );
    pStmt.setInt( 2, 1 );
    pStmt.setInt( 3, 1 );
    int count = pStmt.executeUpdate();
    System.out.println("tt 2, count " + count );
    pStmt.setInt( 1, 2 );
    pStmt.setInt( 2, 2 );
    pStmt.setInt( 3, 2 );
    count = pStmt.executeUpdate();
    System.out.println("tt 3, count " + count );
    ResultSet testRs = conn.executeQuery( "SELECT t1, t2 from SESSION.tempstats" );
    count = 0;
    while ( testRs.next() ) {
        log2 ("result of tempstats table is " + testRs.getInt("t1") + " " + testRs.getString("t2") );
        count++;
    }
    System.out.println("tt 4, query result count = " + count );
    testRs.close();    
    pStmt.close();

The result of the above code is

tt 1a
tt 1b
tt 2, count 1
tt 3, count 1
tt 4, query result count = 0

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

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

发布评论

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

评论(2

长亭外,古道边 2024-10-09 21:34:22

如果自动提交设置为 true,则必须使用“ON COMMIT PRESERVE ROWS”创建临时表,就像我的情况一样。

如果将 autocommit 设置为 false,原始代码将起作用。

在我的例子中,它被设置为 true,现在设置为 false。

The temporary table has to be created with "ON COMMIT PRESERVE ROWS" if autocommit is set to true, as was my case.

If you set autocommit to false the original code would work.

In my case is was set to true, it is now set to false.

月隐月明月朦胧 2024-10-09 21:34:22

创建表时使用“ON COMMIT PRESERVE ROWS”:

在提交时声明全局临时表 SESSION.TEMPSTATS ( t1 int, t2 int, t3 int) 保留未记录的行

Use "ON COMMIT PRESERVE ROWS" when you create a table:

DECLARE GLOBAL TEMPORARY TABLE SESSION.TEMPSTATS ( t1 int, t2 int, t3 int) ON COMMIT PRESERVE ROWS NOT LOGGED

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