1

我已经在这里搜索了一个小时,不幸的是我无法将数据库记录显示到数据表中,我是 JSF 的新手,我现在对 JSF 不太了解,但我正在构建一个简单的 crud 应用程序我已经知道如何使用 JSF 创建、删除记录,但我在将这些记录显示到我的数据表时遇到了问题。我尝试创建arraylist,我尝试为此创建另一个类,为了更清楚,这里是我的代码:

这是我的 index.jsf:

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

<f:view xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <h:head></h:head>
        <h:body>
            Username: 
            <h:outputText value="#{ backing_index.userName }" id="Username">
                <p>
                    &nbsp;
                </p>
                <p>
                    &nbsp;
                </p>
                <p>
                    &nbsp;
                </p>
            </h:outputText>
            <p>
                RoleI.D: 
                <h:outputText value="#{backing_index.roleId}" id="RoleID"/>
            </p>
            Role Description: 
            <h:outputText  value="#{backing_index.roleDesc}" id="Description"/>

            <h:dataTable value="#{ backing_index.tableRs }" var="user" rules="rows" cellpadding="7">
                <f:facet name="header"></f:facet>
                <f:facet name="footer"></f:facet>

                <h:column>
                    <f:facet name="header">ID</f:facet>
                    #{ user.tableId }
                </h:column>

                <h:column>
                    <f:facet name="header">First Name</f:facet>
                     #{ user.tableFirstName }
                </h:column>

                <h:column>
                    <f:facet name="header">Middle Name</f:facet>
                     #{ user.tableMiddleName }
                </h:column>

                <h:column>
                    <f:facet name="header">Last Name</f:facet>
                     #{ user.tableLastName }
                </h:column>

                <h:column>
                    <f:facet name="header">Delete</f:facet>
                    <h:commandButton action="#{ backing_index.deleteAction }" value="Remove this">
                        <f:param value="Remove" name="delete" />
                    </h:commandButton>
                </h:column>

            </h:dataTable>
        </h:body>
    </html>
    <!--oracle-jdev-comment:auto-binding-backing-bean-name:backing_index-->
</f:view>

这是我的 bean 代码:

package view.backing;

import javax.faces.component.html.HtmlOutputText;
import javax.faces.bean.*;
import javax.faces.context.*;
import javax.annotation.*;
import javax.faces.*;
import java.sql.*;
import java.util.*;

@RequestScoped

public class Index {

private Connection con;
private ResultSet rs;

private String userName;
private String roleId;
private String roleDesc;

//Variable of Data Table
private TableUser[] tableRs;
//End of Variable


//Start of getter and setter for Data table

public void setTableRs(Index.TableUser[] tableRs) {
    this.tableRs = tableRs;
}

public Index.TableUser[] getTableRs() {
    return tableRs;
}

//End of getter and setter


public void setUserName(String userName) {
    this.userName = userName;
}

public String getUserName() {
    return userName;
}

public void setRoleId(String roleId) {
    this.roleId = roleId;
}

public String getRoleId() {
    return roleId;
}

public void setRoleDesc(String roleDesc) {
    this.roleDesc = roleDesc;
}

public String getRoleDesc() {
    return roleDesc;
}

@PostConstruct
public void init()throws SQLException, ClassNotFoundException{

    Class.forName("oracle.jdbc.driver.OracleDriver");
    con = DriverManager.getConnection("jdbc:oracle:thin:@localhost/XE", "JEROME", "perbert101");
    displayUserInfo();
    displayTableRecords();
}

private void displayUserInfo()throws SQLException{
    FacesContext context = FacesContext.getCurrentInstance();
    userName = (String)context.getExternalContext().getSessionMap().get("userName");
    roleId = (String)context.getExternalContext().getSessionMap().get("roleId");

    Statement state = con.createStatement();
    state.executeQuery("SELECT * FROM ROLES WHERE ID = 2");
    rs = state.getResultSet();
    while(rs.next()){
        roleDesc = rs.getString(3);
    }

}

private void displayTableRecords()throws SQLException{
    String query = "SELECT * FROM USERS";
    PreparedStatement state = con.prepareStatement(query);
    state.execute();
    rs = state.getResultSet();
    while(rs.next()){
        tableRs = new TableUser[]{new TableUser(rs.getLong(1), rs.getString(2), rs.getString(7), rs.getString(5))};

    }

}

//Table Records Store
public static class TableUser{
    long tableId;
    String tableFirstName;
    String tableMiddleName;
    String tableLastName;

    public TableUser(long tableId, String tableFirstName, String tableMiddleName, String tableLastName){
        this.tableId = tableId;
        this.tableFirstName = tableFirstName;
        this.tableMiddleName = tableMiddleName;
        this.tableLastName = tableLastName;
    }

    public void setTableId(long tableId) {
        this.tableId = tableId;
    }

    public long getTableId() {
        return tableId;
    }

    public void setTableFirstName(String tableFirstName) {
        this.tableFirstName = tableFirstName;
    }

    public String getTableFirstName() {
        return tableFirstName;
    }

    public void setTableMiddleName(String tableMiddleName) {
        this.tableMiddleName = tableMiddleName;
    }

    public String getTableMiddleName() {
        return tableMiddleName;
    }

    public void setTableLastName(String tableLastName) {
        this.tableLastName = tableLastName;
    }

    public String getTableLastName() {
        return tableLastName;
    }
}

}

我没有任何错误或其他东西,它只显示数据库中的最后一条记录。伙计们,如果你知道最简单的方法,你能教我怎么做,我总是追求一个干净、简短的代码。非常感谢您的帮助:)

4

1 回答 1

3

您的 displayTableRecords() 方法中有一个错误。在 while 循环中,您为每次迭代实例化新的 TableUser 数组。实际上,您应该做的是将 TableUser 对象一一添加到现有数组中。

使用 ArrayList inseadof 数组。

private List<TableUser> tableRs = new ArrayList<TableUser>();

public List<TableUser> getTableRs() {
    return tableRs;
}

public void setTableRs(List<TableUser> tableRs) {
    this.tableRs = tableRs;
}

private void displayTableRecords() {
    String query = "SELECT * FROM USERS";
    PreparedStatement state = con.prepareStatement(query);
    state.execute();
    rs = state.getResultSet();
    while (rs.next()) {

        tableRs.add(new TableUser(rs.getLong(1),
                rs.getString(2), rs.getString(7), rs.getString(5)));

    }
}
于 2012-11-06T08:13:02.260 回答