0
@Named(value = "viewtable")
@ManagedBean
@ViewScoped
public final class viewtable implements Serializable {

    ArrayList<viewbean> dlist = new ArrayList<viewbean>();
    ArrayList<Master> mlist = new ArrayList<Master>();


    public ArrayList<viewbean> getDlist() {
        return dlist;
    }

    public viewtable()  {
        get();
    }

    public void get() {
        try
        {
            InitialContext ic = new InitialContext();
            DataSource ds = (DataSource) ic.lookup("jdbc/mydsn");
            Connection con = ds.getConnection();

            PreparedStatement ps=con.prepareStatement("select * from master");
            ResultSet rs=ps.executeQuery();

            while(rs.next())
            {
                dlist.add(new viewbean(rs.getString(1), rs.getString(2),rs.getString(3),rs.getString(4),rs.getDate(5), rs.getString(6), rs.getBytes(7), rs.getString(8),rs.getString(9),rs.getString(10),rs.getString(11),rs.getDate(12),rs.getDate(13),rs.getString(14),rs.getString(15), rs.getString(16)));
            }
        }catch(Exception ex)
        {
            System.out.println("Conncetion pool db error-> "+ex);
        }
    }

    public void onEdit(RowEditEvent event) {
    }
}

上面的代码没有更新数据库中的数据,get()方法是我做错了什么以及如何使用PrimeFaces连接数据库。
我使用托管 bean 将数据库中的详细信息保存为名为mlist.

JSF:

<h:form id="dtform">
    <p:growl showDetail="true" id="mess"/>
    <p:dataTable id="dt1" scrollable="true" scrollWidth="700" var="mas"  value="#{viewtable.dlist}" editable="true">
        <f:facet name="header">
            Edit Here
        </f:facet>
        <p:column headerText="Regno" width="100">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText  value="#{mas.regno}"/>
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{mas.regno}"/>
                </f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="Rollno" width="100">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{mas.rollno}"/>
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{mas.rollno}"/>
                </f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="Firstname" width="100">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{mas.fname}"/>
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{mas.fname}"/>
                </f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="Lastname" width="100">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{mas.lname}"/>
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{mas.lname}"/>
                </f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="D.O.B" width="100">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{mas.dob}"/>
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{mas.dob}"/>
                </f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="Address" width="100">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{mas.address}"/>
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{mas.address}"/>
                </f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="Avatar" width="100">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{mas.avatar}"/>
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{mas.avatar}"/>
                </f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="Email" width="100">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{mas.email}"/>
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{mas.email}"/>
                </f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="Phno" width="100">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{mas.phno}"/>
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{mas.phno}"/>
                </f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="Dept" width="100">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{mas.dept}"/>
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{mas.dept}"/>
                </f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="Year" width="100">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{mas.year}"/>
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{mas.year}"/>
                </f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="Start" width="100">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{mas.inidate}"/>
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{mas.inidate}"/>
                </f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="End" width="100">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{mas.enddate}"/>
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{mas.enddate}"/>
                </f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="Remarks" width="100">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{mas.remarks}"/>
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{mas.remarks}"/>
                </f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="Delivery" width="100">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{mas.delivery}"/>
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{mas.delivery}"/>
                </f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="Message" width="100">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{mas.messdelivery}"/>
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{mas.messdelivery}"/>
                </f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="Options" width="100">
            <p:rowEditor/>
        </p:column>
        <p:ajax event="rowEdit" listener="#{viewtable.onEdit}" update=":dtform:mess"/>
    </p:dataTable>
</h:form>
4

1 回答 1

1

我假设你已经在你的环境中配置了休眠,并将你的数据库映射到一个实体类。您可以相应地更新记录。

您可以访问行并更新记录,例如:

    public void onEdit(RowEditEvent event) {  
            YourHibernateEntity yourEntity = (YourHibernateEntity ) event.getObject());  
            SessionFactory sessFact = new AnnotationConfiguration().configure().buildSessionFactory();    
        Session sess = sessFact.openSession();
        Transaction  tran = sess.beginTransaction();
        sess.update(yourEntity );
        tran.commit();      

        } 
于 2012-09-12T07:02:58.277 回答