Establishing a Database Connection – Database Connectivity

24.3 Establishing a Database Connection

Interaction between the application and the database is in the context of a connection. Database connections are represented by the java.sql.Connection interface. JDBC drivers provide database-specific implementations of this interface.

The class java.sql.DriverManager provides the overloaded factory method get-Connection() that initializes and returns a database Connection object.

Click here to view code image

static Connection getConnection(String jdbcURL)
static Connection getConnection(String jdbcURL, String username,
                                 String password)
static Connection getConnection(String jdbcURL, Properties info)

All versions of the getConnection() method require a JDBC URL and all throw a SQLException. Additional information may be required to establish a database connection, such as username and password, or other means of authentication such as digital signatures that can be set using the Properties object (§18.2, p. 1100). Note also that the Connection object returned is AutoCloseable, and therefore it is best handled in a try-with-resources construct to ensure that the connection is closed when done.

JDBC URL

The general syntax of the JDBC URL is as follows:

Click here to view code image

protocol
:
provider
:
driver_type
:
database_specific_connection_details

The protocol is always specified as “jdbc”. Other details may vary depending on the database provider. For example, to connect to the Apache Derby database you need to specify the provider as “derby”, followed by the description of where this database is located, which would include host, port, and database name information.

Click here to view code image

jdbc:derby:localhost:1521:musicDB

The example below shows a more sophisticated JDBC driver that may provide different connectivity mechanisms, each identified by an appropriate subprotocol. In this case, the JDBC URL example shows how one can specify an Oracle database connection using the most frequently used thin protocol implementation that provides enhanced security features.

Click here to view code image

jdbc:oracle:thin:@localhost:1521:musicDB

Not all JDBC drivers provide alternative connectivity methods. In the Oracle JDBC driver case, alternative connectivity mechanisms, such as thin, OCI, or kprb, can be used. This could be the case with many other providers of JDBC drivers. In the case of a Derby database, it can be accessed as a separate process or be embedded inside a Java program, which would make the JDBC URL look different. It is best to refer to JDBC driver-specific documentation to identify available choices and the rationale behind selecting one or the other connectivity mechanism.

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>