无法使用 dataTable 显示数据库表值

发布于 2024-11-05 17:36:05 字数 1850 浏览 0 评论 0原文

我有一个 bean 类员工,具有属性 id、name 以及公共 getter 和 setter。

我使用以下 bean 进行数据库连接并从数据库表中获取值。

TableBean.java:

  public class TableBean{
    public Connection getVConnection() throws Exception{
      String driver = "oracle.jdbc.driver.OracleDriver";
      String url = "jdbc:oracle:thin:@localhost:1521:globldb3";
      String username = "scott";
      String password = "tiger";
      Class.forName(driver);
      Connection conn = DriverManager.getConnection(url, username, password);
      return conn;
      }    
    public List<Employee> getPerInfoAll() {
    int i = 0;
    Connection conn = null;
    PreparedStatement pstmt = null;
    List<Employee> perInfoAll = new ArrayList(); 
     try {
        conn = getVConnection();
            String query = "select * from employee where e_id>5400";
            pstmt = conn.prepareStatement(query);
            rs = pstmt.executeQuery();
            while(rs.next()){
           System.out.println(rs.getString(1));
               perInfoAll.add(i,newEmployee(rs.getInt(1),rs.getString(2)));
               i++;
             }
             pstmt.close();
             rs.close();
             conn.close();    
      }catch (Exception e){
           e.printStackTrace();
      }
     return perInfoAll;
     }

以下是jsf页面:

 <h:dataTable value="#{TableBean.perInfoAll}" var="e">
        <h:column>
            <f:facet name="header">Employee id</f:facet>
            <h:outputText value="#{e.id}">
        </h:column>

        <h:column>
            <f:facet name="header">Employee name</f:facet>
                     <h:outputText value="#{e.name}">
        </h:column>
    </h:dataTable>

请回复。 提前致谢。

I'm having a bean class employee having attributes id,name along with public getters and setters.

I'm using following bean for db connection and getting values from database table.

TableBean.java:

  public class TableBean{
    public Connection getVConnection() throws Exception{
      String driver = "oracle.jdbc.driver.OracleDriver";
      String url = "jdbc:oracle:thin:@localhost:1521:globldb3";
      String username = "scott";
      String password = "tiger";
      Class.forName(driver);
      Connection conn = DriverManager.getConnection(url, username, password);
      return conn;
      }    
    public List<Employee> getPerInfoAll() {
    int i = 0;
    Connection conn = null;
    PreparedStatement pstmt = null;
    List<Employee> perInfoAll = new ArrayList(); 
     try {
        conn = getVConnection();
            String query = "select * from employee where e_id>5400";
            pstmt = conn.prepareStatement(query);
            rs = pstmt.executeQuery();
            while(rs.next()){
           System.out.println(rs.getString(1));
               perInfoAll.add(i,newEmployee(rs.getInt(1),rs.getString(2)));
               i++;
             }
             pstmt.close();
             rs.close();
             conn.close();    
      }catch (Exception e){
           e.printStackTrace();
      }
     return perInfoAll;
     }

Following is jsf page:

 <h:dataTable value="#{TableBean.perInfoAll}" var="e">
        <h:column>
            <f:facet name="header">Employee id</f:facet>
            <h:outputText value="#{e.id}">
        </h:column>

        <h:column>
            <f:facet name="header">Employee name</f:facet>
                     <h:outputText value="#{e.name}">
        </h:column>
    </h:dataTable>

Kindly reply.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

本王不退位尔等都是臣 2024-11-12 17:36:05

我认为这可能是(再次)getter/setter 的问题方法命名。如果您的属性已命名,则

private List<Employee> perInfoAll

getter 方法必须

public List<Employee> getPerInfoAll() { ... }

注意方法名称中的大写“P”。

此外,您的 Facelet 中的 el 表达式后面不需要分号。

I think it may be (again) a problem of getter/setter method naming. If your property is named

private List<Employee> perInfoAll

the getter method must be

public List<Employee> getPerInfoAll() { ... }

Notice the upper case "P" in the method name.

Furthermore, you don't need the semicolon after the el expression in your facelet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文