gpt4 book ai didi

database - 与 Derby 数据库交互时我的应用程序中的 SQLNonTransientConnectionException : No current connection,

转载 作者:太空狗 更新时间:2023-10-30 01:47:40 27 4
gpt4 key购买 nike

我在我的应用程序中实现了 derby 数据库以保存我的数据,并且在 Derby 数据库中检索结果集形式 select 查询时遇到此异常。为了建立连接,我使用连接字符串作为:

我使用这种方法建立连接:

我在 Connection.java 类中声明了这个方法:

 public synchronized Connection createConnection() throws Exception {

Connection rescon = null;

try {
if (this.dbuser == null) {
rescon = DriverManager.getConnection(this.URI + ";create=true");
} else {
rescon = DriverManager.getConnection(this.URI + ";create=true",
this.dbuser, this.dbpass);
}
// new connection in connection pool created
} catch (SQLException e) {
final String stackNum = Utility.exceptionHandler(e);
throw new Exception("Exception get during Connection - " + e.getMessage());
}
return rescon;
}

我将此方法用作并创建连接,在引入期间使用字符串创建连接:

private Connection objConnection = null;
objConnectionpool = new Connection("jdbc:derby:" + Folder.getAbsolutePath() + "/myapplication" + sFileName, null, null, "org.apache.derby.jdbc.EmbeddedDriver");
objConnection = objConnectionpool.createNewConnection();

我在执行查询时遇到异常:

sQuery = "select value,reason from " + TableNm + " where fileName ='" + FileName + "' AND type='"+Type+"' AND status ='remaining'"; 

在处理和与 Derby 数据库交互期间,我们可能会更新并从数据库中获取数据:

我在插入数据库之前将 setAutoCommit 设置为 false

objConnection.setAutoCommit(false);

插入后:

ps = objConnection.prepareStatement(sQuery);
rs = ps.executeQuery();
objConnection.commit();
objConnection.setAutoCommit(true);

我得到异常作为

java.sql.SQLNonTransientConnectionException: No current connection.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.noCurrentConnection(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.checkIfClosed(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.setupContextStack(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at com.myapplication.c.aw.a(Unknown Source)
at com.myapplication.c.aw.a(Unknown Source)
at com.myapplication.main.avd.run(Unknown Source)
Caused by: java.sql.SQLException: No current connection.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
... 11 more

最佳答案

通过 Google 找到的。

我遇到了同样的问题并在这个示例应用程序中找到了我的答案:http://db.apache.org/derby/docs/10.4/devguide/rdevcsecure26537.html

// force garbage collection to unload the EmbeddedDriver
// so Derby can be restarted
System.gc();

现在完美运行。

关于database - 与 Derby 数据库交互时我的应用程序中的 SQLNonTransientConnectionException : No current connection,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6172930/

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