java - How to update/delete data in mysql using primefaces? -


i have primefaces page, displayed data mysql. table structure:

 +---------------------------+---------------+---------+ | id (pk, auto_increment)   | name(varchar) | age(int)| +---------------------------+---------------+---------+ 

http://imageshack.com/a/img538/9755/2psvyf.png

button add working, when want delete person or update, errors this

апр 29, 2015 6:28:21 pm com.sun.faces.lifecycle.invokeapplicationphase execute         warning: /pages/test.xhtml @40,84 listener="#{test.update()}": java.lang.nullpointerexception         javax.el.elexception: /pages/test.xhtml @40,84 listener="#{test.update()}": java.lang.nullpointerexception             @ com.sun.faces.facelets.el.tagmethodexpression.invoke(tagmethodexpression.java:111)             @ org.primefaces.behavior.ajax.ajaxbehaviorlistenerimpl.processajaxbehavior(ajaxbehaviorlistenerimpl.java:54)             @ org.primefaces.event.roweditevent.processlistener(roweditevent.java:41)             @ javax.faces.component.behavior.behaviorbase.broadcast(behaviorbase.java:106)             @ javax.faces.component.uicomponentbase.broadcast(uicomponentbase.java:760)             @ javax.faces.component.uidata.broadcast(uidata.java:1071)             @ javax.faces.component.uidata.broadcast(uidata.java:1093)             @ javax.faces.component.uiviewroot.broadcastevents(uiviewroot.java:794)             @ javax.faces.component.uiviewroot.processapplication(uiviewroot.java:1259)             @ com.sun.faces.lifecycle.invokeapplicationphase.execute(invokeapplicationphase.java:81)             @ com.sun.faces.lifecycle.phase.dophase(phase.java:101)             @ com.sun.faces.lifecycle.lifecycleimpl.execute(lifecycleimpl.java:118)             @ javax.faces.webapp.facesservlet.service(facesservlet.java:593)             @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:303)             @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208)             @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52)             @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241)             @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208)             @ com.ocpsoft.pretty.prettyfilter.dofilter(prettyfilter.java:145)             @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241)             @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208)             @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:220)             @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:122)             @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:505)             @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:170)             @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:103)             @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:950)             @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:116)             @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:423)             @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1079)             @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:620)             @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:316)             @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145)             @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615)             @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61)             @ java.lang.thread.run(thread.java:745)         caused by: java.lang.nullpointerexception             @ kz.mtf.dao.testdao.update(testdao.java:46)             @ kz.mtf.controller.testcontroller.update(testcontroller.java:42)             @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)             @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57)             @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)             @ java.lang.reflect.method.invoke(method.java:606)             @ org.apache.el.parser.astvalue.invoke(astvalue.java:278)             @ org.apache.el.methodexpressionimpl.invoke(methodexpressionimpl.java:273)             @ com.sun.faces.facelets.el.tagmethodexpression.invoke(tagmethodexpression.java:105)             ... 35 more 

dao:

public class testdao {     public static list<testmodel> getall() throws sqlexception {         list<testmodel> list = new arraylist<testmodel>();         connection con = database.getconnection();         statement st = con.createstatement();         resultset rs = st.executequery("select * a_test");         while ( rs.next() ) {             testmodel testmodel = new testmodel();             testmodel.setname(rs.getstring("name"));             testmodel.setage(rs.getint("age"));             testmodel.setid(rs.getlong("id"));              list.add(testmodel);         }         return list;     }      public static void insert(testmodel testmodel) throws sqlexception{         connection con = database.getconnection();         preparedstatement ps = con.preparestatement("insert a_test (name, age) values (?, ?)");          ps.setstring(1,testmodel.getname());         ps.setint(2, testmodel.getage());         ps.executeupdate();      }      public static void update(testmodel testmodel) throws sqlexception{         connection con = database.getconnection();         preparedstatement ps = con.preparestatement("update a_test set name=?, age=?, id=?");          ps.setstring(1,testmodel.getname());         ps.setint(2, testmodel.getage());         ps.setlong(3, testmodel.getid());         ps.executeupdate();      }      public static void delete(long id) throws sqlexception{         connection con = database.getconnection();         preparedstatement ps = con.preparestatement("delete a_test id=?");          ps.setlong(1, id);         ps.executeupdate();      } } 

controller:

@managedbean(name="test") @requestscoped public class testcontroller implements serializable{     private testmodel testmodel;     private list<testmodel> testmodellist;      public testcontroller() {         testmodel = new testmodel();     }      public testmodel gettestmodel() {         return testmodel;     }      public void settestmodel(testmodel testmodel) {         this.testmodel = testmodel;     }      public list<testmodel> gettestmodellist() {         return testmodellist;     }      public void settestmodellist(list<testmodel> testmodellist) {         this.testmodellist = testmodellist;     }      public void update() throws sqlexception {         testdao.update(testmodel);     }      public void insert() throws sqlexception {         testdao.insert(testmodel);     }      public list<testmodel> getall() throws sqlexception {         return testdao.getall();     }      public void delete() throws sqlexception {         testdao.delete(testmodel.getid());     } } 

test.xhtml:

 <h:form id="form1">     <p:panel id="panel" header="add new person" style="margin-bottom:10px;">         <p:messages id="messages" />         <h:panelgrid columns="2" cellpadding="5">             <p:outputlabel  for="a1" value="name: " />             <p:inputtext  id="a1" value="#{test.testmodel.name}"/>              <p:outputlabel for="a2" value="age: " />             <p:inputtext id="a2" value="#{test.testmodel.age}"/>         </h:panelgrid>     </p:panel>      <p:toolbar>         <f:facet name="left">             <p:commandbutton value="add"  update="form1" action="#{test.insert()}"/>         </f:facet>     </p:toolbar>      <p:spacer height="30px;"/>      <p:datatable value="#{test.getall()}" var="e"  widgetvar="50" editable="true" >         <f:facet name="header">            persons         </f:facet>          <p:ajax event="rowedit" listener="#{test.update()}" update=":form1:messages" />         <p:ajax event="roweditcancel" listener="#{test.delete()}" update=":form1:messages" />          <p:column>             <f:facet name="header">                 <h:outputtext value="name" />             </f:facet>             <p:celleditor>                 <f:facet name="output">                     <h:outputtext value="#{e.name}" />                 </f:facet>                 <f:facet name="input">                     <p:inputtext value="#{e.name}" style="width:100%"/>                 </f:facet>             </p:celleditor>         </p:column>          <p:column>             <f:facet name="header">                 <h:outputtext value="age" />             </f:facet>             <p:celleditor>                 <f:facet name="output">                     <h:outputtext value="#{e.age}" />                 </f:facet>                 <f:facet name="input">                     <p:inputtext value="#{e.age}" style="width:100%"/>                 </f:facet>             </p:celleditor>         </p:column>          <p:column headertext="" style="width:50px">             <p:roweditor />         </p:column>     </p:datatable>     </h:form> 

how can fix it?


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -