JDBC - What is DriverManager class in java


In this tutorial we will learn What is DriverManager class in java.

  • DriverManager class is found in java.sql package in java.
  • java.sql.DriverManager is the basic service for managing set of JDBC drivers in java.
  • DriverManager helps in establishing connection between driver and database in java.
  • java.sql.DriverManager class allows user to customize the JDBC Drivers used in the application by because the java.sql.DriverManager class attempts to load the driver classes referenced in the 'jdbc.drivers' system property.
  • When getConnection method is called, the DriverManager attempts to locate suitable driver from amongst those which were loaded at initialization and those which were loaded explicitly using the same classloader as the current applet or application in java.
  • Important methods of DriverManager class in java JDBC>
    • DriverManager.getConnection = establish connection with the given database URL.
    • DriverManager.registerDriver - It registers the given driver with the DriverManager.
    • DriverManager.deregisterDriver - It deRegisters the given driver with the DriverManager.


DriverManager class - Example/ Full program to connect to Oracle database using Type 4 driver - java JDBC program >

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DriverManagerClassExample {
   public static void main(String... arg) {
         
          Connection con =null;
          try {
                 // registering Oracle driver class
                 Class.forName("oracle.jdbc.driver.OracleDriver");
                 // getting connection
                 con = DriverManager.getConnection(
                              "jdbc:oracle:thin:@localhost:1521:orcl",
                              "ankit", "Oracle123");
                 // Test connection is null or not
                 if (con != null)
                       System.out.println("Connection established successfully!");
                 else
                       System.out.println("No Connection!");
          } catch (ClassNotFoundException e) {
                 e.printStackTrace();
          } catch (SQLException e) {
                 e.printStackTrace();
          }
          finally{
                 try {
                       if(con!=null) con.close(); // close connection
                 } catch (SQLException e) {
                       e.printStackTrace();
                 }
          }
   }
}
/*OUTPUT
Connection established successfully!
*/



So, In this tutorial we learned about what is java.sql.DriverManager class in java.

Read program and example on usage of java.sql.DriverManager classes for following databases in java>

JDBC connection with Oracle 11g


JDBC connection with MySql database



JDBC connection with MsSql (Microsoft) database


JDBC connection with PostgreSQL database



RELATED LINKS>

Labels: Core Java JDBC
eEdit
Must read for you :