Information in a database can be made available to users for searching and updating. Several Web publishing applications are available for Windows NT. You can use these applications to publish databases on the Web in just a few minutes. In this chapter, you learn how you can use Cold Fusion and the Internet Database Connector to publish databases. Note that Internet Information Server (IIS) is required to publish databases on the Web with the Internet Database Connector.
Cold Fusion is a sophisticated database publishing application.
Using it, you can effortlessly publish various Open Database Connectivity
(ODBC) compliant databases on the Web. In this section, you learn
how to install Cold Fusion and use it to publish databases. The
purpose of this section is to provide an overview of how you can
use Cold Fusion to create Web/database interfaces. Refer to the
Cold Fusion documentation to learn about various advanced capabilities
not discussed in this section. I guide you through the installation
of Cold Fusion and then show you how you can use it to update
and query ODBC data sources. Cold Fusion supports several Web
servers, including all four Web servers covered in this book.
Visit the following Web site for the most up-to-date information
about Cold Fusion.
URL |
Cold Fusion (Web database tool) home page: http://www.allaire.com/ |
You can download Cold Fusion from the following Web site. After
you do so, copy it to a temporary directory and decompress the
distribution file.
URL |
Cold Fusion download Web page: http://www.allaire.com/cgi-shl/dbml.exe?template=/allaire/downloads/ |
Then locate the file setup.exe, and execute it to begin installing Cold Fusion. The first installation dialog box displays a software license agreement. Click the OK button to acknowledge it. The next dialog box recommends that you exit all Windows NT applications before installing Cold Fusion. I recommend that you follow this advice. The Cold Fusion installation program is unable to copy certain DLL files if they are being used by other applications. Acknowledge the recommendation and proceed to the next dialog box. In the Choose Destination Directory dialog box shown in Figure 18.1, you can select the destination directory of Cold Fusion. This directory should not be a child of the Web server's document root directory due to security reasons. Click Next and proceed to the dialog box in Figure 18.2.
Figure 18.1: Cold Fusion destination directory selection dialog box.
Figure 18.2: Cold Fusion component selection dialog box.
In the Select Components dialog box, you select which Cold Fusion components should be installed by the Cold Fusion installation program. I recommend that you make sure that all components except the ODBC component are selected. If the version of ODBC installed on your system is the same as the one shown in the dialog box in Figure 18.2, you can skip installing the ODBC Desktop drivers. If you do not have ODBC installed on your system, however, or if the version of ODBC installed is older than the one shown in Figure 18.2, select to install the ODBC component. After you select the Cold Fusion components, click Next and proceed to the next dialog box to select a Web server.
The dialog box shown in Figure 18.3 lists various Web servers supported by Cold Fusion. In this dialog box, you can select the Web server you want to use with Cold Fusion. If your Web server is not listed, select the Other Server radio button. Click the Next button to proceed to the next dialog box.
Figure 18.3: Cold Fusion Web server selection dialog box.
Use the input controls shown in the dialog box in Figure 18.4 to specify various Web server settings. For Cold Fusion to be installed properly, it needs the path of your Web server's document root directory, CGI directory, and the alias of the CGI directory. Cold Fusion attempts to locate these settings for you. If the settings are correct, click the Next button. If they are not, change the settings, verify them with your Web server's configuration settings, and click the Next button.
Figure 18.4: Web server settings dialog box.
If you selected to install ODBC drivers in the Select Components dialog box (refer to Figure 18.2), the installation program displays the dialog box shown in Figure 18.5. Before you install the ODBC drivers, make sure that no other applications are using various shared DLL files that will be replaced by the ODBC installation program. If the ODBC installation program fails to install because other applications are still using various shared DLL files it should replace, reboot your system and execute the ODBC Setup icon shown in Figure 18.6.
Figure 18.5: Quit other applications before attempting to install ODBC drivers.
Figure 18.6: Cold Fusion Windows NT Start Menu folder.
After the installation program installs Cold Fusion, it creates a Windows NT Start Menu folder similar to the one shown in Figure 18.6. After this Start Menu folder is created, Cold Fusion is completely installed and ready for use.
The Cold Fusion Setup dialog box, shown in Figure 18.7, gives you the chance to verify that the Cold Fusion installation is working properly. I recommend that you select the Yes button to verify the Cold Fusion installation. You can also verify it at a later time by executing the Getting Started icon in the Start Menu folder (refer to Figure 18.6).
Figure 18.7: Cold Fusion installation complete dialog box.
After you install Cold Fusion, you should verify that it is installed properly before using it. Use the Getting Started with Cold Fusion Web page (see Figure 18.8) to verify correct installation. You can invoke this Web page either by answering Yes to the Cold Fusion Setup dialog box (refer to Figure 18.7) or by executing the Getting Started icon in the Start Menu folder (refer to Figure 18.6).
To verify successful installation of Cold Fusion, select the Verify Installation and Configuration link on the Web page. You then see a Web page identical to the one shown in Figure 18.9. Use this Web page to submit a query and verify that Cold Fusion has been set up properly. Use the drop-down list to select a department and then click the Submit Query button.
Figure 18.9: The Cold Fusion Test Query Web page.
After you click on the Submit Query button, if Cold Fusion is installed properly, you see a Web page similar to the one shown in Figure 18.10. If you do not see a similar Web page, chances are, ODBC is not set up properly on your system. When you install the ODBC drivers, if you encounter any error messages, you might have to reboot your system and reinstall the ODBC drivers included with Cold Fusion.
Figure 18.10: The Test Query results Web page.
Inserting data into an ODBC data source with Cold Fusion is easy. In this section, you learn how you can insert information in a form into the table of an ODBC-compliant data source. The following example is meant to provide you with an overview of some of Cold Fusion's capabilities. Refer to the Cold Fusion documentation for additional information about inserting data into ODBC data sources.
Cold Fusion is made up of a CGI executable program (DBML.EXE). Data in ODBC data sources is manipulated with Cold Fusion using Database Markup Language (DBML) template files. Cold Fusion functions by manipulating data in ODBC data sources as predefined in a template file. The Cold Fusion executable program (DBML.EXE) is called with a template containing information about how data in an ODBC data source should be manipulated. For example, the URL dbml.exe?template=/cfpro/tutorial/step2b.dbm calls Cold Fusion with the template step2b.dbm. Cold Fusion then accesses an ODBC data source as specified in the template file and returns the results.
After you install Cold Fusion, if you look up the URL http://127.0.0.1/cfpro/tutorial/step1a.htm, you see a Web page similar to the one shown in Figure 18.11. Note that 127.0.0.1 is the address of the local host. Your Web server has to be functioning to access the URL given earlier. The source code of this Web page is shown in Listing 18.1 (line numbers have been added to make it easier to refer to various statements). In line 16, notice that the contents of the form are submitted to Cold Fusion along with the name of a template. Cold Fusion uses this template to determine what to do with the data submitted. The Web page containing the form merely collects information and forwards it to Cold Fusion. Note that names of input controls in the following form correspond with various data fields in the database.
Figure 18.11: Inserting data into an ODBC data source.
Listing 18.1. The Web page source code.
1: <HTML> 2: <HEAD> 3: <TITLE>Lesson 1 - Inserting Data</TITLE> 4: 5: 6: </HEAD> 7: 8: <BODY> 9: <H1><a href="/cfpro/tutorial/tutorial.htm"> <img src="../cfusion.gif" height=72 width=59 border=0 align=left></a> Lesson 1 - Inserting Data </H1> 10: <HR> 11: 12: <P>Displayed below is a standard HTML Form. 13: If you enter data into the form and press the "Enter Course Information" button the 14: data will be automatically appended to the <I>CourseList</I> table of the database: 15: 16: <FORM ACTION="/scripts/dbml.exe?Template=/cfpro/tutorial/step1b.dbm" METHOD="POST"> 17: 18: <PRE> 19: Course Name: <INPUT TYPE="text" NAME="CourseName" SIZE=38> 20: Department: <SELECT NAME="Department_ID"> 21: <OPTION VALUE="BIOL"> Biology 22: <OPTION VALUE="CHEM"> Chemistry 23: <OPTION VALUE="ECON"> Economics 24: <OPTION VALUE="MATH"> Mathematics 25: </SELECT> Level: <SELECT NAME="CourseLevel"> 26: <OPTION VALUE="Basic"> Basic 27: <OPTION VALUE="Intermediate"> Intermediate 28: <OPTION VALUE="Advanced"> Advanced 29: </SELECT> 30: Course No: <INPUT TYPE="text" NAME="CourseNumber" SIZE=3> 31: 32: Course Description: 33: <TEXTAREA COLS=48 ROWS=3 NAME="CourseDescription"></TEXTAREA> 34: 35: <INPUT TYPE="submit" VALUE="Enter Course Information"> 36: </PRE> 37: </FORM>
The contents of the template referred to in line 16 of Listing
18.1 are shown in Listing 18.2. Cold Fusion uses this template
to determine what to do with the data contents of the form. Cold
Fusion uses line 3 of this code listing to determine the data
source and the table where the contents of the form should be
inserted. In this particular example, the data is inserted into
the table CourseList in data
source CF Examples. After
the data is inserted, Cold Fusion
displays the HTML statements shown in lines 8-29 of Listing 18.2.
Refer to the Cold Fusion documentation for information about various
statements that you can insert into the template files to manipulate
data submitted with a form.
Listing 18.2. Inserting data into an ODBC data source table.
1: <!-- Insert the data into the table 'CourseList' -> 2: 3: <DBINSERT DATASOURCE="CF Examples" TABLENAME="CourseList"> 4: 5: <!-- Show the user a brief message indicating that the 6: insert was successful --> 7: 8: <HTML> 9: <HEAD> 10: <TITLE>Lesson 1 - Inserting Data</TITLE> 11: </HEAD> 12: 13: <BODY> 14: 15: <H2>New Course Entered into Database</H2> 16: 17: <P>You just entered a new course into the <I>CourseList</I> table. 18: The fields CourseName, Department_ID, CourseLevel, CourseNumber 19: and CourseDescription were populated by data you entered in the form. 20: 21: <P> 22: 23: <A HREF="/cfpro/tutorial/step1a.htm"> 24: Go Back to Lesson 1 - Inserting Data</A> 25: <P> 26: <HR> 27: 28: </BODY> 29: </HTML>
After the data in Figure 18.11 is inserted into the ODBC data source, Cold Fusion displays a message similar to the one shown in Figure 18.12.
Figure 18.12: The message displayed after data is successfully inserted into an ODBC data source.
Viewing data with Cold Fusion is as easy as inserting data. After you insert data using the form shown in Figure 18.11, enter the URL http://127.0.0.1/scripts/dbml.exe?template=/cfpro/tutorial/step2b.dbm to obtain the contents of the database, as shown in Figure 18.13. Note how the data inserted into the form has been inserted into the database. The information in Figure 18.13 was created with the template (tep2b.dbm) defined in the URL just mentioned.
Figure 18.13: Contents of ODBC data source after inserting the record in Figure 18.11.
Refer to Listing 18.3 for the contents of this template file (line numbers have been added to make it easier to refer to various statements). The first line of the template file uses a Structured Query Language (SQL) statement to obtain data from the data source CF Examples. The statements in lines 12-14 display the contents of the table.
Listing 18.3. Displaying contents of an ODBC data source.
1: <DBQUERY NAME="GetAllCourses" DATASOURCE="CF Examples" 2: SQL="SELECT * FROM CourseList" > 3: 4: <HTML><HEAD> 5: <TITLE>Step2 - Retrieving Data (template)</TITLE> 6: </HEAD> 7: 8: <BODY> 9: 10: <B>AVAILABLE COURSES</B> 11: 12: <DBOUTPUT QUERY="GetAllCourses"> 13: #Department_ID#-#CourseNumber# #CourseName# (#CourseLevel#) 14: </DBOUTPUT> 15: 16: </BODY></HTML>
In the preceding sections, you learned how you can use Cold Fusion to update and query information in an ODBC data source. Refer to the Cold Fusion documentation for additional information about how you can use it to publish databases on the Internet. Cold Fusion also ships with many sample applications that demonstrate various capabilities of Cold Fusion and how you can use it to provide dynamic content to users browsing a Web site. Go over these examples by executing the Getting Started icon in the Start Menu folder.
Using the Internet Database Connector (IDC), you can publish databases on the Web. IDC is a .DLL file (HTTPODBC.DLL) that is included with Internet Information Server (IIS). In this section, you find out how you can publish a database by using the IDC.
To create a Web interface to a database, you have to create two files. The first file is called the Internet Database Connector file and ends with an .IDC extension. This file contains information for accessing an ODBC data source and executing an SQL statement. After the SQL statement is executed, the HTML file defined in the IDC file is used to format the results of the SQL statement. The other file is called the HTML extension file and ends with an .HTX extension.
In the following sections, you learn how you can use IIS and IDC to create a guest book and interface it with the Web. Creating a guest book is a three-stage process composed of the following steps:
You can create a data source by using any application that has an ODBC driver installed. The guest book in this exercise uses a Microsoft Access database. Before you proceed any further, run Microsoft Access Setup and make sure that the Microsoft Access ODBC driver is installed on your system.
Then open Microsoft Access and create a table similar to the one shown in Figure 18.14. It does not have to be a complicated table. Simply create an Access database that you can use to store guest book entries. The database can have fields for the user's name, e-mail address, and comments. After you create the table, make a note of its name and save it as a Microsoft Access file. Also be sure to make a note of the location of the Access file and names of various data fields.
Figure 18.14: Creating the guest book data source by using Microsoft Access.
After you save the Access file, invoke the Control Panel and locate the ODBC icon, as shown in Figure 18.15. If the ODBC icon is not visible in the Control Panel, no ODBC drivers are installed on your system. In that case, run either Microsoft Access or Office Setup, and select to install the Microsoft Access ODBC driver.
Figure 18.15: Locate the ODBC icon in the Control Panel.
After you locate the ODBC icon, execute it to open the Data Sources dialog box shown in Figure 18.16. In this dialog box, you create System DSNs for databases with ODBC drivers. Click on the System DSN button to create a System DSN for the Microsoft Access database created earlier. Note that IDC works only with System DSNs.
Figure 18.16: The Data Sources dialog box.
After you select the System DSN button, a dialog box similar to the one shown in Figure 18.17 appears. In the System Data Sources dialog box, click the Add button to create a System DSN for the Microsoft Access database. After a System DSN is created for the Microsoft Access guest book, it will be visible in this dialog box (look ahead to Figure 18.20).
Figure 18.17: The System Data Sources dialog box.
After you click the Add button, the Add Data Source dialog box appears, as shown in Figure 18.18. Use this dialog box to select the Microsoft Access ODBC driver and then click the OK button. If the Microsoft Access ODBC driver is not visible, reboot your system and reinstall the Microsoft Access ODBC driver.
Figure 18.18: Selecting an ODBC driver for a new system DSN.
Using the dialog box shown in Figure 18.19, you can create a System DSN by specifying a data source name and a description for it. Then click the Select button to select an ODBC database that will become an alias for the System DSN. Use this dialog box to select the Microsoft Access database created earlier. Then click the OK button to create a System DSN for the Microsoft Access guest book.
Figure 18.19: Creating a System DSN for the Microsoft Access guest book.
Next, verify that a System DSN has been created for the Microsoft Access guest book application. You can do so by invoking the Data Sources dialog box (refer to Figure 18.16) and clicking the System DSN button. If a System DSN was successfully created for the Microsoft Access guest book, you see it in the window of the dialog box, as shown in Figure 18.20.
Figure 18.20: Verifying creation of a System DSN for the Microsoft Access guest book.
This completes the information you need for creating a System DSN for a database. You are not done yet, however; you can follow the preceding steps to create various database applications and interface them with the Web using the IDC.
You need to create three files to insert data from a form to a System DSN. Listing 18.4 contains the HTML code used to gather data for the Microsoft Access guest book System DSN. Pay particular attention to line 17, which refers to an .IDC file. IIS maps .IDC files to a .DLL file (HTTPODBC.DLL) that is essentially the Internet Database Connector. Refer to Figure 18.21 for an example of how you can use the HTML code in Listing 18.4 to submit a guest book entry.
Figure 18.21: The form used to gather data for a Microsoft Access guest book.
Listing 18.4. Guest book using Internet Database Connector.
1: <html> 2: <title>Guestbook - Using Internet Database Connector</title> 3: <BODY BGCOLOR="FFFFFF"> 4: <TABLE> 5: <TR> 6: 7: <h2> 8: Guestbook - Using Internet Database Connector 9: </h2> 10: 11: <HR> 12: Please enter the following information and press the SUBMIT button<BR> 13: <HR> 14: 15: <P> 16: 17: <form action="/scripts/samples/GuestBookInsert.idc" method=get> 18: 19: <B>Please enter your name </B> 20: <input type="text" name="Name" value="" size=35 maxlength=40> 21: <P> 22: 23: <B>Please enter your e-mail address</B> 24: <input type="text" name="EMail" value="" size=45 maxlength=50> 25: <P> 26: 27: <B>Please enter your comments below</B> 28: <textarea name="Comments" cols=70 rows=4 ></textarea> 29: <P> 30: 31: <input type="submit" value="SUBMIT Guest Book Entry"> 32: <input type="reset" value="Reset Form"> 33: 34: </form> 35: <P> 36: 37: <HR> 38: <a HREF="/scripts/samples/GuestBookView.idc?">View Guest Book</a> 39: <HR> 40: 41: </body> 42: </html>
Recall that line 17 of Listing 18.4 contains the statement action="/scripts/samples/GuestBookInsert.idc". The contents of the file GuestBookInsert.idc are shown in Listing 18.5. Note how line 1 of this code listing refers to the guest book System DSN you created in the dialog box shown in Figure 18.19. It also declares Name and Email to be required parameters in line 4. These parameters prevent a user from submitting a guest book entry that does not contain a name or an e-mail address. Lines 6-8 contain the SQL statement that inserts data submitted by the form shown in Figure 18.21 to the Microsoft Access guest book System DSN. Line 6 specifies the table of the guest book database to which the data should be inserted. Finally, lines 7 and 8 insert the three form fields to their corresponding table fields.
Listing 18.5. Inserting data to an ODBC data source.
1: Datasource: GuestBook 2: Username: sa 3: Template: GuestBookInsert.htx 4: RequiredParameters: Name, EMail 5: SQLStatement: 6: + INSERT INTO GuestBookEntries 7: + (Name, Email, Comments) 8: + VALUES('%Name%', '%Email%', '%Comments%');
Refer to line 3 of Listing 18.5; it defines the file GuestBookInsert.htx as a template. This file is used to format the message returned to the user after the form is submitted. The message in Figure 18.22 was created with Listing 18.6. Note how the message is personalized to include the user's name in line 10. The expression <%idc.[Field Name]%> can be used to echo information filled in by the user. [Field Name] refers to the name of a form input control.
Listing 18.6 Internet Database Connector HTML Extensions file
(GuestBookInsert.htx).
1: <html> 2: <title>Thanks for the guest book entry!</title> 3: <BODY BGCOLOR="FFFFFF"> 4: 5: <h1>Guest Book entry added to Microsoft Access database.</h1> 6: 7: <HR> 8: 9: <h2> 10: Hello <%idc.Name%>, 11: </h2> 12: 13: Thanks very much for visiting this Web site and signing the guest 14: book. We hope you visit this Web site again, soon. 15: 16: </body> 17: </html>
You can use the URL http://your.server.com/scripts/samples/GuestBookView.idc? to obtain the contents of the Microsoft Access guest book database, assuming that the two files GuestBookView.idc and GuestBookView.htx are stored in the /scripts/samples/ directory. The contents of the GuestBookView.idc file are shown in Listing 18.7. The contents of this file are similar to that of GuestBookInsert.idc. The only difference is the fact that Listing 18.7 selects data from the GuestBook System DSN instead of inserting it.
Listing 18.7. Selecting data from an ODBC data source.
1: Datasource: GuestBook 2: Username: sa 3: Expires: 2 4: Template: GuestBookView.htx 5: SQLStatement: 6: +SELECT Name, EMail, Comments 7: +FROM GuestBookEntries
The contents of the file GuestBookView.htx are shown in Listing 18.8. (Also see Figure 18.23.) This file formats results of the SQL statements in lines 5-7 of Listing 18.7. Pay particular attention to lines 9-15. The two keywords <%begindetail%> and <%enddetail%> surround HTML code that formats data from an SQL statement consisting of multiple records in response to an SQL query. The HTML code defined between the two tags <%begindetail%> and <%enddetail%> format and merge each record returned by the SQL query.
Figure 18.23: Contents of guest book.
Listing 18.8. The contents of GuestBookView.htx.
1: <html> 2: <title>Contents of Guest Book</title> 3: <BODY BGCOLOR="FFFFFF"> 4: 5: <h1>Contents of Guest Book</h1> 6: 7: <HR><P> 8: 9: <%begindetail%> 10: <B>Name: </B><%Name%><BR> 11: <B>EMail: </B><%EMail%><BR> 12: <B>Comments: </B><%Comments%><BR> 13: <p> 14: <HR><P> 15: <%enddetail%> 16: 17: <hr> 18: <a href="/samples/gbook/GuestBookInsert.htm">Sign the Guest Book</a> 19: <hr> 20: 21: </body> 22: </html>
Figure 18.24 displays contents of the Microsoft Access guest book database after two records are inserted to it using the HTML form shown in Figure 18.21. Note how the two records shown in Figure 18.23 are stored in the Microsoft Access database.
Figure 18.24: Contents of the Microsoft Access guest book database.
Using Cold Fusion and IDC, you can effortlessly publish databases on the Internet. IDC is included free of charge with IIS and can be used to create Web interfaces to ODBC data sources. Cold Fusion works with several Windows NT Web servers, including IIS, and offers many powerful features that you can use to create sophisticated Web or database applications.
In this chapter, you learned how you can use Cold Fusion to insert data to an ODBC database. You also learned how you can view data inserted into an ODBC database by using Cold Fusion. In the final sections, you found out how you can use the Internet Database Connector to set up a guest book.
Java is a powerful programming language that you can use to create applications for the WWW. Java is an ideal programming language for the Internet because you can use it to create applications that are platform neutral and secure. Using Java applets, you can make Web pages interactive and more interesting to browse. In the next chapter, you learn how you can develop and add Java applets to a Web site.