Getting a Database Connection – Database Connectivity

Getting a Database Connection

To create a connection to a Derby database, the following code can be emulated:

Click here to view code image

String jdbcURL  = “jdbc:derby:localhost:1521:musicDB”;
String username = “joe”;
String password = “welcome1”;
try (Connection connection = DriverManager.getConnection(jdbcURL,
                                                         username, password)) {
  /* use the connection. */
} catch (SQLException e) {
  e.printStackTrace();
}

A portion of the URL that indicates a driver instructs the DriverManager to load the appropriate driver implementation to memory. This of course would fail if this JDBC driver is not found in the class path or module path.

Connecting to the musicDB Database

We will use the musicDB database which consists of the compositions table shown in Table 24.2. Instructions for downloading the files for the musicDB database and the Derby JAR files can be found on this book’s website. The structure of the working directory is assumed to be the following:

Click here to view code image

Working directory
 ?
 ??? db-derby-10.15.2.0-lib/  <==
Directory with the Apache Derby distribution
 ?   ?
 ?   ??? lib/                 <==
Directory with the Derby JAR files
 ?
 ??? musicDB/                 <==
Directory with the
musicDB
database files
 ?
 ??? dbDemo/                  <==
Package with examples used in this chapter
 ?   ?
 ?   ??? JDBCConnection.java
 ?   ?
… …

Example 24.1 shows how to obtain a connection to the musicDB database. Note that in the JDBC URL, the relative path of the musicDB directory in the working directory is musicDB. The class path must also be set to access the Derby JAR files. In the code below, the implicit finally clause of the try-with-resources statement guarantees to close the connection when done.

Example 24.1 Connecting to the musicDB Database

Click here to view code image

package dbDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCConnection {
  public static void main(String[] args) {
    final String jdbcURL = “jdbc:derby:musicDB”;
    try (Connection connection = DriverManager.getConnection(jdbcURL)) {
      /* use the connection. */
      System.out.println(connection);
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
}

Probable output from the program:

Click here to view code image

org.apache.derby.impl.jdbc.EmbedConnection@1201454821 (XID = 1301),
(SESSIONID = 1), (DATABASE = musicDB), (DRDAID = null)

Legacy JDBC Driver Management

Prior to JDBC version 4.0, drivers had to be loaded into memory explicitly, before using them to establish database connections. There are several ways in which this could be achieved:

  • Instantiate and register the database-specific JDBC driver class using the java.sql.DriverManager.registerDriver() method:

Click here to view code image

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

  • Load a database-specific JDBC driver class using the java.lang.Class.forName() method:

Click here to view code image

try {
  Class.forName(“oracle.jdbc.driver.OracleDriver”);
} catch (ClassNotfoundException c) { }

  • Load a database-specific JDBC driver class using the command-line -D option:

Click here to view code image

java –Djdbc.drivers=oracle.jdbc.driver.OracleDriver …

From JDBC 4.0 onward, JDBC drivers are automatically loaded, and no explicit driver loading or registration is required. However, old-style code would still function for backward compatibility reasons.


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>