3.2. Database Connections

The easiest way to connect to a database is to use the connect( ) method defined in the class oracle.sqlj.runtime.Oracle. Recall from Chapter 1 that this is one of the classes that you must import into your SQLJ program.

3.2.1. The connect( ) Method

The connect( ) method accepts three parameters: a database username, a password, and a database URL. The syntax of a call to the connect( ) method is:

Oracle.connect(URL, username, password);

The syntax elements are as follows:

URL

Identifies the database that you want to connect to, along with the driver you wish to use to make the connection. See Section 3.2.2,for details on this parameter.

username

Specifies your database username.

password

Specifies the password for your username.

This example shows the connect( ) method being used to connect to a database:

Oracle.connect(
  "jdbc:oracle:thin:@localhost:1521:orcl",
  "fundamental_user",
  "fundamental_password"
);

In all cases, the connection to a database is made through Oracle Net8 (or higher).

3.2.2. Database URL Structure

The database URL tells SQLJ where your database is located. The structure of the database URL is dependent on the brand of JDBC driver being used. In the case of Oracle's JDBC drivers, the URL structure is:

driver_name:@driver_specific_information

The syntax elements are as follows:

driver_name

Specifies the name of the Oracle JDBC driver that you want to use. This may be any one of the following:

jdbc:oracle:thin

Get Java Programming with Oracle SQLJ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.