Creating A Data Source

I l @ ve RuBoard

Before you work with a database it might be helpful for you to understand exactly what a database is in terms of how a program like ColdFusion would understand it. A database is a container that stores information in tables. The tables are made up of fields; a collection of fields is a record. Each record contains a complete set of information. It could be a person's name , address, and phone number, for example; or it could be a listing of products. Either way, all the records in a table contain the same pieces of informationonly about different people or products.

A database is a must for just about any site that serves up dynamic content. When you register for services at a site, your information is stored in a database. Any time you search for a product on a Web site, you are accessing a database that contains that site's product catalog.

So how does a database communicate with the Web, making information available to you and other users? One way is by using ColdFusion. ColdFusion is able to "talk" to a database and retrieve information from it using a language called SQL (Structured Query Language). In order to do so, ColdFusion must know where the database is. ColdFusion uses data sources to know how, and where, to find a database. A data source points to the location of the database on your local hard drive. You can also create data sources that will map to a database on any available network drives . By creating a data source in the ColdFusion administrator, we are able to refer to a database by name and let ColdFusion do the dirty work of actually opening the connection. In a production environment, the database will reside on a server. It may be the same server as the Web server or it may be on a standalone database server. Either way, the database is visible to ColdFusion.

A data source is critical to ColdFusion. Without one, ColdFusion won't know where to find the database you want to use (unless you spell it out specifically in your code). Now you will create your own data source.

NOTE

You will be working with a Microsoft Access database throughout the course of this book. Although ColdFusion can work with almost any database program on the market, Microsoft Access is an excellent one to learn on. However, as a site grows, it can be severely hampered by using Microsoft Access, which is a single- user database. This can slow down a server tremendously. Because of this, almost all large sites run on enterprise-level databases such as Oracle or Microsoft SQL Server.


  1. On your local hard drive, create a folder called databases within the Inetpub folder.

    You are going to use this folder in the upcoming tasks .

  2. Move the Customers.mdb file to the Inetpub\databases folder on your local hard drive.

    Customers.mdb is located on your CD in the Lesson5\Start folder.

    Don't put the .mdb file in the wwwroot folder. On your local machine it won't make a difference, but in a production environment, it would mean trouble, since wwwroot is the public level of your server. There, users could access the database the same way they can access your main-screen Web page, and could make changes to your database.

  3. Open ColdFusion Administrator by clicking Start > Programs > Macromedia ColdFusion Server 5 > ColdFusion Administrator.

    ColdFusion data sources can only be created through the ColdFusion Administrator. Once the Administrator is launched, the login screen appears. The ColdFusion Administrator is accessed through a Web browser; when you launch the Administrator, it will launch your default browser.

  4. Enter your password and click Password.

    Your password is the one you created when you installed ColdFusion Server.

    Once you click Password, you see the main screen for ColdFusion Administrator.

  5. Select ODBC Data Sources from the left-hand navigation panel.

    Once you have selected ODBC Data Sources, the Datasources page opens, and you'll see a summary of all the data sources currently set. If you installed the sample applications, four data sources will appear: cfsnippets, cfx, CompanyInfo, and GlobalCorpDB.

    NOTE

    The sample applications can be accessed on the main page of the ColdFusion Administrator.

  6. Type products into the Data Source Name field.

    The label for the Data Source Name is actually below the box into which you are entering information. When you create your data source, products is the name you will be using, and the name that will be referenced in your ColdFusion code. Your database and data source names are the same, but be careful not to get the two confused . The database actually contains information, and the data source is just a pointer to the location of your database. You can name a data source anything, but I think it's better to give it a name that relates to your application. As you start to build more applications and add additional data sources, meaningful, standardized names will help keep things straight. That way, six months from now, you won't find yourself scratching your head, wondering what a particular data source is for.

    TIP

    One way to standardize your data source names (in Microsoft Access) is to give them the same names as their related .mdb files. This makes it easier to maintain and locate them. Naming your database so it relates to your application is also helpful; for example, if you are cataloging stars, you might name your database star_catalog.

  7. Make sure that the ODBC Driver field is set to Microsoft Access Driver (*.mdb) and click Add.

    NOTE

    You may notice that in the ODBC Driver field there is reference to .mdb files (*.mdb). This is the extension for Access databases.

    Once you have given the data source a name and specified what driver to use, the Create ODBC Data Source screen appears. Now you'll tell the ColdFusion server where to find the database that is associated with the data source you are creating.

  8. Click the Browse Server button, located next to the Database File field.

    Because this is the first time you are setting up a data source in ColdFusion, you need to install some additional components .

  9. Click Yes when you are prompted to install and run CFJava.cab.

    Once this has finished installing, the Browse Server screen will appear. You should see your local hard drive listed here.

  10. Find and select your products.mdb file and click Apply.

    Your database should be in C:\Inetpub\databases. Once you have found it and clicked Apply, you return to the Create ODBC Data Source page. The database file field contains the location of your customers.mdb file.

  11. Click Create.

    This is the final step in creating your data source. Once you have clicked Create, you are sent back to the initial ODBC Data Sources screen. If the data source is set up correctly, the status contains the value OK.

    If the data source was not set up properly, you would see the word Failed in red under the status field. This can happen if you specified the incorrect location or database, or if the database is corrupt.

    You are now ready to use ColdFusion to access information from the database.

I l @ ve RuBoard


Macromedia ColdFusion 5. Training from the Source
Macromedia ColdFusion 5 Training from the Source (With CD-ROM)
ISBN: 0201758474
EAN: 2147483647
Year: 2002
Pages: 99
Authors: Kevin Schmidt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net