JDBC connection

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


 

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

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

Advertisements

Share on Google+Share on Google+

JDBC connection

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

Advertisements

 

Discuss: JDBC connection  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:9
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
[email protected]

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

Ads

 

Ads