问题描述
|
我在应用程序中实现了derby数据库来保存数据,并且在从Derby数据库中检索“ 0”查询的结果集时遇到此异常。
要建立连接,我将连接字符串用作:
我使用这种方法建立连接:
我在类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,\"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();
现在可以完美运行。
, Connection.createNewConnection
是什么?我从未在java.sql.Connection
上听说过这种方法,而且在任何文档中都找不到。
我总是使用ѭ10来建立我的Derby数据库连接。