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

Chapter 31. Building a Basic JSP Site > Setting Up a Database Connection

Setting Up a Database Connection

In JSP terms, the database connection is a script that calls on the driver to talk to the database. Dreamweaver creates this script for you, and stores it in a special connections file, when you choose data source name (DSN) from the Databases panel. Because this information gets stored in a special file that can be accessed by any JSP page in your site, you have to define the connection only once for the entire site. (And if you update the information later—for instance, when moving the site to a different server—you have to update it only once.)

Exercise 31.2a Creating a Database Connection (JDBC-ODBC Bridge Driver)

In this exercise, you create the connection script that will allow your pages to communicate with the antiques database. You must already have installed your database and created a driver for it (as outlined in the preceding section) before continuing with this exercise. Only do this exercise if you want to use the JDBC-ODBC bridge driver for your database. If you want to use the mm.mysql JDBC driver, do exercise 31.2b instead.

Because Dreamweaver has to know what kind of connection to create, you must have a dynamic document open before you can create the connection. From your local site, open catalog.jsp.

Take a look at the page in Code view, and you’ll see that so far it contains only standard HTML code.

From the Application panel group, open the Databases panel. If you have catalog.jsp open, the panel will have a plus (+) button at the top. Click the plus (+) button and, from the pop-up menu, choose Sun JDBC-ODBC Driver (ODBC Database). Figure 31.14 shows this happening.

Figure 31.14. Choosing a JDBC-ODBC bridge driver for a database that already has a DSN.

The JDBC-ODBC Driver dialog box will appear. For your connection’s name, enter antiques_conn. (The connection name will be used in the connection script. It’s common practice to include con or conn in the name.)

The driver field should already be filled in as sun.jdbc.odbc.JdbcOdbcDriver. The URL is partially filled in, with placeholder text for the name of the DSN.
Jdbc:odbc:[odbc dsn]

To complete this entry, replace the placeholder text (and the brackets) with the name of your DSN (whatever name you gave the DSN in the ODBC Drivers Control Panel), like this (new code in bold):

What you’re doing here is creating a bridge driver that points to the existing ODBC driver (the DSN).

Figure 31.15 shows this happening.

Figure 31.15. Completing the JDBC-ODBC Driver dialog box by entering the DSN that already exists for the antiques database.

If you specified a name and password when you defined the driver, enter them here. Otherwise, you can leave these fields blank.

To make sure Dreamweaver can connect to the driver, select the radio button for Using Driver on This Machine.

Before leaving the Data Source Name dialog box, click the Test button. If Dreamweaver can find the driver, you’ll get a Connection Successful message. The most common reasons for failing the test are incorrect names and passwords, and incorrectly entered DSNs. After you’ve passed the test, click OK to close the dialog box.


If you have NEO installed on your computer, when you try to connect a prompt will appear asking if you want to allow NEO to map the virtual directory for you. Choose No or there will be a conflict.

The Databases panel will now contain an icon representing your connection. (Congratulations!) You can now use this panel to explore your database. Expand the connection icon to see Tables, Views, and Stored Procedures. The antiques database contains only tables. Expand the Tables icon all the way to see that the database contains two tables—stockitems and customers—and to see what columns (information fields) each table contains. You can’t see the records stored in the database from here, but you can examine its structure (see Figure 31.16).

Figure 31.16. The Databases panel showing the structure of the antiques database.

In the Site panel, examine your local root folder. You’ll see a new Connections folder. Inside that folder is the antique_conn.jsp file. That file contains your connection script. Each file in your site that needs to access the database will link to it. Open antique_conn.jsp and examine it in Code view, and you’ll see the connection script:
// FileName="sun_jdbc_odbc_conn.htm" 
// Type="JDBC" "" 
// DesigntimeType="JDBC" 
// HTTP="false" 
// Catalog="" 
// Schema="" 
String MM_antique_conn_DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver"; 
String MM_antique_conn_USERNAME = ""; 
String MM_antique_conn_PASSWORD = ""; 
String MM_antique_conn_STRING = "jdbc:odbc:AntiquesBarn"; 

You don’t need to know what everything in this code means. However, one important piece of syntax that you should get familiar with is the <%...%> tags. All JSP code is contained within these tags. Whatever code is inside the tags must be valid Java. When the application server processes this page, it looks for these tags and executes all code inside them. All other code on the page is assumed to be regular HTML or client-side scripting, and is just passed back to the browser.



Not a subscriber?

Start A Free Trial

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