JSF在数据表上显示数据库记录 [英] JSF display database records on datatable

查看:118
本文介绍了JSF在数据表上显示数据库记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been searching here for hour now and unfortunately I can't how to display database records to datatable, I'm newbie to JSF and I don't know much on JSF right now but I'm building a simple crud application I already know how to create, delete records using JSF but I'm having problem displaying this records to my datatable. I tried creating arraylist, I tried creating another class for this, To make it more clear here is my code:

这是我的index.jsf:

This is my 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代码:

Here is my code for the 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;
    }
}

}

我没有任何错误或什么,它仅显示数据库中的最后一条记录.伙计们,如果您知道最简单的方法,可以教我如何做,而我一直都希望获得简洁,简短的代码.非常感谢您的帮助:)

I don't have any error or something and it display only the last records in the database. Guys if you know the easiest ways can you teach me how to do it, and I always go for a nice clean, short code. your help is really much appreciated :)

推荐答案

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

There is a bug in your displayTableRecords() method. Within while loop you instantiate new TableUser array for each iteration. Actually what you should do is add TableUser object one by one to existing array.

使用ArrayList inseadof数组.

Use ArrayList inseadof array.

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

    }
}

这篇关于JSF在数据表上显示数据库记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆