Heyhou ich versuche mich grade an einer JSF JDBC Anwendung ein Login Example habe ich auch schon hinbekommen daher gehe ich aus das meine Datenbankverbindung soweit funktioniert leider gibt es bei dem Crud Example ein Problem...vielleicht könnt ihr mir helfen wieso werden die Daten aus der Datenbanktabelle nicht angezeigt ich sehe nur die Überschriften der Tabelle :/
Alles läuft auf der Eclipse EE und auf einem Tomcat 8.5 verwendet wird jsf 2.1.7
[CODE lang="java" title="Car.java"]
public class Car {
private Integer cid;
private String cname;
private String color;
private Integer speed;
private String mfdctry;
public Car() {
}
public Car(Integer cid, String cname, String color, Integer speed,
String mfdctry) {
this.cid = cid;
this.cname = cname;
this.color = color;
this.speed = speed;
this.mfdctry = mfdctry;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Integer getSpeed() {
return speed;
}
public void setSpeed(Integer speed) {
this.speed = speed;
}
public String getMfdctry() {
return mfdctry;
}
public void setMfdctry(String mfdctry) {
this.mfdctry = mfdctry;
}
}
[/CODE]
[CODE lang="java" title="CarBean.java"]import java.sql.*;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class CarBean {
private final String url = "jdbcostgresql://localhost/postgres";
private final String user ="postgres";
private final String password ="admin";
private void connect() {
try(Connection con = DriverManager.getConnection(url, user, password) {
if(con != null) {
System.out.println("Verbunden");
}else {
System.out.println("failed to connect");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<Car> getCars() throws ClassNotFoundException, SQLException {
Connection connect = null;
String url = "jdbcostgresql://localhost/postgres";
String username = "postgres";
String password = "admin";
try {
Class.forName ("org.postgresql.Driver");
connect = DriverManager.getConnection(url, username, password);
System.out.println("Connection established"+connect);
} catch (SQLException ex) {
System.out.println("in exec");
System.out.println(ex.getMessage());
System.out.println("Verbindung wurde nicht hergestellt");
}
List<Car> cars = new ArrayList<Car>();
PreparedStatement pstmt = connect
.prepareStatement("select car_id, cname, color, speed, Manufactured_Country from Car");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Car car = new Car();
car.setCid(rs.getInt("car_id"));
car.setCname(rs.getString("cname"));
car.setColor(rs.getString("color"));
car.setSpeed(rs.getInt("speed"));
car.setMfdctry(rs.getString("Manufactured_Country"));
cars.add(car);
}
// close resources
rs.close();
pstmt.close();
connect.close();
return cars;
}
public static void main(String[] args) {
CarBean connect = new CarBean();
connect.connect();
}
}[/CODE]
und meine
[CODE lang="xml" title="car.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">
<html xmlns="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 JDBC Integration</title>
</h:head>
<h:body>
<h2>Car Details</h2>
<h:dataTable value="#{carBean.cars}" var="car" border="2">
<h:column>
<f:facet name="header">Car ID</f:facet>
#{car.cid}
</h:column>
<h:column>
<f:facet name="header">Car Name</f:facet>
#{car.cname}
</h:column>
<h:column>
<f:facet name="header">Car Color</f:facet>
#{car.color}
</h:column>
<h:column>
<f:facet name="header">Car Speed</f:facet>
#{car.speed}
</h:column>
<h:column>
<f:facet name="header">Manufactured Country</f:facet>
#{car.mfdctry}
</h:column>
</h:dataTable>
</h:body>
</html>[/CODE]
und die Web.xml
[CODE lang="xml" title="web.xml"]<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>car.xhtml</welcome-file>
</welcome-file-list>
</web-app>[/CODE]
Ich weiß leider nichtmehr weiter meine faces-config.xml
[CODE lang="java" title="faces-config.xml"]<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<navigation-rule>
<description>StartPage</description>
<from-view-id>/car.xhtml</from-view-id>
</navigation-rule>
</faces-config>[/CODE]
Wie gesagt ich sehe nur die Tabellen der Car.xhtml aber nicht die Einträge aus der Datenbank die verbindung funktioniert aber...ihr wert mir echt eine Hilfe..
Alles läuft auf der Eclipse EE und auf einem Tomcat 8.5 verwendet wird jsf 2.1.7
[CODE lang="java" title="Car.java"]
public class Car {
private Integer cid;
private String cname;
private String color;
private Integer speed;
private String mfdctry;
public Car() {
}
public Car(Integer cid, String cname, String color, Integer speed,
String mfdctry) {
this.cid = cid;
this.cname = cname;
this.color = color;
this.speed = speed;
this.mfdctry = mfdctry;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Integer getSpeed() {
return speed;
}
public void setSpeed(Integer speed) {
this.speed = speed;
}
public String getMfdctry() {
return mfdctry;
}
public void setMfdctry(String mfdctry) {
this.mfdctry = mfdctry;
}
}
[/CODE]
[CODE lang="java" title="CarBean.java"]import java.sql.*;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class CarBean {
private final String url = "jdbcostgresql://localhost/postgres";
private final String user ="postgres";
private final String password ="admin";
private void connect() {
try(Connection con = DriverManager.getConnection(url, user, password) {
if(con != null) {
System.out.println("Verbunden");
}else {
System.out.println("failed to connect");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<Car> getCars() throws ClassNotFoundException, SQLException {
Connection connect = null;
String url = "jdbcostgresql://localhost/postgres";
String username = "postgres";
String password = "admin";
try {
Class.forName ("org.postgresql.Driver");
connect = DriverManager.getConnection(url, username, password);
System.out.println("Connection established"+connect);
} catch (SQLException ex) {
System.out.println("in exec");
System.out.println(ex.getMessage());
System.out.println("Verbindung wurde nicht hergestellt");
}
List<Car> cars = new ArrayList<Car>();
PreparedStatement pstmt = connect
.prepareStatement("select car_id, cname, color, speed, Manufactured_Country from Car");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Car car = new Car();
car.setCid(rs.getInt("car_id"));
car.setCname(rs.getString("cname"));
car.setColor(rs.getString("color"));
car.setSpeed(rs.getInt("speed"));
car.setMfdctry(rs.getString("Manufactured_Country"));
cars.add(car);
}
// close resources
rs.close();
pstmt.close();
connect.close();
return cars;
}
public static void main(String[] args) {
CarBean connect = new CarBean();
connect.connect();
}
}[/CODE]
und meine
[CODE lang="xml" title="car.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">
<html xmlns="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 JDBC Integration</title>
</h:head>
<h:body>
<h2>Car Details</h2>
<h:dataTable value="#{carBean.cars}" var="car" border="2">
<h:column>
<f:facet name="header">Car ID</f:facet>
#{car.cid}
</h:column>
<h:column>
<f:facet name="header">Car Name</f:facet>
#{car.cname}
</h:column>
<h:column>
<f:facet name="header">Car Color</f:facet>
#{car.color}
</h:column>
<h:column>
<f:facet name="header">Car Speed</f:facet>
#{car.speed}
</h:column>
<h:column>
<f:facet name="header">Manufactured Country</f:facet>
#{car.mfdctry}
</h:column>
</h:dataTable>
</h:body>
</html>[/CODE]
und die Web.xml
[CODE lang="xml" title="web.xml"]<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>car.xhtml</welcome-file>
</welcome-file-list>
</web-app>[/CODE]
Ich weiß leider nichtmehr weiter meine faces-config.xml
[CODE lang="java" title="faces-config.xml"]<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<navigation-rule>
<description>StartPage</description>
<from-view-id>/car.xhtml</from-view-id>
</navigation-rule>
</faces-config>[/CODE]
Wie gesagt ich sehe nur die Tabellen der Car.xhtml aber nicht die Einträge aus der Datenbank die verbindung funktioniert aber...ihr wert mir echt eine Hilfe..