java - PrimeFaces + DataTable(编辑行) + JAVA + MYSQL

标签 java mysql primefaces datatable edit

我想编辑数据表行。我的代码在这里:

这是我的 jfs 页面 (Teszt.xhtml)已更新:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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"
      >

    <h:head>    
            <title>Teszt</title>        
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>                        
            <link rel="stylesheet" type="text/css" href="style.css"/>            
    </h:head>

    <ui:debug />
    <h:form>
    <p:fieldset legend="Teszt:">  
        <p:dataTable id="dataTable" value="#{TesztBean.values}" var="t"  paginator="true" rows="10" editable="true" editMode="cell">
        <p:ajax event="rowEdit" listener="#{TesztBean.update}"/>           

            <p:column filterBy="#{t.id}" sortBy="#{t.id}">
                <f:facet name="header">ID:</f:facet>                                                
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{t.id}"/>
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{t.id}"/>
                    </f:facet>
                </p:cellEditor>
            </p:column>

            <p:column filterBy="#{t.name}" sortBy="#{t.name}">
                <f:facet name="header">Name:</f:facet>                                                
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{t.name}"/>
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{t.name}"/>
                    </f:facet>
                </p:cellEditor>
            </p:column>


            <p:column filterBy="#{t.age}" sortBy="#{t.age}">
                <f:facet name="header">Age:</f:facet>                                                
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{t.age}"/>
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{t.age}"/>
                    </f:facet>
                </p:cellEditor>
            </p:column>

            <p:column><f:facet name="header">UPDATE</f:facet>                
                <p:commandButton value="Update" action="#{Teszt.updateOsszesito(t)}">                     
                   <f:ajax render="@all" execute="@form" /> 
                </p:commandButton>    
            </p:column>                               

            <p:column headerText="Edit">
                <p:rowEditor/>
            </p:column>

        </p:dataTable>
     </p:fieldset> 
    </h:form>
</html>

Teszt.java:

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<TesztSetGet> selectTable(){

        ResultSet rs = null;
        PreparedStatement pst = null;
        Connection con = getDBConnection();

        String stm = "select * from  teszt";

        List<TesztSetGet> records = new ArrayList<TesztSetGet>();

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

TesztBean.java 更新:

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<TesztSetGet> 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<TesztSetGet> selectTesztTable() throws SQLException{

        ResultSet rs = null;
        PreparedStatement pst = null;
        Connection con = getDBConnection();

        String stm = "select * from  teszt";

        List<TesztSetGet> records = new ArrayList<TesztSetGet>();


        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<TesztSetGet> 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();
     }

}

}

有 TesztSetGet(设置和获取方法):

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

}

当我修改单元格并单击更新按钮时,单元格将返回到原始状态:(

如何修改和更新行?

非常感谢,

最佳答案

您可以使用 Primefaces 作为 UI。 您必须向数据表添加 editable 属性,并添加包含 rowEditor 标记的列:

            <p:column headerText="edit">
                <p:rowEditor/>
            </p:column>

可编辑列必须像这样更改:

              <p:column headerText="your column" >
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{t.name}"/>
                    </f:facet>
                    <f:facet name="input">
                        <h:inputText value="#{t.name}"/>
                    </f:facet>
                </p:cellEditor>
             </p:column>

更新: 添加 ajax 标签 t 数据表并为监听器属性分配更新方法:

public class TesztBean  {

public void update(RowEditEvent event) {
        TesztSetGet edittedObject = (TesztSetGet) event.getObject();
        // update values field
    }
}

关于java - PrimeFaces + DataTable(编辑行) + JAVA + MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22284203/

相关文章:

Java:随机化方法递归调用的顺序

java - 局部字符串变量未初始化错误

java - 为什么我无法以这种方式关闭我的 JAR 可执行文件?

mysql - 我应该合并我的 MySQL 表吗?

jsf - 空 h :dataTable in JSF renders empty row with invalid number of columns?

jsf - Primefaces cellEditor 在 ajax 提交中传递整个数据表

java - 部署时 java.lang.Boolean 的 Jersey 正文编写器错误

mysql - 动态 SQL 查询根据单元格中的空值忽略空值

mysql - 在 MySQL 中获取当前 UTC 时间的 unix 时间戳的最佳方法

jsf - p :dataExporter not exporting just the displayed page