Coldfusion Tutorials



Spry Tutorials

Air Tutorials


Database Basics

In the Database Basics tutorial we were introduced to some basics on databases and created two tables that demonstrated, graphically, how two tables are 'joined' together. In this tutorial we will be taking the sample database that is shipped with Microsoft Access (Northwind.mdb) and creating a connection to it in the Coldfusion Administrator panel.

Why do we need to make a connection?

We make a connection in the Coldfusion Administrator in order to be able to gain access to a database from Coldfusion code. We give each database we connect to a unique name in the Administrator and then use that name in our code to perform database commands.

Coldfusion Administrator

The first thing we need to do is log into the Administrator. Depending on how you installed coldfusion the URL will be http://127.0.0.1/CFIDE/Administrator or http://127.0.0.1:8500/CFIDE/Administrator for the built-in webserver intallation. Enter in your password and then on the left navigation click on datasources. You should see something similar to the image below.

Image 1:
Coldfusion Administrator datasource view



Adding a Datasource Name

The next thing we are going to do is enter the datasource name and select the type of database we are going to connect to.

Image 2:
Making a connection
Once we have added the datasource name and selected Microsoft Access click the add button.



Adding the database path

I previously took a copy of the sample database and put it in a directory on my computer. The database does not need to be in a web accessible directory in order to work, and for security purposes shouldn't be.
On my computer I put the copy in c:\projects\tutorials\
Once we have added the path we just need to click submit button and we should be good to go. For other database types the form may look drastically different and will require different information.

Image 3:
Making a connection

 


Verifying the Connection

After we click the submit button we will be taken back to the datasource listing. If we entered everything correctly we should see the following: verified connection

If you see a error message such as the one below failed connection click the edit link to make sure that you entered the path correctly, alternatively you could click the Browse Server button (image 3 above) and browse for the database instead of entering the path by hand. Once you have verified that all the information is correct re-submit and you should see the OK message.

 



Conclusion

In this tutorial we learned how to set up a datasource name in the Coldfusion Administrator. With this datasource name we can now write queries using SQL and the cfquery tag to create, read, update and delete information (and even create new tables) in the database.