Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jdbc Jdbc Get Connection

Related Tutorials


 
 

Share on Google+Share on Google+

Jdbc Get Connection

Advertisement
In this Tutorial we want to describe you a code that help you in understand JDBC GetConnection.

JDBC Get Connection

     

The JDBC Get Connection is the mean of establishing a connection between url and database. Whenever get Connection method is called, The Driver Manager search a suitable driver from loaded at initialization. 

Understand with Example

The Tutorial help you to understand a code from JDBC Get Connection. The code describe you connection status between url and database. For this we have a class Jdbc Get Connection. The class include loading a driver by calling class.forName ( ),which accept com.mysql.jdbc.Driver as parameter. The Driver Manager class call a get Connection ( ) method ,which return you a connection object. The con.close ( ) close the connection between url and database. In the first  println print false for  the connection is closed because con.close is called afterward the first println statement. The second println return  true for the connection is closed,As connection object calls close ( ) is called before it

In case there is an exception in the code ,The catch block caught and handle the exception.

JdbcGetConnection.java

import java.sql.*;

public class JdbcGetConnection {

public static void main(String args[]) {

Connection con = null;
Statement st = null;
ResultSet rs = null;

try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/komal", "root", "root");

System.out.println("Connection is closed : " + con.isClosed());
con.close();
System.out.println("Connection is closed : " + con.isClosed());

} catch (Exception e) {
System.out.println(e);
}
}
}

Output

Connection is closed  : false
Connection is closed  : true

Download code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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

Related Tutorials

Discuss: Jdbc Get Connection  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com