Connecting to the Database

After the driver has loaded and registered itself with the DriverManager, the ListEmployees class can attempt to connect to the database server, as shown in the following code fragment:

String url = "jdbc:edb://localhost:5444/edb";
String user = "enterprisedb";
String password = "enterprisedb";
Connection con = DriverManager.getConnection(url, user, password);

All JDBC connections start with the DriverManager. The DriverManager class offers a static method called getConnection() that is responsible for creating a connection to the database. When you call the getConnection() method, the DriverManager must decide which JDBC driver to use to connect to the database; that decision is based on a URL (Uniform Resource Locator) that you pass to getConnection().

A JDBC URL takes the following general format:

jdbc:<driver>:<connection parameters>

The first component in a JDBC URL is always jdbc. When using the Advanced Server JDBC Connector, the second component (the driver) is edb.

The Advanced Server JDBC URL takes one of the following forms:

jdbc:edb:<database>

jdbc:edb://<host>/<database>

jdbc:edb://<host>:<port>/<database>

The following table shows the various connection parameters:

Table - Connection Parameters

Name Description
host The host name of the server. Defaults to localhost.
port The port number the server is listening on. Defaults to the Advanced Server standard port number (5444).
database The database name.