• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 4. Working with Databases > Understanding ColdFusion Data Sources

Understanding ColdFusion Data Sources

For ColdFusion to interface with a database, you must create a ColdFusion data source. A data source is a connection to a database management system or database file. Just like a Windows data source, a ColdFusion data source represents a kind of ghost copy of the real database. Because ColdFusion runs as a Java application, it interfaces with a system database through Java Database Connectivity (JDBC) , a Java technology for standardizing access to databases.

JDBC uses database drivers to communicate directly with the database. JDBC drivers are specifically designed for a particular database system, so, for example, there is an Oracle driver and a Sybase driver. As Figure 4.3 shows, JDBC provides a level of abstraction that lets you work with multiple databases.

Figure 4.3. ColdFusion uses JDBC, a Java technology, to connect to different databases. JDBC drivers provide direct connections to a specific database type.

The Windows operating system uses Open Database Connectivity (ODBC) for database connections. Because Microsoft Access is a Windows-based application, it uses ODBC as well. If you are running ColdFusion on Windows, you must register the data sources in Windows first before you can use them in ColdFusion. For example, to open the Data Source Administrator in Windows 2000, select the Start menu, Settings, Control Panel, Administrative Tools, (ODBC) Data Sources.

Figure 4.4 shows the ODBC Data Source Administrator.

Figure 4.4. In Windows, the ODBC Data Source Administrator lets you review, modify, and create new ODBC data sources. Notice that when you installed ColdFusion, three ODBC data sources were created: CFSnippets, ExampleApps (used in this book), and CompanyInfo.

You might be wondering how ColdFusion, which uses JDBC, works with an ODBC data source like Microsoft Access. ColdFusion contains a database driver named ODBC Socket. The ODBC Socket database driver lets JDBC interface with ODBC to simulate a JDBC connection.

Creating a ColdFusion Data Source

To work with ColdFusion data sources, you use the Data Sources page in the ColdFusion Administrator. You can open the ColdFusion Administrator in Windows by selecting Start, Programs, ColdFusion MX, Administrator. Once you log into the Administrator, click the Data Sources link in the left navigation bar.

In the Data Sources page, shown in Figure 4.5, you see the registered data sources and the driver used for each. To see the details of a data source, click on the Edit icon. This book uses the exampleapps data source, so click on its Edit icon. In the ODBC Socket page that appears, as shown in Figure 4.6, you see the details of the data source, such as the data source name, the database driver, and a description.

Figure 4.5. The ColdFusion Administrator’s Data Sources page contains the available data sources and the capability to create new data sources. Create new data source Registered data sources

Figure 4.6. The ODBC Socket page lets you see the details of an ODBC Socket data source, such as the ExampleApps data source. Click the Show Advanced Settings button to reveal additional settings.

Although this book uses the ExampleApps data source, the following procedure creates a new data source for the cfexamples.mdb file:

In the Data Sources page of the ColdFusion Administrator, enter a name for the data source, such as myDataSource, in the Data Source Name text box. The name that you enter here will be the name used in Dreamweaver and your CFML code.

In the Driver menu, select a database driver. Because you are using the cfexamples.mdb file (Microsoft Access), select the Microsoft Access driver. Notice that the ODBC socket driver was not used. The Microsoft Access driver in the Data Sources page is a JDBC driver, which provides better performance and stability than the ODBC socket driver.

Click the Add button.

In the Microsoft Access page that appears, as shown in Figure 4.7, click the Browse Server button to the right of the Database File text box. In the Browse Server page, shown in Figure 4.8, browse to the cfexamples.mdb file in the CFusionMX\db directory. Click the Apply button.

Figure 4.7. The Microsoft Access page lets you select the database file as well as enter a description for the data source. If you need additional options, such as security login information, click the Advanced Settings button.

Figure 4.8. The Browse Server page lets you navigate your file system to find the database file. After you click the Apply button, the file path appears in the Database File text box.

Make sure the Use Default Username check box is selected, and enter a short description in the Description text box.

Click the Submit button.

Duplicate Data Sources

Avoid creating duplicate data sources for the same database. Duplicate data sources will likely cause confusion or mistakes later.

In the Connected Data Sources table on the Data Sources page, shown in Figure 4.9, you can do more than just see the available data sources. Using the Edit, Verify, and Delete icons in the Actions columns, you can manage your data sources easily. When you verify a data source to make sure it is functioning properly, OK appears in the Status column.

Figure 4.9. The Databases panel of the Application panel group lets you see the registered ColdFusion data sources, their tables, and their table columns.

You can also see ColdFusion data sources in the Databases panel of Dreamweaver MX. As shown in Figure 4.9, the Databases panel displays the same list of databases as the Data Sources page in the ColdFusion Administrator. You can also expand and collapse the databases to reveal tables and columns.

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint