2016-05-14 37 views
0

我必須做一個網頁,從jdbc和mysql數據庫中選擇數據。沒有看到任何問題。但頁面不從數據庫中獲取數據庫值,它只是顯示列名稱。我可以如何在網頁的表格上顯示數據庫值?mysql jdbc連接沒有錯誤,但不能在網頁上工作

這是我的工作人員類:

import com.mysql.jdbc.Connection; 
import com.mysql.jdbc.PreparedStatement; 
import com.mysql.jdbc.ResultSet; 
import java.sql.DriverManager; 
import java.util.ArrayList; 
import java.util.List; 

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* 
* 

*/ 
public class Staff { 
    List <Kisiler> sorguSonucu; 

    public List<Kisiler> getSorguSonucu(List<Kisiler> sorguSonucu){ 
     return sorguSonucu; 
    } 
    public List<Kisiler> getTablodakiKayitlar() 
    { 
     Connection connection = null; 
     PreparedStatement preparedStatement = null; 
     ResultSet resultSet = null; 

     sorguSonucu = new ArrayList<Kisiler>(); 

     try{ 
      Class.forName("com.mysql.jdbc.Driver"); 

      String url = "jdbc:mysql://localhost:3309/staff"; 
      String user = "admin"; 
      String psw = "admin"; 
      Class.forName("com.mysql.jdbc.Driver"); 
      connection = (Connection) DriverManager.getConnection(url, user, psw); 

      preparedStatement = (PreparedStatement) connection.prepareStatement("SELECT * FROM staff"); 
      resultSet = (ResultSet) preparedStatement.executeQuery(); 
      while(resultSet.next()){ 
       Kisiler kisiler = new Kisiler(); 
       kisiler.setId(resultSet.getInt("id")); 
       kisiler.setFirstName(resultSet.getString("firstName")); 
       kisiler.setLastName(resultSet.getString("lastName")); 
       kisiler.setTelephone(resultSet.getInt("telephone")); 
       kisiler.setEmail(resultSet.getString("email")); 
       sorguSonucu.add(kisiler); 
      } 
     } 
     catch(Exception e){ 
      System.err.println("Hata meydana geldi.Hata: " + e); 
     } 

     finally{ 
      try{ 
       connection.close(); 
       preparedStatement.close(); 
      } 
      catch(Exception e){ 
       System.err.println("Hata meydana geldi.Hata: " + e); 
      } 
     } 

    return sorguSonucu; 
      } 

} 

和我有kisiler類

class Kisiler { 

    private int id; 
private String firstName; 
private String lastName; 
private int telephone; 
private String email; 

/** 
* @return the id 
*/ 
public int getId() { 
    return id; 
} 

/** 
* @param id the id to set 
*/ 
public void setId(int id) { 
    this.id = id; 
} 

/** 
* @return the firstName 
*/ 
public String getFirstName() { 
    return firstName; 
} 

/** 
* @param firstName the firstName to set 
*/ 
public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 

/** 
* @return the lastName 
*/ 
public String getLastName() { 
    return lastName; 
} 

/** 
* @param lastName the lastName to set 
*/ 
public void setLastName(String lastName) { 
    this.lastName = lastName; 
} 

/** 
* @return the telephone 
*/ 
public int getTelephone() { 
    return telephone; 
} 

/** 
* @param telephone the telephone to set 
*/ 
public void setTelephone(int telephone) { 
    this.telephone = telephone; 
} 

/** 
* @return the email 
*/ 
public String getEmail() { 
    return email; 
} 

/** 
* @param email the email to set 
*/ 
public void setEmail(String email) { 
    this.email = email; 
} 

}

和我的html代碼:

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://xmlns.jcp.org/jsf/html" 
     xmlns:f="http://xmlns.jcp.org/jsf/core"> 
    <h:head> 
     <title>View Staff</title> 
    </h:head> 
    <h:body> 
     <h:form> 

      <h:dataTable value="#{staff.tablodakikayitlar}" var="kayitCekObjesi" 
         > 
       <h:column> 
        <f:facet name="header"> 
         ID 
        </f:facet> 
        <h:outputText value="#{kayitCekObjesi.id}"/> 
       </h:column> 
       <h:column> 
        <f:facet name="header"> 
         First Name 
        </f:facet> 
        <h:outputText value="#{kayitCekObjesi.firstName}"/> 
       </h:column> 
       <h:column> 
        <f:facet name="header"> 
         Last Name 
        </f:facet> 
        <h:outputText value="#{kayitCekObjesi.lastName}"/> 
       </h:column> 
       <h:column> 
        <f:facet name="header"> 
         Telephone 
        </f:facet> 
        <h:outputText value="#{kayitCekObjesi.telephone}"/> 
       </h:column>> 
       <h:column> 
        <f:facet name="header"> 
         E-mail 
        </f:facet> 
        <h:outputText value="#{kayitCekObjesi.email}"/> 
       </h:column>> 
      </h:dataTable> 
     </h:form> 
    </h:body> 
</html> 
+0

我想你命名了你的數據表的值是錯的。由於您的方法名稱是:'getTablodakiKayitlar',因此您應該將其稱爲'value =「#{staff.tablodakiKayitlar}」'嘗試它,如果它起作用我將添加爲答案。 CamelCase真的很重要 –

+0

感謝評論,但它仍然不起作用,表是這樣的http://i.hizliresim.com/6n7XNW.png – Jane

+0

那麼你檢查數據庫表中是否有實際的數據?在填充它之後添加'System.out.println()'並查看控制檯上顯示的內容。如果列表是空的,我還注意到其他問題:在你的列上有幾處錯誤''看到雙'>'?這可能會導致錯誤。 –

回答

0

首先請檢查來自Databa的價值se實際上是通過調試返回或者不是。如果你看到value實際返回,那麼檢查kisiler類是否有getter方法,用於kisiler類中的那些屬性id; firstName; .lastName。請檢查kisiler類中的那些屬性是否有getter方法。

+0

現在我得到一個錯誤「/index.xhtml @ 18,65 value =」#{kayitCekObjesi.id}「:屬性'id'在Kisiler類型上不可讀」,並且我在主消息 – Jane

+0

http上添加了kisiler類://stackoverflow.com/questions/21887122/property-not-readable-on-type-java-lang-string-when-using-jsf-primefaces。請參閱此鏈接。您的kisiler類需要公開。 – sawyinwaimon

+0

謝謝你 – Jane

相關問題