我不明白为什么会收到此错误..在我的网络应用程序中,我使用了很多方法执行insert
,select
,update
等所有方法它们正常工作没有错误......现在我想实现一个delete
动作。所以,我有:
public void deleteBox(Label label)
throws SQLException, ClassNotFoundException {
System.out.println("Deleting box from BOXES");
sq = "DELETE FROM BOXES WHERE product=? AND version=? AND productionD=? AND AA=?";
try {
Class.forName(typeDB);
c = DriverManager.getConnection(path);
PreparedStatement stm = c.prepareStatement(sq);
stm.setString(1, label.getProduct());
stm.setString(2, label.getVersion());
stm.setString(3, label.getDateProduction());
stm.setString(4, label.getPacketNumber());
stm.executeUpdate();
} catch (SQLException e) {
System.out.println("hereeee error: " + e.getMessage());
e.printStackTrace();
} finally {
if (stm != null)
stm.close(); //<---
if (c != null)
c.close();
}
}
其中path
和typeDB
定义为:
private final String path = "jdbc:sqlite:C:\\DB_LABELS\\labels.sqlite";
private final String typeDB= "org.sqlite.JDBC";
并在课程开始时:
private Connection c = null;
PreparedStatement stm = null;
此方法从servlet调用:
try{
Label box = (Label) session.getAttribute("boxFound");
action.deleteBox(box); //here I call the method
//some other stuff here..
action.addTransaction(box, "extract", timeStamp1, user.getId());
old = action.searchQuantityOfPackage(box);
}catch(SQLException | ClassNotFoundException e){
System.err.println("Exception at extract. More: " + e);
}
执行时我收到错误:
Deleting box from BOXES
Exception at extract. More: java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (Connection is closed)
并且所有其他命令都不在try
内执行。我不明白为什么会这样。正如我在讨论中所说,我对其他sql操作使用相同的设置,因此必须正确定义path
和typeDB
。如果我删除action.deleteBox(box);
没有发生错误,那么这种方法就会发生......
同时
我忘了提到表BOXES
中删除了行,尽管有错误!
我还添加了完整的堆栈跟踪
Exception at extract. More: java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (Connection is closed)
java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (Connection is closed)
at org.sqlite.core.DB.newSQLException(DB.java:890)
at org.sqlite.core.CoreStatement.internalClose(CoreStatement.java:109)
at org.sqlite.jdbc3.JDBC3Statement.close(JDBC3Statement.java:35)
at gr.products.labelInventory.ActionsDB.deleteBox(ActionsDB.java:557)
at gr.products.labelInventory.Inventory.processRequest(Inventory.java:285)
at gr.products.labelInventory.Inventory.doGet(Inventory.java:652)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at gr.products.labelInventory.LogFilter.doFilter(LogFilter.java:22)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:724)
此行at gr.products.labelInventory.ActionsDB.deleteBox(ActionsDB.java:557)
指向&lt; - i放入代码。