title: PrimeFaces + DataTable(edit row) + JAVA + MYSQL
body: <p>I would like to edit datatable row. My code here: </p>
  
  <p>There is my jfs page (Teszt.xhtml) <strong>UPDATED</strong>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"   
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:ui="http://java.sun.com/jsf/facelets" 
        xmlns:p="http://primefaces.org/ui"
        &gt;
  
      &lt;h:head&gt;    
              &lt;title&gt;Teszt&lt;/title&gt;        
              &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/&gt;                        
              &lt;link rel="stylesheet" type="text/css" href="style.css"/&gt;            
      &lt;/h:head&gt;
  
      &lt;ui:debug /&gt;
      &lt;h:form&gt;
      &lt;p:fieldset legend="Teszt:"&gt;  
          &lt;p:dataTable id="dataTable" value="#{TesztBean.values}" var="t"  paginator="true" rows="10" editable="true" editMode="cell"&gt;
          &lt;p:ajax event="rowEdit" listener="#{TesztBean.update}"/&gt;           
  
              &lt;p:column filterBy="#{t.id}" sortBy="#{t.id}"&gt;
                  &lt;f:facet name="header"&gt;ID:&lt;/f:facet&gt;                                                
                  &lt;p:cellEditor&gt;
                      &lt;f:facet name="output"&gt;
                          &lt;h:outputText value="#{t.id}"/&gt;
                      &lt;/f:facet&gt;
                      &lt;f:facet name="input"&gt;
                          &lt;p:inputText value="#{t.id}"/&gt;
                      &lt;/f:facet&gt;
                  &lt;/p:cellEditor&gt;
              &lt;/p:column&gt;
  
              &lt;p:column filterBy="#{t.name}" sortBy="#{t.name}"&gt;
                  &lt;f:facet name="header"&gt;Name:&lt;/f:facet&gt;                                                
                  &lt;p:cellEditor&gt;
                      &lt;f:facet name="output"&gt;
                          &lt;h:outputText value="#{t.name}"/&gt;
                      &lt;/f:facet&gt;
                      &lt;f:facet name="input"&gt;
                          &lt;p:inputText value="#{t.name}"/&gt;
                      &lt;/f:facet&gt;
                  &lt;/p:cellEditor&gt;
              &lt;/p:column&gt;
  
  
              &lt;p:column filterBy="#{t.age}" sortBy="#{t.age}"&gt;
                  &lt;f:facet name="header"&gt;Age:&lt;/f:facet&gt;                                                
                  &lt;p:cellEditor&gt;
                      &lt;f:facet name="output"&gt;
                          &lt;h:outputText value="#{t.age}"/&gt;
                      &lt;/f:facet&gt;
                      &lt;f:facet name="input"&gt;
                          &lt;p:inputText value="#{t.age}"/&gt;
                      &lt;/f:facet&gt;
                  &lt;/p:cellEditor&gt;
              &lt;/p:column&gt;
  
              &lt;p:column&gt;&lt;f:facet name="header"&gt;UPDATE&lt;/f:facet&gt;                
                  &lt;p:commandButton value="Update" action="#{Teszt.updateOsszesito(t)}"&gt;                     
                     &lt;f:ajax render="@all" execute="@form" /&gt; 
                  &lt;/p:commandButton&gt;    
              &lt;/p:column&gt;                               
  
              &lt;p:column headerText="Edit"&gt;
                  &lt;p:rowEditor/&gt;
              &lt;/p:column&gt;
  
          &lt;/p:dataTable&gt;
       &lt;/p:fieldset&gt; 
      &lt;/h:form&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Teszt.java:</p>
  
  <pre><code>import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.util.ArrayList;
  import java.util.List;
  import java.io.Serializable;
  import javax.faces.bean.ManagedBean;
  import javax.faces.bean.SessionScoped;
  
  
  @ManagedBean
  @SessionScoped
  
  public class Teszt implements Serializable {
  
  
      public List&lt;TesztSetGet&gt; selectTable(){
  
          ResultSet rs = null;
          PreparedStatement pst = null;
          Connection con = getDBConnection();
  
          String stm = "select * from  teszt";
  
          List&lt;TesztSetGet&gt; records = new ArrayList&lt;TesztSetGet&gt;();
  
          try {
  
              pst = con.prepareStatement(stm);
              pst.execute();
              rs = pst.getResultSet();
  
           while(rs.next()){
  
              TesztSetGet objectMeghiv = new TesztSetGet();
  
              objectMeghiv.setId(rs.getInt(1));
              objectMeghiv.setName(rs.getString(2));
              objectMeghiv.setAge(rs.getInt(3));             
  
              records.add(objectMeghiv);                        
           }
  
              rs.close();
              pst.close();
              con.close();
  
        } catch (SQLException e) {
           e.printStackTrace();         
       } catch (Exception e) {
           e.printStackTrace();         
       }
        return records;
  
  
      }
  
  
  
     public void updateOsszesito(TesztSetGet nth){
  
         Connection connection = null;
           PreparedStatement pst = null;
           ResultSet rs = null;
  
           String sql = "update teszt set name=?, age=? where id=?";                                 
  
  
           try{
  
              connection = getDBConnection();                                                                       
              pst = connection.prepareStatement(sql);
  
              pst.setString(1, nth.getName());
              pst.setInt(2, nth.getAge());
              pst.setInt(3, nth.getId());                        
  
              System.out.println(nth.getId()+"    " + nth.getName() + "   " + nth.getAge() );
  
              pst.executeUpdate();
              pst.close();
              connection.close();
  
           }catch(SQLException se){
              se.printStackTrace();
              se.getMessage();
           }catch(Exception e){
               e.printStackTrace();
               e.getMessage();
           }
  
       }
  
  
  
  
     public Connection getDBConnection() {
  
          Connection dbConnection = null;
  
          String URL = "jdbc:mysql://IP:3306/osszesito";
          String USER = "osszesito";        
          String PASSWORD = "Password";
          String DRIVER = "com.mysql.jdbc.Driver";
  
          try {
  
              Class.forName(DRIVER);
              dbConnection= DriverManager.getConnection(URL, USER, PASSWORD);
              System.out.println("Connection completed.");
  
          } catch (SQLException e) { 
  
              System.out.println(e.getMessage()); 
  
          }catch(ClassNotFoundException cnfe){
  
             cnfe.printStackTrace();
             System.out.println(cnfe.getMessage());
             System.exit(-1);
  
         }
  
          return dbConnection; 
      }
  }
  </code></pre>
  
  <p>TesztBean.java <strong>UPDATED</strong>:</p>
  
  <pre><code>import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.util.ArrayList;
  import java.util.List;
  import javax.annotation.PostConstruct;
  
  public class TesztBean {
  
      String URL = "jdbc:mysql://IP:3306/osszesito";
      String USER = "osszesito";        
      String PASSWORD = "Password";
      String DRIVER = "com.mysql.jdbc.Driver";
  
  
      private List&lt;TesztSetGet&gt; values;
  
      @PostConstruct
      public void init() {
          try {
              values = selectTesztTable();
          } catch (SQLException e) {
  
               e.printStackTrace();    
          }
      }
  
  
      public Connection getDBConnection() {
  
          Connection dbConnection = null;
  
          try {
  
              Class.forName(DRIVER);
              dbConnection= DriverManager.getConnection(URL, USER, PASSWORD);
              System.out.println("Connection completed.");
  
          } catch (SQLException e) { 
  
              System.out.println(e.getMessage()); 
  
          }catch(ClassNotFoundException cnfe){
  
             cnfe.printStackTrace();
             System.out.println(cnfe.getMessage());
             System.exit(-1);
  
         }
  
          return dbConnection; 
      }
  
  
  
      public List&lt;TesztSetGet&gt; selectTesztTable() throws SQLException{
  
          ResultSet rs = null;
          PreparedStatement pst = null;
          Connection con = getDBConnection();
  
          String stm = "select * from  teszt";
  
          List&lt;TesztSetGet&gt; records = new ArrayList&lt;TesztSetGet&gt;();
  
  
          try {
  
              pst = con.prepareStatement(stm);
              pst.execute();
              rs = pst.getResultSet();
  
           while(rs.next()){
  
              TesztSetGet objectMeghiv = new TesztSetGet();
  
              objectMeghiv.setId(rs.getInt(1));
              objectMeghiv.setName(rs.getString(2));
              objectMeghiv.setAge(rs.getInt(3));             
  
              records.add(objectMeghiv); 
  
           }
  
           return records;
  
  
          }catch (SQLException e) {
              e.printStackTrace();         
          }catch (Exception e) {
              e.printStackTrace();         
          }finally{
  
              rs.close();
              pst.close();            
              con.close();
  
       }
  
        return records;
  
      }
  
  
      public List&lt;TesztSetGet&gt; getValues() { 
  
          return values; 
  
      }
  
      public void update(RowEditEvent event) {
          TesztSetGet edittedObject = (TesztSetGet) event.getObject();        
      System.out.println(edittedObject.getName() + "--------------------------" +edittedObject.getAge());
  
  
      Connection connection = null;
      PreparedStatement pst = null;
      ResultSet rs = null;
  
       String sql = "update teszt set name=?, age=? where id=?";                                 
  
  
       try{
  
          connection = getDBConnection();                                                                       
          pst = connection.prepareStatement(sql);
  
          pst.setString(1, edittedObject.getName());
          pst.setInt(2, edittedObject.getAge());
          pst.setInt(3, edittedObject.getId());                        
  
          pst.executeUpdate();
          pst.close();
          connection.close();
  
       }catch(SQLException se){
          se.printStackTrace();
          se.getMessage();
       }catch(Exception e){
           e.printStackTrace();
           e.getMessage();
       }
  
  }
  
  }
  </code></pre>
  
  <p>There is TesztSetGet (set and get methods):</p>
  
  <pre><code>public class TesztSetGet {
  
      private int id;
      private String name;
      private int age;
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public int getAge() {
          return age;
      }
  
      public void setAge(int age) {
          this.age = age;
      }
  
  }
  </code></pre>
  
  <p>When I modify the cells and i click update button, then the cells return to original state:(</p>
  
  <p>How can i modify and update the rows?</p>
  
  <p>thank you very much, </p>