ich möchte meine JSF Seite mir Daten aus der Datenbank füllen. Dazu habe ich folgenden Code geschrieben. Wenn ich die .xhtml Seite ausführe bekomme ich nur den Text aus den facet tags. Mache ich etwas falsch? Ich benutze Eclipse, Tomcat 8.5, Maven, jdbc driver und XAMPP für den MySQL Server. Welche Möglichkeit/Debugging gibt es um auf dem MySQL Server nachzuschauen ob überhaupt auf die Datenbank zugegriffen wurde ?
.xhtml
<?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"><htmlxmlns="http://www.w3.org/1999/xhtml"xmlns:f="http://java.sun.com/jsf/core"xmlns:h="http://java.sun.com/jsf/html"><h:head><title>JSF 2.0 Hello World</title></h:head><h:body><h1>JSF 2.0 + JDBC Example</h1><h:dataTablevalue="#{testuser.testuser()}"var="c"><h:column><f:facetname="header">
ID_Hardware
</f:facet><h:outputTextvalue="#{c.id_Hardware}"/></h:column><h:column><f:facetname="header">
Color
</f:facet><h:outputTextvalue="#{c.Color}"/></h:column><h:column><f:facetname="header">
Format
</f:facet><h:outputTextvalue="#{c.Format}"/></h:column><h:column><f:facetname="header">
HypAS
</f:facet><h:outputTextvalue="#{c.HyPAS}"/></h:column><h:column><f:facetname="header">
id_fw_release
</f:facet><h:outputTextvalue="#{c.id_fw_release}"/></h:column><h:column><f:facetname="header">
PPM
</f:facet><h:outputTextvalue="#{c.PPM}"/></h:column><h:column><f:facetname="header">
id_type
</f:facet><h:outputTextvalue="#{c.id_type}"/></h:column><h:column><f:facetname="header">
Launch_Date
</f:facet><h:outputTextvalue="#{c.Launch_Date}"/></h:column><h:column><f:facetname="header">
End_of_Sale
</f:facet><h:outputTextvalue="#{c.End_of_Sale}"/></h:column><h:column><f:facetname="header">
End_of_support
</f:facet><h:outputTextvalue="#{c.end_of_support}"/></h:column><h:column><f:facetname="header">
Link
</f:facet><h:outputTextvalue="#{c.link}"/></h:column></h:dataTable></h:body></html>
The testuser.java(bean)
package jsfexampl;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.ManagedBean;
import javax.enterprise.context.SessionScoped;
import jsfexampl.DBM;
@ManagedBean("testuser")
@SessionScoped
public class testuser implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int id_Hardware;
private int Color;
private String Format;
private int HyPAS;
private int id_fw_release;
private int PPM;
private int id_type;
private Date Launch_Date;
private Date End_of_Sale;
private Date end_of_support;
private String link;
private static String db_table = "hardware";
public testuser(){
}
public testuser(int id_Hardware,int Color, String Format, int HyPAS,int id_fw_release,int PPM,int id_type, Date Launch_Date, Date End_of_Sale, Date end_of_support, String link) {
super();
this.id_Hardware = id_Hardware;
this.Color = Color;
this.Format = Format;
this.HyPAS = HyPAS;
this.id_fw_release = id_fw_release;
this.PPM = PPM;
this.id_type = id_type;
this.Launch_Date = Launch_Date;
this.End_of_Sale = End_of_Sale;
this.end_of_support = end_of_support;
this.link = link;
}
public int getId_Hardware() {
return id_Hardware;
}
public void setId_Hardware(int id_Hardware) {
this.id_Hardware = id_Hardware;
}
public int getColor() {
return Color;
}
public void setColor(int color) {
Color = color;
}
public String getFormat() {
return Format;
}
public void setFormat(String format) {
Format = format;
}
public int getHyPAS() {
return HyPAS;
}
public void setHyPAS(int hyPAS) {
HyPAS = hyPAS;
}
public int getId_fw_release() {
return id_fw_release;
}
public void setId_fw_release(int id_fw_release) {
this.id_fw_release = id_fw_release;
}
public int getPPM() {
return PPM;
}
public void setPPM(int pPM) {
PPM = pPM;
}
public int getId_type() {
return id_type;
}
public void setId_type(int id_type) {
this.id_type = id_type;
}
public Date getLaunch_Date() {
return Launch_Date;
}
public void setLaunch_Date(Date launch_Date) {
Launch_Date = launch_Date;
}
public Date getEnd_of_Sale() {
return End_of_Sale;
}
public void setEnd_of_Sale(Date end_of_Sale) {
End_of_Sale = end_of_Sale;
}
public Date getEnd_of_support() {
return end_of_support;
}
public void setEnd_of_support(Date end_of_support) {
this.end_of_support = end_of_support;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
//Array output
public List<testuser> getList(DBM dbm, String cid, boolean close) throws Exception{
List<testuser> list = new ArrayList<testuser>();
PreparedStatement preState = null;
ResultSet resultSet = null;
try {
if( dbm == null ){
dbm = new DBM();
}
String sql = "SELECT * FROM "+db_table;
preState = dbm.initConnection().prepareStatement(sql);
preState.setString(1, cid);
resultSet = preState.executeQuery();
while (resultSet.next()) {
list.add( new testuser(resultSet.getInt(1),resultSet.getInt(2),
resultSet.getString(3),resultSet.getInt(4),resultSet.getInt(5),
resultSet.getInt(6),resultSet.getInt(7),resultSet.getDate(8),
resultSet.getDate(9),resultSet.getDate(10),resultSet.getString(11)) );
}
}catch (Exception e) {
e.printStackTrace();
}finally{
if( preState != null )
preState.close();
if( close && dbm.connection != null )
dbm.connection.close();
}
return list;
}
}
Frage
Gast DNIM_10
Hallo zusammen,
ich möchte meine JSF Seite mir Daten aus der Datenbank füllen. Dazu habe ich folgenden Code geschrieben. Wenn ich die .xhtml Seite ausführe bekomme ich nur den Text aus den facet tags. Mache ich etwas falsch? Ich benutze Eclipse, Tomcat 8.5, Maven, jdbc driver und XAMPP für den MySQL Server. Welche Möglichkeit/Debugging gibt es um auf dem MySQL Server nachzuschauen ob überhaupt auf die Datenbank zugegriffen wurde ?
.xhtml
The testuser.java(bean)
DBM.java (Controller):
package jsfexampl; import java.io.Serializable; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.regex.Pattern; import javax.faces.context.FacesContext; public class DBM implements Serializable { private static final long serialVersionUID = 9204275723046653468L; private String db_server = ""; private String db_user = ""; private String db_password = ""; private String db_driver = ""; public Connection connection = null; public DBM() throws Exception { init(); } private void init()throws Exception{ FacesContext fc = FacesContext.getCurrentInstance(); db_server = fc.getExternalContext().getInitParameter("DB-SERVER"); db_user = fc.getExternalContext().getInitParameter("DB-USER"); db_password = fc.getExternalContext().getInitParameter("DB-PASSWORD"); db_driver = fc.getExternalContext().getInitParameter("JDBC-DRIVER"); Class.forName(db_driver); } public Connection initConnection() throws Exception{ if( this.connection == null ){ this.connection = DriverManager.getConnection(db_server, db_user, db_password); this.connection.setAutoCommit(false); }else if( this.connection.isClosed() ){ this.connection = null; this.connection = DriverManager.getConnection(db_server, db_user, db_password); this.connection.setAutoCommit(false); } return this.connection; } public void closeConnection(){ try { if( this.connection != null ){ this.connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } public void commitConnection(){ try { if( this.connection != null && !this.connection.isClosed() ){ this.connection.commit(); } } catch (SQLException e) { e.printStackTrace(); } } }
Link zu diesem Kommentar
Auf anderen Seiten teilen
5 Antworten auf diese Frage
Empfohlene Beiträge
Dein Kommentar
Du kannst jetzt schreiben und Dich später registrieren. Wenn Du ein Konto hast, melde Dich jetzt an, um unter Deinem Benutzernamen zu schreiben.