Tuesday, March 16, 2010

Java DataBase Connectivity JDBC

JDBC Basics

Types of JDBC Drivers
Type 1:- Bridge drivers. Example JDBC-ODBC Bridge
Type 2:- Native API drivers, partially in Java.
Type 3:-Middleware is involved in communication from client's request and data source.
Type 4:-The client connects directly to DataSource, pure Java drivers.

Establishing the Connection
Two ways:-
1) DriverManager class
2) DataSource interface

Using DriverManager class for making connection
Load the driver:-   Class.forName("oracle.jdbc.driver.OracleDriver");
Define Connection URL:-  String url = "jdbc:oracle:thin:@" + hostName + ":" + portName + ":" + dbName;
Establish Connection:- Connection conn =DriverManager.getConnection(url,username,password);

Using DataSource interface for making connection
InitialContext initContext = new InitialContext();
DataSource ds = initContext.lookup("java:comp/env/jdbc/ownDataBase");
Connection con = ds.getConnection();

The above method uses JNDI lookup to get the datasource.

Using Datasource for getting JDBC connection is preferred over DriverManager class.
 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.