ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzatz_5.4.0.1/51/program/jspqry.htm

98 lines
5.4 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
<title>&lt;tsx:dbquery&gt;</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h5><a name="jspqry"></a>&lt;tsx:dbquery&gt;</h5>
<p>Use the &lt;tsx:dbquery&gt; syntax to establish a connection to a database, submit database queries, and return the results set.</p>
<p>The &lt;tsx:dbquery&gt; tag:</p>
<ul>
<li>Refers to a &lt;tsx:dbconnect&gt; in the same JSP file and uses the information it provides to determine the database URL and driver. The user ID and password are also obtained from the &lt;tsx:dbconnect&gt; if those values are provided in the &lt;tsx:dbconnect&gt;.</li>
<li>Establishes a new connection.</li>
<li>Retrieves and caches data in the results object.</li>
<li>Closes the connection (releases the connection resource).</li>
</ul>
<p>The &lt;tsx:dbquery&gt; syntax is:</p>
<pre>&lt;tsx:dbquery id=&quot;<em>query_id</em>&quot; connection=&quot;<em>connection_id</em>&quot; limit=&quot;<em>value</em>&quot; &gt;
&lt;%-- SELECT commands and (optional) JSP syntax can be --%&gt;
&lt;%-- placed within the tsx:dbquery tag. Any other --%&gt;
&lt;%-- syntax, including HTML comments, are not valid. --%&gt;
&lt;/tsx:dbquery&gt;</pre>
<p>This list describes the attributes and their values:</p>
<ul>
<li><p><strong>id</strong>
<br>The identifier of this query. The scope is the JSP file. This identifier is used to reference the query, for example, from the &lt;tsx:getProperty&gt; tag to display query results.</p>
<p>The id is a tsx reference to the bean and can be used to retrieve the bean from the page context. For example, if id is named mySingleDBBean, replace this:</p>
<pre>if (mySingleDBBean.getValue("UISEAM",0).startsWith("N"))</pre>
<p>with this:</p>
<pre>com.ibm.ws.webcontainer.jsp.tsx.db.QueryResults bean =
(com.ibm.ws.webcontainer.jsp.tsx.db.QueryResults)pageContext. findAttribute("mySingleDBBean");
if (bean.getValue("UISEAM",0).startsWith("N")). . . </pre>
<p>The bean properties are dynamic and the property names are the names of the columns in the results set. If you want different column names, use the SQL keyword for specifying an alias on the SELECT command. In the following example, the database table contains columns named FNAME and LNAME, but the SELECT statement uses the <tt>AS</tt> keyword to map those column names to FirstName and LastName in the results set:</p>
<pre>Select FNAME, LNAME AS FirstName, LastName from Employee where FNAME='Jim'</pre></li>
<li><p><strong>connection</strong>
<br>The identifier of a &lt;tsx:dbconnect&gt; in this JSP file. That &lt;tsx:dbconnect&gt; provides the database URL, driver name, and (optionally) the user ID and password for the connection.</p></li>
<li><p><strong>limit</strong>
<br>An optional attribute that constrains the maximum number of records returned by a query. If the attribute is not specified, no limit is used and the effective limit is determined by the number of records and the system caching capability.</p></li>
<li><p>SELECT command and JSP syntax
<br>Because the &lt;tsx:dbquery&gt; must return a results set, the only valid SQL command is SELECT. For more information about the SELECT command, see this resource:</p>
<ul>
<li><a href="../../../icbase/db2/v5r2rbafzmst02.htm" target="_blank">DB2 Universal Database for iSeries SQL Reference (V5R2)</a></li>
<li><a href="../../../icbase/db2/v5r3rbafzmst02.htm" target="_blank">DB2 Universal Database for iSeries SQL Reference (V5R3)</a></li>
</ul></li>
</ul>
<p>In the following example, a database is queried for data about employees in a specified department. The department is specified using the &lt;tsx:getProperty&gt; to embed a variable data field. The value of that field is based on user input.</p>
<pre>&lt;% String workdept = request.getParameter(&quot;WORKDEPT&quot;); %&gt;
&lt;tsx:dbquery id=&quot;qs2&quot; connection=&quot;conn&quot; &gt;
select * from WSDEMO.EMPLOYEE where WORKDEPT= '&lt;%=workdept%&gt;'
&lt;/tsx:dbquery&gt;</pre>
<p><strong>Displaying query results</strong></p>
<p>To display the query results, use the &lt;tsx:repeat&gt; and &lt;tsx:getProperty&gt; syntax. The &lt;tsx:repeat&gt; loops through each of the rows in the query results. The &lt;tsx:getProperty&gt; uses the query results object (for the &lt;tsx:dbquery&gt; syntax whose identifier is specified by the &lt;tsx:getProperty&gt; bean attribute) and the appropriate column name (specified by the &lt;tsx:getProperty&gt; property attribute) to retrieve the value. For example:</p>
<pre>&lt;tsx:repeat&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;tsx:getProperty name=&quot;empqs&quot; property=&quot;EMPNO&quot; /&gt;
&lt;tsx:getProperty name=&quot;empqs&quot; property=&quot;FIRSTNME&quot; /&gt;
&lt;tsx:getProperty name=&quot;empqs&quot; property=&quot;WORKDEPT&quot; /&gt;
&lt;tsx:getProperty name=&quot;empqs&quot; property=&quot;EDLEVEL&quot; /&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tsx:repeat&gt;</pre>
<p>The <a href="jspexam.htm">JSP10employeeRepeatResults.jsp</a> example illustrates the syntax of the &lt;tsx:getProperty&gt; tag. </p>
</body>
</html>