JDBC DriverManager Connection .

JDBC Connection to a database

As mentioned earlier, there are two main ways to go in creating a connection to a database:
  1. You can download and register a JDBC database vendor driver and then use a method with the url, username and password arguments in a DriverManager class to create a connection to a database.
  2. Or you can get a connection via a DataSource, that is already registered as a resource in a Connection pool. Connection pools are pre-registered in a web server with the necessary arguments (vendor driver, url, user, password etc.) for a database connection.
    You get a DataSources by doing a JNDI lookup from a web server java context.

This is about using DriverManager connection.

If your application is outside a server this is the way to go. The procedure to achieve a such connection is then:
  1. Loading a Driver

    • First you need to include vendor specific driver classes for your database, which exist (normally) in a jar file(s) that you can download from the vendor web-site.
    • There are two ways to load the driver classes:
      1. You can use the static java.lang.Class.forName() method.
        try {
         java.lang.Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
         e.printStackTrace();
        }
      2. You can use the System Property parameter when you run the application.
        >java –Djdbc.drivers=com.mysql.jdbc.Driver JDBCClient
        
    • In both cases we are loading a MySQL database driver, com.mysql.jdbc.Driver.
  2. Create Connection with java.sql.DriverManager class


    The java.sql.DriverManager class has three static database connection methods that returns an object with a java.sql.Connection interface access.
    1. Connection getConnection(String url)
    2. Connection getConnection(String url,String username,String password)
    3. Connection getConnection(String url,Properties properties)
    1. In this case the url is the connection string to a database and will be on the form:
      jdbc:<subprotocol>:<subname>//<host>:<port>/
      [databasename][,user=xxx,password=zzz]
      Element Description
      jdbc: This is a Keyword for a JDBC protocol connection.
      subprotocol and subname This is a vendor specific protocol notation for the database.
      host The host address where we can find the database.
      port The port used to access the database.
      database The name of the database to use
    2. As a second alternative, you can pass the user name and password for the connection to the database as arguments to the connection method.
    3. As a final alternative, you can create a Properties object that you can provide a user name and password, and other properties that may be of a database vendor-specific information (database name if not in the url). You then use the Properties object as the second argument with the connection method.
    Example of a JDBC Connection using a DriverManager:
    import java.sql.Connection;
    import java.sql.DatabaseMetaData;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class JDBCClient {
    
     public static void main(String[] args) {
      Connection conn = null;
      Statement stmt = null;
      try {
       // Register a driver for the MySQL database
       Class.forName("com.mysql.jdbc.Driver");
       // Create a url for accessing the MySQL
       // on local host, port 3306 and database CarDB
       String url = "jdbc:mysql://localhost:3306/CarDB";
       // user and passord to access the database
       String username = "root";
       String password = "root";
       // User the DriverManager to get a Connection to the database
       conn = DriverManager.getConnection(url, username, password);
       DatabaseMetaData dbmd = conn.getMetaData();
       System.out.println("db name is " + dbmd.getDatabaseProductName());
      } catch (ClassNotFoundException ex) {
       ex.printStackTrace();
      } catch (SQLException e) {
       e.printStackTrace();
      } finally {
       try {
        conn.close();
       } catch (Exception xe) {
        xe.printStackTrace();
       }
      }
     }
    }

    You should use the close() method in the Connection object when you do not need it anymore.

    The result of this is:
    db name is :MySQL

    You can download this example here (needed tools can be found in the right menu on this page).

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.