Share on Google+Share on Google+

JDBC connection

In this Tutorial we want to describe you a code that helps you in understanding a Jdbc Connection.

JDBC connection


The JDBC connection implements a connection with database in front end application. The JDBC includes execution of SQL queries return a result set over that specified connection.

 Understand with Example

In last Tutorial we illustrates you JDBC url connection. The current Tutorial helps you to understand JDBC connection. The code explains you   how creating and closing of connection is done.

For this program code we have a class Jdbc Connection, We have a list of method required to connect and close the connection. Before the implementation of class we need to import a package java.sql.* needed for network interface between url and back end.

   Loading a driver is done by calling a class.forName ( ) and accept the driver class as argument.

   DriverManager.getConnection  ( ) :This method returns you an connection object.DriverManager.getConnection built a connection with database.

Using this method we establish a connection between url and database. Once the connection is built,println print the connection is created

   con.close ( ) : An object of connection is used to close the connection between database and url.

Finally the println print the connection is closed.In case there is an exception in try block, during the establishment of connection between the front end in java application(url) and backend. The catch block is responsible for handling the exception.

import java.sql.*;
public class JdbcConnection {
    public static void main(String args[]) {
        Connection con = null;
        String url = "jdbc:mysql://localhost:3306/";
        String db = "komal";
        String driver = "com.mysql.jdbc.Driver";
        String user = "root";
        String pass = "root";
        try {
            con = DriverManager.getConnection(url + db, user, pass);
            System.out.println("jdbc driver for mysql : " + driver);
            System.out.println("Connection url : " + url + db);
            System.out.println("Connection is created...");
            System.out.println("Connection is closed...");
        } catch (Exception e) {


Connection is created...
Connection is closed...

Download code



Posted on: November 17, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.

Discuss: JDBC connection  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
asif mohammed
October 29, 2011
jdbc connectionpool

what is connectionpool ? how to use usedefind methods
shyam yadav
November 10, 2011

aboutjdbc drivers
November 10, 2011

why works sql server
November 10, 2011
visual studio

why works visul studio
sattur rameshgoud
December 16, 2011

I have one doubt regarding jdbc connections,i.e in MVC where we use the jdbc connections. (i.e in Model layer,or view layer, or control layer,) please clarify this question.
December 21, 2011

no comments
January 4, 2012

I made the data base,written program how to connect that data base through JDBC
Akshay Singh
February 7, 2012

u r coding is not working in my project... can u give me any sample project of database connectivity to java. i want to update data from java net beans an the new data must save in the database ...i also want to retrieve data from database to the java programme. Can u help me?
May 5, 2012

what was the class path setting of jdbc connection of type1 driver and type 4 Driver