Unit 4
Unit 4
Unit 4
JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the query with the
database. It is a part of Java SE (Java Standard Edition). JDBC API uses JDBC drivers to connect with the database.
JDBC or Java Database Connectivity is a Java API to connect and execute the query with the database. It is a
specification from Sun micro systems that provides a standard abstraction(API or Protocol) for java applications
to communicate with various databases.
Components of JDBC
There are generally four main components of JDBC through which it can interact with a database. They are as
mentioned below:
1. JDBC API: It provides various methods and interfaces for easy communication with the database. It provides two
packages as follows, which contain the java SE and Java EE platforms to exhibit WORA(write once run anywhere)
capabilities.
java.sql.*;
It also provides a standard to connect a database to a client application.
2. JDBC Driver manager: It loads a database-specific driver in an application to establish a connection with a
database. It is used to make a database-specific call to the database to process the user request.
3. JDBC Test suite: It is used to test the operation(such as insertion, deletion, updation) being performed by JDBC
Drivers.
4. JDBC-ODBC Bridge Drivers: It connects database drivers to the database. This bridge translates the JDBC
method call to the ODBC function call. It makes use of the sun.jdbc.odbc package which includes a native library
to access ODBC characteristics.
Architecture of JDBC
JDBC Drivers
JDBC drivers are client-side adapters (installed on the client machine, not on the server) that convert requests
from Java programs to a protocol that the DBMS can understand. There are 4 types of JDBC drivers:
1. Type-1 driver or JDBC-ODBC bridge driver
2. Type-2 driver or Native-API driver
3. Type-3 driver or Network Protocol driver
4. Type-4 driver or Thin driver
Advantages:
o easy to use.
o can be easily connected to any database.
Disadvantages:
o Performance degraded because JDBC method call is converted into the ODBC function calls.
o The ODBC driver needs to be installed on the client machine.
2) Native-API driver
The Native API driver uses the client-side libraries of the database. The driver converts JDBC method calls
into native calls of the database API. It is not written entirely in java.
Advantage:
o performance upgraded than JDBC-ODBC bridge driver.
Disadvantage:
o The Native driver needs to be installed on the each client machine.
o The Vendor client library needs to be installed on client machine.
The Network Protocol driver uses middleware (application server) that converts JDBC calls directly or indirectly
into the vendor-specific database protocol. It is fully written in java.
Advantage:
o No client side library is required because of application server that can perform many tasks like auditing,
load balancing, logging etc.
Disadvantages:
o Network support is required on client machine.
o Requires database-specific coding to be done in the middle tier.
o Maintenance of Network Protocol driver becomes costly because it requires database-specific coding to be
done in the middle tier.
4) Thin driver
The thin driver converts JDBC calls directly into the vendor-specific database protocol. That is why it is known as thin
driver. It is fully written in Java language.
Advantage:
o Better performance than all other drivers.
o No software is required at client side or server side.
Disadvantage:
o Drivers depend on the Database.
Java Database Connectivity with 5 Steps
There are 5 steps to connect any java application with the database using JDBC. These
steps are as follows:
Class.forName("oracle.jdbc.driver.OracleDriver");
NATURAL JOIN: It is a type of join that retrieves data within specified tables to a specific field that is matched.
NATURAL LEFT JOIN: In this operation, both tables are merged with each other according to common fields but the priority
is given to the first table in the database.
NATURAL RIGHT JOIN: It also the same as Natural left join but it retrieves the data from the second table in the database.
Second table
Merging table:
To connect java application with the oracle database, we need to follow 5 following steps. In this example, we are
using Oracle 10g as the database. So we need to know following information for the oracle database:
1. Driver class: The driver class for the oracle database is oracle.jdbc.driver.OracleDriver.
2. Connection URL: The connection URL for the oracle10G database
is jdbc:oracle:thin:@localhost:1521:xe where jdbc is the API, oracle is the database, thin is the driver,
localhost is the server name on which oracle is running, we may also use IP address, 1521 is the port
number and XE is the Oracle service name. You may get all these information from the tnsnames.ora file.
3. Username: The default username for the oracle database is system.
4. Password: It is the password given by the user at the time of installing the oracle database.
Create a Table
Before establishing connection, let's first create a table in oracle database. Following is the SQL query to create a
table.
In this example, we are connecting to an Oracle database and getting data from emp table.
Here, system and oracle are the username and password of the Oracle database.
import java.sql.*;
class OracleCon{
public static void main(String args[]){
try{
//step1 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
//step2 create the connection object
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
//step3 create the statement object
Statement stmt=con.createStatement();
//step4 execute query
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
//step5 close the connection object
con.close();
}
catch(Exception e){ System.out.println(e);}
} }
1) JSP out implicit object
For writing any data to the buffer, JSP provides an implicit object named out. It is the object of JspWriter. In case of
servlet you need to write:
1. PrintWriter out=response.getWriter();
index.jsp
1. <html>
2. <body>
3. <% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>
4. </body>
5. </html>
Output
Java Bean:
A JavaBean is a specially constructed Java class written in the Java and coded
according to the JavaBeans API specifications.
Following are the unique characteristics that distinguish a JavaBean from other Java
classes −
It provides a default, no-argument constructor.
It should be serializable and that which can implement the Serializable interface.
It may have a number of properties which can be read or written.
It may have a number of "getter" and "setter" methods for the properties.
It is a reusable software component. A bean encapsulates many objects into one object
so that we can access this object from multiple places. Moreover, it provides easy
maintenance.
JavaBean Properties
A JavaBean property is a named feature that can be accessed by the user of the object.
The feature can be of any Java data type, containing the classes that you define.
1. getPropertyName ()
For example, if the property name is firstName, the method name would be
getFirstName() to read that property. This method is called the accessor.
2. setPropertyName ()
For example, if the property name is firstName, the method name would be
setFirstName() to write that property. This method is called the mutator.
Advantages of JavaBean
Disadvantages of JavaBean
package mypack;
public class Employee implements java.io.Serializable{
private int id;
private String name;
public Employee(){}
public void setId(int id)
{
this.id=id;
}
public int getId()
{
return id;
}
public void setName(String name){this.name=name;}
public String getName(){
return name;
}
}
How to access the JavaBean class?
To access the JavaBean class, we should use getter and setter methods.
package mypack;
public class Test{
public static void main(String args[]){
Employee e=new Employee();//object is created
e.setName("Arjun");//setting value to the object
System.out.println(e.getName());
}}
Types of EJB
2. Message Driven Bean: Like Session Bean, it contains the business logic but it is
invoked by passing message.
3. Entity Bean: It summarizes the state that can be remained in the database. It is
deprecated. Now, it is replaced with JPA (Java Persistent API). There are two types of
entity bean: