Il bean
package nextre.controller;
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;
import nextre.entity.City;
@ManagedBean(name="ReportBean")
@SessionScoped
public class ReportBean implements Serializable {
/*
* In JSF i bean che possono essere acceduti dalle pagine JSF sono detti managed
* bean. Si tratta di normalissimi bean java che possono contenere proprietà con
* i relativi metodi get e set, ma anche logica di business. Un managed bean può
* essere configurato sia utilizzando un file XML (faces-config.xml obbligatorio
* in JSX 1.x) ma anche attraverso le annotation.
*/
private static final long serialVersionUID = 1L;
public List<City> getCity() throws ClassNotFoundException, SQLException {
Connection connect = null;
String url = "jdbc:mysql://localhost:3306/venere_mod";
String username = "root";
String password = "root";
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection(url, username, password);
} catch (SQLException ex) {
System.out.println("in exec");
System.out.println(ex.getMessage());
}
List<City> citys = new ArrayList<City>();
PreparedStatement pstmt = connect.prepareStatement("select * from city");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
City city = new City();
city.setId(rs.getInt("id"));
city.setAccentCity(rs.getString("accent_city"));
city.setCity(rs.getString("city"));
city.setCountry(rs.getString("country"));
citys.add(city);
}
rs.close();
pstmt.close();
connect.close();
return citys;
}
}
il model
package nextre.entity;
public class City {
private int id;
private String accentCity;
private String city;
private String country;
public City(int id, String accentCity, String city, String country) {
this.id = id;
this.accentCity = accentCity;
this.city = city;
this.country = country;
}
public City() {}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAccentCity() {
return accentCity;
}
public void setAccentCity(String accentCity) {
this.accentCity = accentCity;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
index.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>Report City Project Venere</title>
</h:head>
<h:body>
<h2>City Dettagli:</h2>
<h:dataTable value="#{ReportBean.citys}" var="mycity" border="2">
<h:column><f:facet name="header">Id</f:facet> #{mycity.id}</h:column>
<h:column><f:facet name="header">accent City</f:facet> #{mycity.accentCity}</h:column>
<h:column><f:facet name="header">city</f:facet>#{mycity.city}</h:column>
<h:column><f:facet name="header">country</f:facet>#{mycity.country}</h:column>
</h:dataTable>
</h:body>
</html>
i driver che ho scaricato nel pom sono:
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.faces/jsf-api -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.faces/jsf-impl -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.sun.el</groupId>
<artifactId>el-ri</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Vorrei sapere come mai non mi estrapola i dati nel database??