Unit - Iii (Working With Database) What Is ODBC?
Unit - Iii (Working With Database) What Is ODBC?
What is ODBC?
What is JDBC?
JDBC stands for Java Database Connectivity, which is a standard Java API for database-independent
connectivity between the Java programming language and a wide range of databases.
The JDBC library includes APIs for each of the tasks mentioned below that are commonly associated
with database usage.
JDBC Architecture
The JDBC API supports both two-tier and three-tier processing models for database access but in
general, JDBC Architecture consists of two layers −
The JDBC API uses a driver manager and database-specific drivers to provide transparent
connectivity to heterogeneous databases.
The JDBC driver manager ensures that the correct driver is used to access each data source. The
driver manager is capable of supporting multiple concurrent drivers connected to multiple
heterogeneous databases.
1
2
Following is the architectural diagram, which shows the location of the driver manager with respect to
the JDBC drivers and the Java application −
DriverManager: This class manages a list of database drivers. Matches connection requests
from the java application with the proper database driver using communication sub protocol.
The first driver that recognizes a certain subprotocol under JDBC will be used to establish a
database Connection.
Driver: This interface handles the communications with the database server. You will interact
directly with Driver objects very rarely. Instead, you use DriverManager objects, which
manages objects of this type. It also abstracts the details associated with working with Driver
objects.
Connection: This interface with all methods for contacting a database. The connection object
represents communication context, i.e., all communication with database is through
connection object only.
Statement: You use objects created from this interface to submit the SQL statements to the
database. Some derived interfaces accept parameters in addition to executing stored
procedures.
2
3
ResultSet: These objects hold data retrieved from a database after you execute an SQL query
using Statement objects. It acts as an iterator to allow you to move through its data.
SQLException: This class handles any errors that occur in a database application.
For example, using JDBC drivers enable you to open database connections and to interact with it by
sending SQL or database commands then receiving results with Java.
TYPE-2 Driver This driver I partly java and partly Native Code
Driver
3
4
The TYPE-1 driver acts as a bridge between JDBC and ODBC. An example of this driver is the SUN
JDBC-ODBC driver implementation. Java application os programmed using JDBC API and will use
JDBC_ODBC bridge to get access to database server.
Advantages :
Single Driver implementation can be used to interact with different data stores.
This I vendor independent driver.
Disadvantages:
4
5
Type-2 driver converts JDBC call into DB vendor specific native call. The application is built by using
JDBC API ,but JDBC call are converted to database specific native library calls
Advantages:
Disadnatage:
Native libraries must be installed on client machine since the conversion of JDBC calls to
native calls is done on the client machine.
It may increase cost of the application if we want to install all vendors native libraries .
5
6
The type-3 driver translates jdbc calls into middleware server specific calls ,and then converted to
database server specific calls by middleware server.
Advantages:
Disadvantages:
Compared to Type-2 driver,Type-3 drivers are slow due to the increased number of network
calls.
Type-3 drivers are costlier compared to the other drivers.
6
7
The Type-4 driver is a pure java driver, which implements the database protocol to interact directly
with a database. This type of drivers does not require any native database libraries to retrieve the
records from the database. This type of drivers translates JDBC calls into database specific network
calls. This type of drivers is light weight known as thin driver.
This type of drivers are pure java drivers and hence auto downloadable.
No native libraries are required to be installed.
Secure to use since it uses database specific protocols.
Disadvantages:
7
8
class Db1
{
public static void main(String args[])
{
Connection con=null;
Statement stmt=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:data4","system","pru");
stmt=con.createStatement();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
9
10
class Db2
{
public static void main(String args[])
{
Connection con=null;
Statement stmt=null;
String mid="123",mname="raghu";
int sal=20000;
try
{
10
11
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:data4","system","pru");
stmt=con.createStatement();
System.out.println("inserted successfully");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
11
12
class PS1
{
public static void main(String args[])
{
Connection con=null;
String mid="7676",mname="girish";
PreparedStatement ps;
int sal=40000;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:data4","system","admin");
String q="insert into manager values(?,?,?)";
ps=con.prepareStatement(q);
ps.setString(1,mid);
ps.setString(2,mname);
ps.setInt(3,sal);
ps.executeUpdate();
System.out.println("inserted successfully");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class PS2
{
public static void main(String args[])
{
Connection con=null;
ResultSet rs=null;
PreparedStatement ps;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
12
13
con=DriverManager.getConnection("jdbc:odbc:data4","system","pru");
String q="select * from manager ";
ps=con.prepareStatement(q);
rs=ps.executeQuery();
while(rs.next())
{
System.out.println(rs.getString("id")+"
"+rs.getString("name")+" "+rs.getInt("salary"));
System.out.println("\n");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
13
14
import java.io.*;
import java.sql.*;
class Db3
{
public static void main(String args[])
{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:data4","system","pru");
stmt=con.createStatement();
14
15
System.out.println(rs.getString("id")+"
"+rs.getString("name")+" "+rs.getInt("salary"));
System.out.println("\n");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
15
16
Servlets are the components of java that are used to create dynamic web
applications.Servlets can run on any java enabled platform.Java Servlets are usually
designed to process HTTP requets.
Advantages of servlets:
Servlet Disadvantage :
3. You need a Java Runtime Environment on the server to run servlets. CGI is a
completely language independent protocol, so you can write CGIs in whatever
languages you have available (including Java if you want to).
Packages:
Javax.servlet
Javax.servlet.http
Interface RequestDispatcher
Interface Servlet
Interface ServletConfig
Interface ServletContext
Interface ServletRequest
17
18
Interface ServletResponse
Class GenericServlet
Class ServletException
Interface HttpSession
Interface HttpServletRequest
Interface HttpServletResponse
Class Cookie
Class HttpServlet
Loading a servlet
Initializing a servlet
Request Handling
Destroying the servlet
Loading a servlet:
18
19
The first stage of the servlet life cycle involves loading and initializing the servlet by
container.
The servlet container perorms the following operations as a part of this process:
Loading:The servlet container loads the servlet class by using class loading option from
file system.
Initializing a servlet:
After the servlet is instantiated successfully,the servlet container initializes the
instantiated servlet object.The servlet container initializes this object by invoking ”
init()” method.In init() method you can configure the servlet.
Request handling:
After servlet is intialized ,the servlet is now ready to process client request.
To process client request the servlet container invokes “service()” method
This service() is responsible for handling client request and response.
Destroy(end of service):
It let all the threads currently running in the service method of the servlet instance
to complete thier jobs and get released.
Then sevlet container make a call to ” destroy()” method.
Then the destroy method releases the resources held by the servlet.
Servlet Skeleton:
import javax.servlet.*;
19
20
import java.io.*;
public class greeting extends GenericServlet
{
public void init(ServletConfig sc)
{
//initilaze the configurations for servlet
}
public void service(ServletRequest req,ServletResponse res)throws
ServletException,IOException
{
//handle request and response
}
public void destroy()
{
//resources are released by the servlet.
}
20
21
Before developing the web applications, we need to have idea about design models. There are two
types of programming models (design models)
1. Model 1 Architecture
2. Model 2 (MVC) Architecture
Model 1 Architecture
As you can see in the above figure, there is picture which show the flow of the model1
architecture.
21
22
Model - The lowest level of the pattern which is responsible for maintaining
data. It can also have business logic.
View - This is responsible for displaying all or a portion of the data to the user.
represents the presentaion i.e. UI(User Interface).
Controller - Software Code that controls the interactions between the Model and
View.
22
23
MVC is popular as it isolates the application logic from the user interface layer and
supports separation of concerns. Here the Controller receives all requests for the
application and then works with the Model to prepare any data needed by the View.
The View then uses the data prepared by the Controller to generate a final presentable
response. The MVC abstraction can be graphically represented as follows.
23
24
24
25
25