Jdbc Get Connection

Ads
 

Jdbc Get Connection

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

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

Ads

Tutorials   

Ads