The Cars web application is designed to keep track of information about cars and their owners in a database. Its user interface allows a user to query the database to find the owners of a car given its vehicle identification number (VIN).
The Cars web application can be run here. It is available as a zipped Netbeans project. It illustrates the following:
The Cars JSF application is defined by the following:
Java classes (Source Packages/cars/):
Java utility class for SQL (Source Packages/sql/):
JSF pages (Web Pages/):
SQL files (Source Packages/sql/):
The two JSF managed beans are defined by the Database.java and OwnerQuery.java files. The best place to start is the Database.java file. Its class defines an application scoped JSF managed bean that directly accesses the database. It encapsulates the resource injection and JDBC behind a simple Java interface. JSF pages and the OwnerQuery class access the database only by invoking the interface methods provided by the Database class.
The OwnerQuery class defines a request scoped bean that manages queries from the user. It receives query data from the user in the index.xhtml page and invokes database bean methods to set properties that are displayed in the owners.xhtml page.
Once a GlassFish connection pool resource is set up, it can be injected into JSF managed beans using a @Resource annotation on a variable. Beans that need database connections can obtain them through the injected variable. The beans can use Java Database Connectivity (JDBC) to send Structured Query Language (SQL) statements and retrieve their results through the connections.