Saturday, 3 January 2015

What is DriverManager class and Connection interface in JDBC

DriverManager class:

The DriverManager class acts as an interface between user and drivers.It keeps track of the drivers that are available and handles establishing a connection between a database and the appropriate driver.The DriverManager class maintains a list of Driver classes that have registered themselves by
calling the method DriverManager.registerDriver().

Methods of DriverManager class:

>1. public static void registerDriver(Driver driver): It is used to register the given driver with DriverManager.
>2. public static void deregisterDriver(Driver driver): It is used to deregister the given driver (drop the driver from the list) with DriverManager.
>3. public static Connection getConnection(String url): It is used to establish the connection with the specified url.
>4. public static Connection getConnection(String url,String userName,String password): It is used to establish the connection with the specified url, username and password.

Connection interface:

A Connection is the session between java application and database. The Connection interface is a factory of Statement, PreparedStatement, and DatabaseMetaData i.e. object of Connection can be used to get the object of Statement and DatabaseMetaData. The Connection interface provide many methods for transaction management like commit(), rollback() etc.

Note: By default, connection commits the changes after executing the queries.

Methods of Connection interface:

>1. public Statement createStatement(): creates a statement object that can be used to execute SQL queries.
>2. public Statement createStatement(int resultSetType,int resultSetConcurrency): Creates a Statement object that will generate ResultSet objects with the given type and concurrency.
>3. public void setAutoCommit(boolean status): is used to set the commit status. By default it is true.
>4. public void commit(): saves the changes made since the previous commit/rollback permanent.
>5. public void rollback(): Drops all changes made since the previous commit/rollback.
>6. public void close(): closes the connection and releases a JDBC resources immediately.

2 comments :

  1. This comment has been removed by the author.

    ReplyDelete
  2. perfect explanation about java programming .its very useful.thanks for your valuable information.top 10 java training institutes in chennai | chennai's no.1 java training in chennai

    ReplyDelete