Home Jdbc JDBC connection
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

JDBC connection

Advertisement
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.

JdbcConnection.java

 
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 {
            Class.forName(driver);
            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...");
            con.close();
            System.out.println("Connection is closed...");
        } catch (Exception e) {
            System.out.println(e);
        }
    }
} 

Output

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

Download code

Advertisement

Liked it!  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.

Posted on: November 17, 2008

Ask Questions?    Discuss: JDBC connection  

Post your Comment


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

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

aboutjdbc drivers
shyam
November 10, 2011
sql

why works sql server
shyam
November 10, 2011
visual studio

why works visul studio
sattur rameshgoud
December 16, 2011
doubt

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.
harikrishna
December 21, 2011
hari.kanuru@gmail

no comments
swapnali
January 4, 2012
JDBC

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

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?
narender
May 5, 2012
java

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