Java Reference
In-Depth Information
The
Variable Name
field is the name that will be given to the variable that will hold
the result set generated by our query. The
Scope
field is the scope where this variable
will be stored, the variable can be stored on any valid scope (page, request, session,
or application). The
Data Source
field is for the JNDI name of the data source we
will be using to obtain a connection to the database, this data source must be added
as a resource reference to our web application's
web.xml
deployment descriptor, as
explained in the previous section.
After entering appropriate values for all fields and clicking on
OK
, the following
markup is generated in our page:
<sql:query var="allRows" dataSource="jdbc/sample">
SELECT name, city, state FROM customer
</sql:query>
The values of the
var
and
dataSource
attributes of the tag correspond to the values
we entered in the
Variable Name
and
Data Source
fields in the
Insert DB Query
window. Since page scope is the default scope, we don't see an attribute defining the
variable scope, had we picked a scope different from page, NetBeans would have
added a scope attribute to the tag, containing the scope for the variable as its value
(i.e.
scope="session"
).
We then need to add some logic to our page to traverse the result set, this is typically
done through the
<c:forEach>
tag. After adding the required markup, the body of
our page now looks like this:
<body>
<h2>Hello World!</h2>
<sql:query var="allRows" dataSource="jdbc/sample">
SELECT name, city, state FROM customer
</sql:query>
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Location</th>
</tr>
</thead>
<tbody>
<c:forEach var="currentRow"
items="${allRows.rows}">
<tr>
<td>${currentRow.name}</td>
<td>${currentRow.city},
${currentRow.state}