Insert a new record inside the Java class

127 views Asked by At

I have this method in my Java class, to insert a new record to my DB (a date and thee booleans values).

    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");
}

but it doesn't work. The Query is right because if I execute it in SQL Console, the insert is happening.

edit: I remove the c.setAutoCommit(false); as you say, but i still have no result..

            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$1.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"
4

There are 4 answers

5
Naman Gala On BEST ANSWER

Either remove c.setAutoCommit(false); or add c.commit(); before connection close.

Your statement object is null.

stmt = connection.createStatement(); // creating statement object.
stmt.executeUpdate(sql);

Note: Start using PreparedStatement instead of Statement and try to avoid sql injection.

0
couettos On

stmt is null.

Insert stmt = c.createStatement(); before stmt.executeUpdate(sql);

1
couettos On

You never commit your transaction. Try c.commit(); before closing your connection.

Edit:

stmt is null.

Insert stmt = c.createStatement(); before stmt.executeUpdate(sql);

1
vanje On

You set autocommmit to false but there is no manual commit.