我现在一直在这里搜索一小时,不幸的是我不能如何向数据表显示数据库记录,我是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>
</p>
<p>
</p>
<p>
</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;
}
}
}
我没有任何错误或其他内容,它只显示数据库中的最后一条记录。伙计们,如果你知道最简单的方法,你可以教我如何做到这一点,我总是寻求一个干净,简短的代码。非常感谢你的帮助:)
答案 0 :(得分:2)
您的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)));
}
}