JDBC  «Prev  Next»
Lesson 3 JDBC URL formats
ObjectiveWrite JDBC URLs.

JDBC URL Connection

From what you have learned in past modules, you may be wondering how to direct JDBC to the database driver. The JDBC URL is the code that programmatically identifies the database driver. In principle, the JDBC URL works much like the URLs you use to get to your favorite Web sites, except that you are pointing to a database.
The JDBC URL is how the driver recognizes the data source and establishes a connection.
In it, you specify the JDBC URL, starting with the syntax jdbc: to identify the JDBC URL. Next, you add the sub-protocol (i.e., odbc if you are using the JDBC-ODBC bridge, or cloudscape if you are using the Cloudscape database in later lessons) and the name of the data source or database system.
For example, if you are connecting to a Cloudscape travel, the URL becomes:

jdbc:cloudscape:c:\database\travel

Three parts of a JDBC URL.

The following diagram illustrates the three parts of a JDBC URL.
JDBC URL Components
JDBC URL Components

JDBC URL format

Example

Since our project uses Cloudscape, the URL's protocol and sub-protocol are:

jdbc:cloudscape:

The subname of a Cloudscape URL is the <path of base directory><fileSep>databaseName, where <fileSep> is the appropriate file separator for your computer (/ for UNIX and \ for Windows). An example for UNIX is:

/data/databases/myDatabase

URLs for other database drivers will be different. You have to consult the vendor-supplied documentation to discover the exact format expected by the driver you are using. You can use a JDBC URL to obtain a connection to a database as shown in this statement:

Connection con = DriverManager.getConnnection(
url, "myLogin", "myPassword");

getConnection()method

When you use the getConnection()method, you will often include a login ID and password as the second and third parameters. The arguments are passed to the database, setting the access rights associated with that user's ID. However, these arguments are optional; whether or not they are required depends on the driver. If they are not supplied, the driver and database usually provide a default identity and default set of access rights. In the case of Cloudscape, the default rights are sufficient for the examples you will create, and so you will not need to supply a login ID and password to create a connection. You will have to consult the driver documentation for its precise requirements.
The JDBC URL, while not complicated, must be exact in order to make the database connection.
You do not have to determine the driver's JDBC protocol when building the URL. The driver writer defines the protocol. Refer to your driver documentation for its URL format. In the next lesson, you will learn how to use the JDBC Driver Manager to connect to the DBMS.

JDBC URL Format - Exercise

Click the Exercise link below to test your knowledge of writing a JDBC URL.
JDBC URL Format - Exercise