在 Java class 内插入一条新记录
Insert a new record inside the Java class
我的 Java class 中有这个方法,用于将新记录插入我的数据库(日期和布尔值)。
private void setTodayInfo(HttpSession session, String date){
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:<path>\myDataBase.sqlite");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
String sql = "INSERT INTO IsSELECTED VALUES ('"+date+"', 'FALSE', 'FALSE', 'FALSE')";
stmt.executeUpdate(sql);
c.close();
}catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
}
但它不起作用。查询是正确的,因为如果我在 SQL 控制台中执行它,插入就会发生。
edit: 我按照你说的把c.setAutoCommit(false);
去掉了,但是还是没有结果..
Opened database successfully
java.lang.NullPointerException: null
09-Jun-2015 12:36:08.837 INFO [Thread-5] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-8084"]
09-Jun-2015 12:36:08.888 INFO [Thread-5] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["ajp-nio-8009"]
09-Jun-2015 12:36:08.938 INFO [Thread-5] org.apache.catalina.core.StandardService.stopInternal Stopping service Catalina
09-Jun-2015 12:36:11.104 WARNING [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [WorkplanMaker] registered the JDBC driver [org.sqlite.JDBC] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
09-Jun-2015 12:36:11.105 WARNING [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [WorkplanMaker] is still processing a request that has yet to finish. This is very likely to create a memory leak. You can control the time allowed for requests to finish by using the unloadDelay attribute of the standard Context implementation. Stack trace of request processing thread:
java.lang.Object.wait(Native Method)
java.lang.Thread.join(Thread.java:1260)
java.lang.Thread.join(Thread.java:1334)
java.lang.ApplicationShutdownHooks.runHooks(ApplicationShutdownHooks.java:106)
java.lang.ApplicationShutdownHooks.run(ApplicationShutdownHooks.java:46)
java.lang.Shutdown.runHooks(Shutdown.java:123)
java.lang.Shutdown.sequence(Shutdown.java:167)
java.lang.Shutdown.exit(Shutdown.java:212)
java.lang.Runtime.exit(Runtime.java:109)
java.lang.System.exit(System.java:961)
dev.com.WorkplanMakerControlRoom.WorkplanMakerServlet.setTodayInfo(WorkplanMakerServlet.java:192)
dev.com.WorkplanMakerControlRoom.WorkplanMakerServlet.processRequest(WorkplanMakerServlet.java:34)
dev.com.WorkplanMakerControlRoom.WorkplanMakerServlet.doGet(WorkplanMakerServlet.java:447)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
org.apache.tomcat.util.threads.TaskThread$Wrappindevunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:724)
09-Jun-2015 12:36:11.113 INFO [Thread-5] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-8084"
您永远不会提交您的交易。
在关闭连接之前尝试 c.commit();。
编辑:
stmt 为空。
在stmt.executeUpdate(sql);之前插入stmt = c.createStatement();
您将 autocommmit 设置为 false 但没有手动提交。
在连接关闭之前删除 c.setAutoCommit(false);
或添加 c.commit();
。
你的语句对象是null
.
stmt = connection.createStatement(); // creating statement object.
stmt.executeUpdate(sql);
注意: 开始使用 PreparedStatement
而不是 Statement
并尽量避免使用 sql injection.
stmt 为空。
在stmt.executeUpdate(sql);之前插入stmt = c.createStatement();
我的 Java class 中有这个方法,用于将新记录插入我的数据库(日期和布尔值)。
private void setTodayInfo(HttpSession session, String date){
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:<path>\myDataBase.sqlite");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
String sql = "INSERT INTO IsSELECTED VALUES ('"+date+"', 'FALSE', 'FALSE', 'FALSE')";
stmt.executeUpdate(sql);
c.close();
}catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
}
但它不起作用。查询是正确的,因为如果我在 SQL 控制台中执行它,插入就会发生。
edit: 我按照你说的把c.setAutoCommit(false);
去掉了,但是还是没有结果..
Opened database successfully
java.lang.NullPointerException: null
09-Jun-2015 12:36:08.837 INFO [Thread-5] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-8084"]
09-Jun-2015 12:36:08.888 INFO [Thread-5] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["ajp-nio-8009"]
09-Jun-2015 12:36:08.938 INFO [Thread-5] org.apache.catalina.core.StandardService.stopInternal Stopping service Catalina
09-Jun-2015 12:36:11.104 WARNING [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [WorkplanMaker] registered the JDBC driver [org.sqlite.JDBC] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
09-Jun-2015 12:36:11.105 WARNING [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [WorkplanMaker] is still processing a request that has yet to finish. This is very likely to create a memory leak. You can control the time allowed for requests to finish by using the unloadDelay attribute of the standard Context implementation. Stack trace of request processing thread:
java.lang.Object.wait(Native Method)
java.lang.Thread.join(Thread.java:1260)
java.lang.Thread.join(Thread.java:1334)
java.lang.ApplicationShutdownHooks.runHooks(ApplicationShutdownHooks.java:106)
java.lang.ApplicationShutdownHooks.run(ApplicationShutdownHooks.java:46)
java.lang.Shutdown.runHooks(Shutdown.java:123)
java.lang.Shutdown.sequence(Shutdown.java:167)
java.lang.Shutdown.exit(Shutdown.java:212)
java.lang.Runtime.exit(Runtime.java:109)
java.lang.System.exit(System.java:961)
dev.com.WorkplanMakerControlRoom.WorkplanMakerServlet.setTodayInfo(WorkplanMakerServlet.java:192)
dev.com.WorkplanMakerControlRoom.WorkplanMakerServlet.processRequest(WorkplanMakerServlet.java:34)
dev.com.WorkplanMakerControlRoom.WorkplanMakerServlet.doGet(WorkplanMakerServlet.java:447)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
org.apache.tomcat.util.threads.TaskThread$Wrappindevunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:724)
09-Jun-2015 12:36:11.113 INFO [Thread-5] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-8084"
您永远不会提交您的交易。 在关闭连接之前尝试 c.commit();。
编辑:
stmt 为空。
在stmt.executeUpdate(sql);之前插入stmt = c.createStatement();
您将 autocommmit 设置为 false 但没有手动提交。
在连接关闭之前删除 c.setAutoCommit(false);
或添加 c.commit();
。
你的语句对象是null
.
stmt = connection.createStatement(); // creating statement object.
stmt.executeUpdate(sql);
注意: 开始使用 PreparedStatement
而不是 Statement
并尽量避免使用 sql injection.
stmt 为空。
在stmt.executeUpdate(sql);之前插入stmt = c.createStatement();