Databases are an important part of most web applications. In this web presentation a database is set up for the Cars JSF web application. It illustrates the use of the following tools:
Source code and web pages for the Cars JSF web application are available here.
Two steps are require to set up and populate the database tables for the application:
To make the Cars application run it must have access to the database.
The rest of this presentation provides some additional information:
Complete versions of the NetBeans IDE are bundled with an Apache Derby database server. It provides support for creating a Derby database and building tables in the database.
There are five tables in the Cars database:
The SQL files for building, populating, and viewing these tables can are included in the Cars project in the "Web Pages/sql/" folder.
Detailed steps for executing SQL are given for the person table.
There are 3 SQL files for the Manufacturer
table:
These files should be executed in order.
The last one is a select
statement.
You should view the select results by clicking on the
"manufacturer-select.sql" tab in NetBeans.
There are 4 SQL files for the Model
table:
These files should be executed in order.
The last two are both select
statements.
You should view the select results and notice the difference.
There are 4 SQL files for the Car
table:
These files should be executed in order.
The last two are both select
statements.
You should view the select results and notice the difference.
There are 3 SQL files for the Ownership
table:
These files should be executed in order.
The last two are both select
statements.
You should view the select results and notice the difference.
Once a connection pool resource is set up for a JSF application the
application can have access to the database while it is running.
In order to use the access, database access code needs to inject the
resource with an @Resource
annotation added to a variable
of type DataSource.
The "name" parameter for the annotation must match the JNDI name for
the resource.
The annotation automatically initializes the variable when the bean that
contains it is created.
You should not make any assignment to the variable.
Typically the only thing you do with it is send it
getConnection()
messages.
The code below is part of the code for the cars.Database class. It captures the ideas presented above.