gpt4 book ai didi

java - 如何从内存中的 Derby 数据库表中选择数据?

转载 作者:行者123 更新时间:2023-12-04 14:48:22 26 4
gpt4 key购买 nike

我使用 JavaDB/Derby 创建了一个内存数据库表。我可以INSERT 数据到表中。但是当我尝试从表中SELECT 数据时它不起作用。

创建表:

DECLARE GLOBAL TEMPORARY TABLE SESSION.memtable (id int, name varchar(10))
NOT LOGGED

插入数据:

INSERT INTO SESSION.memtable (id, name) VALUES (?,?)

它为 受影响的行 返回 1。我通过以下方式选择数据:

SELECT name FROM SESSION.memtable WHERE id = ?

但它返回一个 ResultSet(rs.next() 为假)。

我在使用 SELECT 时做错了什么?

我使用 JDK7 中包含的 derby.jar

这是我的代码:

public class DBTest {
public static void main(String[] args) {
final int userId = 4;
final String sql = "DECLARE GLOBAL TEMPORARY TABLE SESSION.memtable "+
"(id int, name varchar(10)) NOT LOGGED";
final String inSQL = "INSERT INTO SESSION.memtable (id, name) "+
"VALUES (?,?)";
final String selSQL = "SELECT name FROM SESSION.memtable WHERE id = ?"
final String connURL = "jdbc:derby:memory:memdatabase;create=true";
try(Connection conn = DriverManager.getConnection(connURL);) {
try (PreparedStatement ps = conn.prepareStatement(sql);) {
ps.execute();
}
try (PreparedStatement ps = conn.prepareStatement(inSQL);) {
ps.setInt(1, userId);
ps.setString(2, "Jonas");
int rows = ps.executeUpdate();
System.out.println(rows + " rows inserted.");
}
try (PreparedStatement ps = conn.prepareStatement(selSQL);) {
ps.setInt(1, userId);
try (ResultSet rs = ps.executeQuery();) {
String name;
if(rs.next()) {
name = rs.getString("name");
} else {
name = null;
}
System.out.println("Name: " + name);
}
}
} catch (SQLException e) {e.printStackTrace();}}}

更新

如果我将 SELECT 查询更改为:

SELECT t1.name AS name FROM SESSION.memtable t1 WHERE id = ?

我收到此错误消息:

java.sql.SQLException: Operation 'DROP TABLE' cannot be performed on object 'MEMTABLE(ID, NAME)' because there is an open ResultSet dependent on that object.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.commitIfAutoCommit(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.commitIfAutoCommit(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.resultSetClosing(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedResultSet.movePosition(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedResultSet.next(Unknown Source)
at DBTest.main(DBTest.java:29)
Caused by: java.sql.SQLException: Operation 'DROP TABLE' cannot be performed on object 'MEMTABLE(ID, NAME)' because there is an open ResultSet dependent on that object.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
... 11 more
Caused by: ERROR X0X95: Operation 'DROP TABLE' cannot be performed on object 'MEMTABLE(ID, NAME)' because there is an open ResultSet dependent on that object.
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.verifyNoOpenResultSets(Unknown Source)
at org.apache.derby.impl.sql.GenericPreparedStatement.prepareToInvalidate(Unknown Source)
at org.apache.derby.impl.sql.depend.BasicDependencyManager.coreInvalidateFor(Unknown Source)
at org.apache.derby.impl.sql.depend.BasicDependencyManager.invalidateFor(Unknown Source)
at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.tempTablesAndCommit(Unknown Source)
at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.doCommit(Unknown Source)
at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.userCommit(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.commit(Unknown Source)
... 6 more

但是Name:没有打印出来,所以还是不行。

最佳答案

这个问题在 derby-user 上讨论过邮件列表,其中注意到包含 on commit preserve rows 的额外要求。

启用自动提交后,默认情况下会在提交时清除临时表,因此额外的子句可确保行在事务提交后保持可用。

关于java - 如何从内存中的 Derby 数据库表中选择数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8093368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com