String query = "SELECT vin FROM car"; Connection carsConnection = carsSource.getConnection(); Statement statement = carsConnection.createStatement(); ResultSet resultSet = statement.executeQuery(query); // use the result here carsConnection.close();
List<String> vinList = new ArrayList<String>(); while (resultSet.next()) { vinList.add(resultSet.getString(columnName)); }The list can then be used to populate a JSF input selection component:
<h:selectOneListbox value="#{ownerQuery.vin}">
<f:selectItems value="#{database.vinList}"/>
</h:selectOneListbox>
Note that the result set might have column values that are not strings.
In that case you would use the appropriate ResultSet
method: getInt, getDouble, getBlob, etc.
Opening and closing database connections using data sources involves connection pools. Each JSF cycle that uses a database must open and close a connection; you can't hold onto a connection across multiple JSF cycles.
If there are SQLExceptions during database processing and they aren't properly caught, impatient users can repeatedly make doomed requests that cause database connection leaks.
You can make sure that a connection is properly closed by using a try with resources statement.
Recommendation: make each method that deals with the database use the try with resources:
public someDBMethod() throws SQLException { String query = ... ; try (Connection carsConnection = carsSource.getConnection()) { // make the query and process result } }
SELECT vin FROM caris easy enough to include in Java code, but most queries are not so simple. Consider:
SELECT car.vin as "VIN", manufacturer."name" AS "Manufacturer", model."name" AS "Model" FROM car, manufacturer, model WHERE car.vin = 0123456789abcdefg AND car.model = model.id AND model.manufacturer = manufacturer.idIt is better to separate query strings from Java code by storing them in files, as we show in this section.
SELECT vin FROM caris contained in vin-query.sql. Then our statement example from before should have:
String query = SQL.getSQL("vin-query"); ... ResultSet resultSet = statement.executeQuery(query);You are encouraged to use the the SQL.java class in your own web apps.
someDBMethod() throws SQLException, IOException { try(Connection carsConnection = carsSource.getConnection()) { String query = SQL.getSQL(...) ; // make the query and process result } }
SELECT person."name" FROM person, ownership, car
WHERE
ownership.owner = person.id
AND ownership.car = car.id
AND car.vin = 0123456789abcdefg
But the vin cannot be known at the time this file is created.
SELECT person."name" FROM person, ownership, car
WHERE
ownership.owner = person.id
AND ownership.car = car.id
AND car.vin = ?
try(Connection carsConnection = carsSource.getConnection()) { String query = SQL.getSQL("owner-query"); PreparedStatement statement = carsConnection.prepareStatement(query); statement.setString(1, vin); ResultSet resultSet = statement.executeQuery(); // use the result here }
<h:dataTable>
tag can take as a value the
result returned by an SQL query of a database.
The database example illustrates how this is done.