2011-05-09 73 views
0

我有一個bean類員工具有屬性id,名稱以及公共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> 

的善意回應。 在此先感謝。

回答

2

我認爲這可能是(again)getter/setter方法命名的問題。如果你的屬性名爲

private List<Employee> perInfoAll 

getter方法必須

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

注意方法名中的大寫字母「P」。

此外,你的facelet中的el表達式之後不需要分號。