21Writing CFX Tags with Java
■
The returnAsQuery() function, which accepts any Java-style ResultSet object and makes
it available to the calling page as a ColdFusion-style query object. You can cut and paste
this code into other CFX tags that need to return
ResultSet objects as CFML queries.
■
The two forms of the getTagAttr() function, which wraps around the attributeExists()
and getAttribute() methods from the CFX API that were used previously in Listing 30.1.
The first form requires that the attribute be passed to the tag, and the second form makes
an attribute be considered optional by using a default value when it is not provided. Again,
you can cut and paste these functions into your own CFX tags if you find them convenient.
Within
processRequest(), the first portion calls getTagAttr() repeatedly to establish the required
and optional attributes for the tag. If any required attributes are missing, an error message will be
shown; optional attributes will be assigned default value when not provided. The next few lines load
the JDBC driver class specified in the tag’s
driver attribute with Class.forName(), then connect
to the data source by supplying the
connect, username, and password attributes to getConnection(),
then create an object called
dbmd to represent the data source’s metadata structure.
Next, a series of
if and else if tests are used to call the appropriate DatabaseMetaData method
depending on the
action attribute that the tag is being called with. No matter which method ends
up being called (
getTables(), getColumns(), or the like), the result is always a ResultSet object
called
rs. The returnAsQuery() function is then called to return the data in the ResultSet to the
calling ColdFusion page.
The main task of
returnAsQuery() is to create the new CFML query object with the addQuery()
method. That method requires the new query’s column names to be specified as an array of strings,
so a few lines of code are required first to obtain the recordset’s column names using
getMetaData(),
getColumnCount(), and getColumnName() (all described in the Java SDK documentation). Once the
query has been created, it is a relatively simple task to loop through the
ResultSet, adding rows to
the CFML query and filling it with data from the
ResultSet along the way. See Table 30.6, earlier
in this chapter, for details about the
addQuery(), addRow(), and setData() methods used in this por-
tion of the listing.
NOTE
Please consult the Java SDK documentation for more information on the classes and methods used in this listing (except for the
ones specific to CFX development; those are detailed in the tables at the beginning of this chapter). Most of them are in the
j
ava.sql package.
Using the New Tag in ColdFusion
To use the new <CFX_DatabaseMetaData> tag in a ColdFusion page, you need to use javac to com-
pile Listing 30.1, then register the resulting DatabaseMetaData.class file with the ColdFusion
Administrator. These steps are described in the “Getting Started” section earlier in this chapter.
You can then use the tag in any CFML page, such as the simple example shown in Listing 30.4.
This listing creates a page that allows the user to select from the tables in the
ows.mdb database file
using a drop-down list. When the user selects a table, the names, types, and widths for each of the
table’s columns are displayed (Figure 30.5).
Kommentare zu diesen Handbüchern