7. stmt = conn.createStatement();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getConn() {
return conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
public Statement getStmt() {
return stmt;
}
public void setStmt(Statement stmt) {
this.stmt = stmt;
}
public DriverManager getDriverManager() {
return driverManager;
}
public void setDriverManager(DriverManager driverManager) {
this.driverManager = driverManager;
}
}
ApplicationConstants.java
Store standard error messages, file names etc in this file. Don't use file names anywhere in the project explicitely. This file
should be the only file where you should be able to update file names (apart from faces-config.xml)
package XXXX.constants;
public class ApplicationConstants {
public static final String ERROR_STRING = "There is some error executing the database query. Go to home page and
please try again.";
public static final String HOME_PAGE = "index";
public static final String REGISTERED_USERS = "registeredUsersReport";
public static final String REGISTERED_USERS_EXCEL = "registeredUsersReport.xls";
}
8. RegisteredUsersTO.java
Use a transfer object as resultset. This List of these objects will store the data retrieved from the database. This is a
standard coding practice.
package XXXX.resultsets;
import java.util.Date;
public class RegisteredUsersTO {
String firstName;
String lastName;
String emailAddress;
String addressLine1;
String addressLine2;
String country;
String state;
String city;
String pinCode;
Date dateOfBirth;
String gender;
/**
* @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 emailAddress
*/
public String getEmailAddress() {
return emailAddress;
}
/**
* @param emailAddress the emailAddress to set
*/
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
9. /**
* @return the addressLine1
*/
public String getAddressLine1() {
return addressLine1;
}
/**
* @param addressLine1 the addressLine1 to set
*/
public void setAddressLine1(String addressLine1) {
this.addressLine1 = addressLine1;
}
/**
* @return the addressLine2
*/
public String getAddressLine2() {
return addressLine2;
}
/**
* @param addressLine2 the addressLine2 to set
*/
public void setAddressLine2(String addressLine2) {
this.addressLine2 = addressLine2;
}
/**
* @return the country
*/
public String getCountry() {
return country;
}
/**
* @param country the country to set
*/
public void setCountry(String country) {
this.country = country;
}
/**
* @return the state
*/
public String getState() {
return state;
}
/**
* @param state the state to set
*/
public void setState(String state) {
this.state = state;
}
/**
* @return the city
*/
public String getCity() {
return city;
}
/**
* @param city the city to set
*/
public void setCity(String city) {
this.city = city;
}
/**
* @return the pinCode
10. */
public String getPinCode() {
return pinCode;
}
/**
* @param pinCode the pinCode to set
*/
public void setPinCode(String pinCode) {
this.pinCode = pinCode;
}
/**
* @return the dateOfBirth
*/
public Date getDateOfBirth() {
return dateOfBirth;
}
/**
* @param dateOfBirth the dateOfBirth to set
*/
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
/**
* @return the gender
*/
public String getGender() {
return gender;
}
/**
* @param gender the gender to set
*/
public void setGender(String gender) {
this.gender = gender;
}
}
BaseUIController.java
You can store all common methods/procedures which all controller classes will access under this class. Controller classes
will inherit this class.
package XXXX.ui;
import java.io.Serializable;
import javax.faces.context.FacesContext;
public class BaseUIController implements Serializable {
private static final long serialVersionUID = 1L;
private static final String appVersion = "1.0";
public String getRealFileStoragePath() {
return FacesContext.getCurrentInstance().getExternalContext()
.getRealPath("/");
}
}
11. RegisteredUsersUIController.java
package XXXX.ui;
import java.io.File;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.faces.application.FacesMessage;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import XXXX.constants.ApplicationConstants;
import XXXX.database.DbConnectionController;
import XXXX.resultsets.RegisteredUsersTO;
public class RegisteredUsersUIController extends BaseUIController {
private static final long serialVersionUID = 1L;
List<RegisteredUsersTO> registeredUsersTOList;
DbConnectionController dbConnectionController;
ResultSet resultSet;
String excelFile;
public String getRegisteredUsers()
{
dbConnectionController = new DbConnectionController();
this.registeredUsersTOList = new ArrayList<RegisteredUsersTO>();
String query = "SELECT FIRST_NAME, LAST_NAME, EMAIL_ADDRESS, ADDR_LINE_1, ADDR_LINE_2,"
+ " PIN_CODE, CITY_NAME, STATE_NAME, COUNTRY_NAME, DATE_OF_BIRTH, GENDER"
+ " FROM USER INNER JOIN USER_DETAILS ON USER.USER_ID=USER_DETAILS.USER_ID"
+ " INNER JOIN COUNTRY ON USER_DETAILS.COUNTRY_CODE=COUNTRY.COUNTRY_CODE";
try {
resultSet = dbConnectionController.getStmt().executeQuery(query);
while (resultSet.next()) {
RegisteredUsersTO registeredUsersTO = new RegisteredUsersTO();
registeredUsersTO.setFirstName(resultSet
.getString("FIRST_NAME"));
registeredUsersTO.setLastName(resultSet
.getString("LAST_NAME"));
registeredUsersTO.setEmailAddress(resultSet
.getString("EMAIL_ADDRESS"));
registeredUsersTO.setAddressLine1(resultSet
.getString("ADDR_LINE_1"));
registeredUsersTO.setAddressLine2(resultSet
.getString("ADDR_LINE_2"));
registeredUsersTO.setPinCode(resultSet
.getString("PIN_CODE"));
registeredUsersTO.setCity(resultSet
.getString("CITY_NAME"));
registeredUsersTO.setState(resultSet
16. index.xhtml
When the application starts, by default index.xhtml will get called. Once the user clicks on the commandLink present in
this file, registeredUsersReport.xhtml will show the detailed report.
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<h:form>
<div style="float: left; margin-left: 235px; margin-bottom: 5px;">
<h2>List Of Registered Users</h2>
<a4j:commandLink
action="#{registeredUsersUIController.getRegisteredUsers()}"
value="List of Registered Users of Website" />
</div>
</h:form>
</html>
Maven Update, Clean and Install
1) Right click on Project from project explorer
2) Select Maven-> Update Project
3) Right click on project from project explorer
4) select Run As -> Maven Clean
5) Right click on project from project explorer
6) select Run As -> Maven Install
This will now generate war file. You need to copy this war file and deploy it on tomcat.