当我从数据库中提取数据时,它会给出唯一的数据,但当我打印到<;h:数据表>;使用jsf可以打印包含重复数据的数据

当我从数据库中提取数据时,它会给出唯一的数据,但当我打印到<;h:数据表>;使用jsf可以打印包含重复数据的数据,jsf,jdbc,primefaces,Jsf,Jdbc,Primefaces,当我使用不同的关键字从数据库中获取数据时,它会给出唯一的或单个的数据,但当我使用jsf打印时,每次记录时都会打印重复的数据 bean.java @ManagedBean(name = "privilegeBean", eager = true) @SessionScoped public class Privilege { private int id; private String privilege; public List<Privilege> messages; priva

当我使用不同的关键字从数据库中获取数据时,它会给出唯一的或单个的数据,但当我使用jsf打印时,每次记录时都会打印重复的数据

bean.java

@ManagedBean(name = "privilegeBean", eager = true)
@SessionScoped
public class Privilege {

private int id;
private String privilege;
public List<Privilege> messages;
private PrivilegeDao prvlgdao=new PrivilegeDao();
private boolean canEdit;
private Privilege privilegeedit;
private String str;

public Privilege() {

    messages = new ArrayList<Privilege>();
}

public Privilege(int id, String privilege) {
    this.id = id;
    this.privilege = privilege;
    this.canEdit = false;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getPrivilege() {
    return privilege;
}

public void setPrivilege(String privilege) {
    this.privilege = privilege;
}

public List<Privilege> getMessages() {
    this.messages = prvlgdao.getUserList();
    return messages;
}

public void setMessages(List<Privilege> messages) {
    this.messages = messages;
}

public boolean isCanEdit() {
    return canEdit;
}

public void setCanEdit(boolean canEdit) {
    this.canEdit = canEdit;
}

public String editPrivilege() {
    privilegeedit.setCanEdit(true);
    return null;
}

public String addPrivileges() {
    int ins = prvlgdao.insertPrivilege(privilege);
    if (ins == 1) {
        System.out.println("privilege is insert ........");
        str="1";
    }
    return str;
}
}
@ManagedBean(name=“privilegeBean”,eager=true)
@会议范围
公共阶级特权{
私有int-id;
私有字符串特权;
公开列表信息;
private PrivilegeDao prvlgdao=新PrivilegeDao();
私有布尔canEdit;
私人特权;
私有字符串str;
公共特权(){
messages=newarraylist();
}
公共权限(整数id、字符串权限){
this.id=id;
特权=特权;
this.canEdit=false;
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getPrivilege(){
返回特权;
}
public void setPrivilege(字符串特权){
特权=特权;
}
公共列表getMessages(){
this.messages=prvlgdao.getUserList();
返回消息;
}
公共消息(列出消息){
this.messages=消息;
}
公共布尔值isCanEdit(){
返回canEdit;
}
公共void setCanEdit(布尔canEdit){
this.canEdit=canEdit;
}
公共字符串editPrivilege(){
privilegedit.setCanEdit(true);
返回null;
}
公共字符串addPrivileges(){
intins=prvlgdao.insertPrivilege(privilege);
如果(ins==1){
System.out.println(“权限为插入…”);
str=“1”;
}
返回str;
}
}
道:-PrivilegeDao

public class PrivilegeDao {
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/city_info";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "root";
private static Connection dbConnection = null;
private PreparedStatement preparedStatement = null;
private String sqlprivilege;
private int pinsert;
private ResultSet rs=null;
private List<Privilege> list = new ArrayList<Privilege>();
private Privilege prg;

private static Connection getDBConnection() {

    Connection dbConnection = null;

    try {

        Class.forName(DB_DRIVER);

    } catch (ClassNotFoundException e) {

        System.out.println(e.getMessage());

    }

    try {

        dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER,
                DB_PASSWORD);
        return dbConnection;

    } catch (SQLException e) {

        System.out.println(e.getMessage());

    }

    return dbConnection;

}

public List<Privilege> getUserList()
{
    try
    {
        //list=null;
        prg=new Privilege();
        sqlprivilege = "select distinct privilege from privilege";
        dbConnection = getDBConnection();
        // execute select SQL stetement
        preparedStatement = dbConnection.prepareStatement(sqlprivilege);
        rs = preparedStatement.executeQuery();
        while(rs.next())
        {
            prg.setPrivilege(rs.getString("privilege"));
            list.add(prg);
            System.out.println("user id is ......");

        }


    }
    catch (SQLException e) {

        System.out.println(e.getMessage());

    } finally {

        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (dbConnection != null) {
            try {
                dbConnection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
    return list;

}
}
public类PrivilegeDao{
私有静态最终字符串DB_DRIVER=“com.mysql.jdbc.DRIVER”;
私有静态最终字符串DB_CONNECTION=“jdbc:mysql://localhost:3306/city_info";
私有静态最终字符串DB_USER=“root”;
私有静态最终字符串DB_PASSWORD=“root”;
私有静态连接dbConnection=null;
private PreparedStatement PreparedStatement=null;
私有字符串权限;
私人国际品赛;
私有结果集rs=null;
私有列表=新的ArrayList();
私人特权prg;
私有静态连接getDBConnection(){
连接dbConnection=null;
试一试{
类forName(DB_驱动程序);
}catch(classnotfounde异常){
System.out.println(e.getMessage());
}
试一试{
dbConnection=DriverManager.getConnection(DB\U连接,DB\U用户,
DB_密码);
返回数据库连接;
}捕获(SQLE异常){
System.out.println(e.getMessage());
}
返回数据库连接;
}
公共列表getUserList()
{
尝试
{
//列表=空;
prg=新特权();
sqlprivilege=“从特权中选择不同的特权”;
dbConnection=getDBConnection();
//执行select SQL语句
preparedStatement=dbConnection.prepareStatement(sqlprivilege);
rs=preparedStatement.executeQuery();
while(rs.next())
{
prg.setPrivilege(rs.getString(“privilege”);
列表。添加(prg);
System.out.println(“用户id是……”);
}
}
捕获(SQLE异常){
System.out.println(e.getMessage());
}最后{
if(preparedStatement!=null){
试一试{
preparedStatement.close();
}捕获(SQLE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
if(dbConnection!=null){
试一试{
dbConnection.close();
}捕获(SQLE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
退货清单;
}
}
xhtml:-Privilege.xhtml

<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>Welcome</title>
</h:head>
<h:body>
<h:form>
    <p:dataTable value="#{privilegeBean.messages}" var="u"
        id="datatbldispprivilege">

        <p:column headerText="Privileges">
            <p:outputLabel value="#{u.privilege}" />
        </p:column>
    </p:dataTable>
    <br />

    <h3>Add Employee</h3>
    <hr />
    <h:panelGrid id="pnlgrdlogin">
        <p:column headerText="Add Privileges">
            <p:inputText value="#{privilegeBean.privilege}" name="txtprivilege" /><br>    </br><br></br>
            <p:commandButton name="btmaddprivilege" value="Add Privilege"
                action="privilegeBean.addPrivileges" />
        </p:column>
    </h:panelGrid>
</h:form>
</h:body>
</html>

欢迎

添加员工




html文件使用数据表显示重复数据,mysql数据包含一个表“privilege”和两个属性“id”、“privilege”


当我从数据库中获取记录时,它给出1条记录,但当我打印它时,它给出2,3,4,5。。。记录每次日志记录时间。

每次在类
PrivilegeDao
上调用
public List getUserList()
时,将所有记录添加到
List
并返回此列表。 在方法
public list getUserList()
中创建本地列表将解决此问题

public List<Privilege> getUserList()
{
    List<Privilege> localList = new ArrayList<>();
    try
    {
        // ...
        while(rs.next())
        {
            prg.setPrivilege(rs.getString("privilege"));
            localList.add(prg);
        }
    }
    // ...
    return localList;
}
public List getUserList()
{
List localList=new ArrayList();
尝试
{
// ...
while(rs.next())
{
prg.setPrivilege(rs.getString(“privilege”);
localList.add(prg);
}
}
// ...
返回localList;
}