18 CHAPTER 30 Extending ColdFusion with CFX
Listing 30.3 shows how these objects can be used together to create the <CFX_DatabaseMetaData>
tag. This is a relatively long listing, but it you look at it part by part you’ll see that the code is really
quite simple and straightforward. To a large extent, you will find examples in the Java SDK docu-
mentation that contain lines quite similar to many of the lines in this listing.
Listing 30.3 DatabaseMetaData.java—Using Functionality from the java.sql Package in a CFX Tag
import com.allaire.cfx.* ;
import java.sql.* ;
public class DatabaseMetaData implements CustomTag
{
// Constant string for error messages
final String msgError = “Error occurred in a <CFX_DATABASEMETADATA> tag. “;
// This gets called each time the tag is used in a ColdFusion page
public void processRequest( Request request, Response response )
throws Exception
{
// Obtain the values for the tag’s attributes, and throw exceptions
// if any of the required attributes have not been provided.
String strAction = getTagAttr(“ACTION”, request);
String userName = getTagAttr(“USERNAME”, request);
String password = getTagAttr(“PASSWORD”, request);
String driver = getTagAttr(“DRIVER”, request);
String connect = getTagAttr(“CONNECT”, request);
String strQueryName = getTagAttr(“NAME”, request);
// These are optional attributes
String DBCatalog = getTagAttr(“DBCATALOG”, request, null);
String DBSchema = getTagAttr(“DBSCHEMA”, request, null);
String DBTableName = getTagAttr(“DBTABLENAME”, request, “%”);
String DBColumnName = getTagAttr(“DBCOLUMNNAME”, request, “%”);
String DBProcedureName = getTagAttr(“DBPROCEDURENAME”, request, “%”);
// For the DBTableName attribute, consider an empty string to mean null
//if (DBTableName.equals(“”)) DBTableName = null;
// Load the specified database driver
Class.forName(driver).newInstance();
// Attempt to connect to the data source
Connection conn = DriverManager.getConnection(connect, userName, password);
// Get the metadata object from the database connection
java.sql.DatabaseMetaData dbmd = conn.getMetaData();
// This ResultSet will be returned to ColdFusion as a query object
ResultSet rs = null;
// Handle ACTION=”GetTables” (user wants a list of tables)
if ( strAction.equalsIgnoreCase(“GetTables”) ) {
String[] types = {“TABLE”};
rs = dbmd.getTables(DBCatalog, DBSchema, DBTableName, types);
// Handle ACTION=”GetViews” (user wants a list of views)
Komentarze do niniejszej Instrukcji