Exercise 3: Configuring a database connection

One of the most commonly used and most powerful features of ColdFusion is the ability to connect to and manipulate data from a wide variety of databases. Like most ColdFusion developers, you will probably use this capability often. To be able to connect to a database, you must create a a data source in ColdFusion. A data source contains the information that ColdFusion needs to be able to recognize and communicate with a database.

ColdFusion MX 7 includes a Dreamweaver extension that lets you create a data source without having to leave the Dreamweaver environment. If you are not using Dreamweaver, you use the ColdFusion Administrator to create data sources; seeConfiguring a data source in the Administrator for more information.

Using Dreamweaver to configure a data source

To create a ColdFusion data source without leaving the Dreamweaver environment, you need the following information:

To configure a database connection in Dreamweaver:

  1. Ensure that the site that contains the tutorial files is the current site.
  2. Select Create New > ColdFusion.
  3. On the Databases tab, click the + button.
  4. Select Microsoft Access Connection.
  5. In the CF Data Source Name text box, enter CompassTravel.
  6. Specify the Database File as c:\CFusionMX7\wwwroot\cfdocs\getting_started\db\CompassTravel.mdb.
  7. Select the Use Default Username check box.
  8. When prompted for the password for this site, enter the ColdFusion Administrator password you specified when you installed ColdFusion MX 7.
  9. Click OK.
  10. Continue with Exercise 4: Configuring debugging options.

Configuring a data source in the Administrator

You can use the ColdFusion MX 7 Administrator to configure a data source. If you are running ColdFusion in a UNIX environment, you must use the ColdFusion MX 7 Administrator to connect to the PointBase database in the data source.

To access the ColdFusion MX 7 Administrator:

Note: If you are accessing the ColdFusion MX Administrator from a remote client, you must replace localhost with the host name or IP address of the computer where ColdFusion MX is installed.

The following sections describe how to establish a connection to the sample tutorial database file and how to enable optional debugging settings.

Configuring the connection to the sample database file

The following procedures describe how to configure a connection to the sample database file (CompassTravel) using the ColdFusion MX Administrator. Prior to building the sample application, you must configure the Compass Travel database connection.

Perform one of the following procedures. The Microsoft Access procedure is for Windows users. The PointBase procedure is for UNIX and Macintosh OS X users.

To define the connection to the sample Microsoft Access database:

  1. In the ColdFusion MX Administrator, select Data & Services > Data Sources.
  2. In the Data Source Name box, enter CompassTravel.
  3. In the Driver list, select Microsoft Access with Unicode.
  4. Click Add.
  5. Click Browse Server and navigate to the cf_root/cfdocs/getting_started/db/CompassTravel.mdb file.
  6. Click Apply.
  7. Click Show Advanced Settings and ensure that the settings for CLOB and BLOB are enabled (checked).
  8. Click Submit to complete the data source configuration.

    The ColdFusion MX Administrator verifies the data source connection and the name CompassTravel appears in the Connected Data Sources table.

  9. Ensure that OK appears in the Status column for CompassTravel.

    If the connection to the CompassTravel data source fails, do the following:

    1. Verify that the name of the data source file does not contain a space. If it does contain a space, delete the data source from the Connected Data Source dialog box. To do this, click the Delete action button associated with the CompassTravel data source name, and then repeat the steps in this procedure to reconfigure this data source.
    2. Verify that the path specified for the Compass Travel database file is correct.

To define the sample PointBase database file:

  1. In the ColdFusion MX Administrator, select Data & Services > Data Sources.

    The Add New Data Source dialog box appears.

  2. Specify the following:

    Field

    Action

    Data Source name text box

    Specify the name CompassTravel.

    Note: Ensure that the name of the data source file does not contain any spaces. If the name contains a space, the data source connection fails.

    Driver drop-down selection box

    Select Other.

  3. Click Add to configure the data source name and driver.

    The Data Source dialog box appears.

  4. Specify the following:

    Field

    Action

    JDBC URL

    Enter the following JDBC URL for the Compass Travel PointBase files:

    jdbc:pointbase:compasstravel,database.home=/<home location>/wwwroot/cfdocs/getting_started/db

    The following is the default home location for stand-alone ColdFusion web server configurations:

    /opt/coldfusionmx7/wwwroot/cfdocs/getting_started/db

    Driver Class

    Enter the following driver class:

    com.pointbase.jdbc.jdbcUniversalDriver

    Driver Name

    Specify PointBase.

    Username

    Specify PBPUBLIC.

    Password

    Specify PBPUBLIC.

    Description

    Enter the following:

    Database file for Compass Travel tutorial

  5. Click Show Advanced Settings to make the following settings:

    Field

    Action

    Maintain Connection

    Disabled (clear)

    CLOB

    Enabled (checked)

    BLOB

    Enabled (checked)

  6. Click Submit to complete the data source configuration.

    The name CompassTravel appears in the Connected Data Sources dialog box.

  7. Click Verify All Connections to ensure that ColdFusion can access this file.

    OK appears in the Status column for successful connections.

    If the connection to the compass travel data source fails, do the following:

    1. Verify that the name of the data source file does not contain a space. If it does contain a space, delete the data source from the Connected Data Source dialog box. To do this, click the Delete action button associated with the CompassTravel data source name, and then repeat the steps in this procedure to reconfigure this data source.
    2. Verify that the JDBC URL is correct.