To get connected to any database, it is general to use a driver, which is specific for a vendor. Java Data Base Connectivity provides four types of Drivers, popularly called JDBC Drivers for connectivity. they are:
Type 1: JDBC-ODBC Driver---
This is used for small scale database connectivity which makes use of a JDBC-ODBC bridge driver to get connected to the database. The client system must contain an installed ODBC driver for this purpose, which is done in the following steps.
1. Open Control Panel
2. Open Administrative Tools (present in the Category View)
3. Open Data sources (ODBC)
4. Click the Tab User Dsn and click Add
5. Look for "Microsoft ODBC for Oracle" and the give the Name
6. Click OK and you are done!
Type 2: Native API partly Java Technology Enabled Driver--
This type of driver converts JDBC calls into calls on the client API for Oracle, Sybase, Informix, DB2, or other DBMS. Note that, like the bridge driver, this style of driver requires that some binary code be loaded on each client machine.
Type 3: Pure Java Drive for Data Base Middle ware--
This style of driver translates JDBC calls into the middle ware vendor's protocol, which is then translated to a DBMS protocol by a middle ware server. The middle ware provides connectivity to many different databases.
Type 4: Java to Database Pure Java Driver---
This is also called a light weight driver, this can be used to directly connect a client side system to a remote server system. a light weight driver and can be used free of cost. This style of driver converts JDBC calls into the network protocol used directly by DBMS allowing a direct call from the client machine to the DBMS server and providing a practical solution for intranet access.
The interfaces and classes in the JDBC are in the java.sql package.
Out of all the interfaces present in the package we mostly come across three major interfaces,
- Connection
- Statement
- ResultSet
Statement: java.sql.Satement, this interface is used to write the sql statements in through the java programming language. and get them executed.
ResultSet: java.sql.ResultSet, this interface is used to store the resultant rows, it acts like a buffer that can be retrieved row wise.
Let me Demonstrate with the following Example----
import java.sql.*; //To use the interfaces and classes of JDBC we use this statement
public class DataBaseDemo{
public static void main(String[] args)throws Exception{
Connection con=null;
Statement st=null; //The variables of the types are initially assigned to null
ResultSet rs=null;
try{
// The interfaces raise an exception SQLException, for which we use try-catch block to handle them.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// This statement calls the type of driver to be used.
con= DriverManager.getConnection("jdbc:odbc:dsn","scott","tiger");
// Here we get the connection to the database using the JDBC-ODBC driver
st = con.createStatement();
// An environment for sql statements is created.
rs = st.executeQuery("SELECT * FROM student");
// Here the method executes the Query, select * from student and returns the rows to the Resultset variable, rs which acts like an implicit cursor to work with.
while(rs.next()){
System.out.print(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));
// here the individual columns are represented by the getter methods for the respective data types.
}
}
catch(SQLException e){
System.out.print(e.getMessage());
}
finally{
rs.close();
st.close();
con.close();
}
}
}
No comments:
Post a Comment