Home Tutorial Java Jdbc JDBC DriverManager Example

 
 

Share on Google+Share on Google+
JDBC DriverManager Example
Posted on: October 16, 2010 at 12:00 AM
Advertisement
In this tutorial you will learn java.sql.DriverManager, And how to use it to create a connection to the database.

JDBC Driver Manager

DriverManager is a class of java.sql.*; package. Its main function is to manage the set of JDBC drivers. It is traditional management layer of JDBC   which works between user and Driver. Driver Manager keeps track of driver available and connection between database and driver. It maintains a list of driver classes that has registered them self by calling a method DriverManager.registerDriver. All the driver classes are written in static section, which creates an instance on class and register automatically with DriverManager automatically when it is loaded. Therefore user only call DriverManager, registerDriver is automatically called. You can register a driver manager in two ways

1. By calling the Class.forName("DriverName"); Which explicitly loads the river class and does not depends on external setup.

2.By loading the driver class to java.lang.System property jdbc.drivers, this is the list of driver class names which DriverManager loads. When the DriverManager class is initialized it looks for the driver name which user have entered and load that driver.

If the driver class is loaded and registered with DriverManager now it is ready for establishing connection. When DiverManager.getConnection("ConnectionURL"); is called the driver manager test each driver to establish a connection and finally establishes the connection with the appropriate driver.

Following is the example that loads the driver and establish a connection with MySql database.

Class.forName("com.mysql.jdbc.Driver");
onnection con=null;	
con=DriverManager.getConnection("jdbc:mysql://localhost/student");

All the Methods of DriverManager class are static

A Simple Example of DriverManager is given below

To run this example you must create a databse in MySql database named student and create table of name student as,

CREATE TABLE student (
RollNo int(9)  PRIMARY KEY NOT NULL,
Name tinytext NOT NULL,
Course varchar(25) NOT NULL,
Address text  );

Then Run The following code

DriverManagerClaz.java

package roseindia.net;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DriverManagerClaz {
	String driverName = "com.mysql.jdbc.Driver";
	Connection con = null;
	public DriverManagerClaz() {
		try {
			//Loading Driver for MySql
			Class.forName(driverName);
		} catch (ClassNotFoundException e) {
			System.out.println(e.toString());
		}
	}
	public Connection createConnection() {
		try {
			String connectionUrl="jdbc:mysql://localhost:3306/student";
			String userName="root";
			String userPass="root";
			con = DriverManager
					.getConnection(connectionUrl,userName,userPass);
			System.out.println("******* Connection created successfully........");
			} catch (SQLException e) {
			System.out.println(e.toString());
		}
		return con;
	}
	public void closeConnection(){
		try{
			this.con.close();
		System.out.println("******* Connection closed Successfully.........");
		}catch(Exception e){
			System.out.println(e.toString());
		}
	}
	public static void main(String args[]){
		DriverManagerClaz con=new DriverManagerClaz();
		con.createConnection();
		con.closeConnection();
	}
}

When you run this application it will display message as shown below:


******* Connection created successfully........
******* Connection closed Successfully.........

Download this example code

Advertisement

Related Tags for JDBC DriverManager Example:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 16, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com