Accessibility

ColdFusion Article

 

Displaying database data on a web page


Table of Contents

Connecting to a database

ColdFusion can connect to a variety of data sources, including the following, among others:

  • Relational database
  • Text file
  • Microsoft Excel spreadsheet

In this example, we will discuss how to connect to a Microsoft Access database. While you should not use Access in a production environment because it is not a relational database server, it is sufficient for this tutorial. You can connect to Access databases the same way as any other relational database: by creating a connection in the ColdFusion Administrator.

Launch the ColdFusion Administrator (when browsing on the local machine) by placing the following URL in your browser: http://localhost/cfide/administrator/index.cfm.

Note: If you are browsing from another machine, replace "localhost" in the URL with the server name of the machine where ColdFusion Server is running. Also, if you are using the embedded web server that ships with ColdFusion, you should append a :8500 port number to the end of "localhost" or the server name.

The ColdFusion Administrator has a menu grouping for Data & Services on the main, left menu. There are several types of connections you can make to a relational database: JDBC, ODBC, OLE DB, and Native. This article focuses on connecting to JDBC databases.

Because ColdFusion runs on top of a Java server, the connections to data sources are created using JDBC—an industry standard for connecting to many different data sources from Java-based applications. Using a JDBC connection, ColdFusion can send SQL statements to the database, and receive result sets from it (see Figure 3).

Open Database Connectivity

Figure 2. Open database connectivity

When you choose Datasources from the ColdFusion Administrator, you can add a data connection to any data source:

  1. Click Datasources under Data & Services in the main menu. This opens the Data Sources page. Assign a name to the data source and choose the database type to connect to—in this case, Microsoft Access (see Figure 4).

    Figure 3. Choosing ODBC data sources

  2. Click Add.
  3. You will see a set of connection parameters for the Microsoft Access data source you are creating (see Figure 5).

    Figure 4. Connection parameters for a Microsoft Access data source

    To keep things simple, set the following options:

    • CF Data Source Name: The name you use within your ColdFusion code to access this database
    • Description: Text description
    • Database File: Click the Browse button to find the Access database file (MDB) on your local machine
  4. Once you have entered the data source information, click Create. The data source list will appear with a status of OK.

If you cannot connect to your data source successfully, refer to the ColdFusion Support Center: Database troubleshooting page.