1. Introduction to JDBC
2. JDBC Architecture
3. Interaction of JDBC with Database
4. Introduction to Hibernate
5. Hibernate Architecture
6. Hibernate Communication with RDBMS
7. Hibernate vs. JDBC
7.1. Advantage of Hibernate over JDBC
7.2. Disadvantages of Hibernate
1. Introduction to JDBC
JDBC
stands for Java Database Connectivity allows developers to connect,
query and update a database using the Structured Query Language. JDBC
API standard provides Java developers to interact with different RDBMS
and access table data through Java application without learning RDBMS
details and using Database Specific JDBC Drivers.
2. JDBC Architecture
JDBC makes the interaction with RDBMS simple and
intuitive. when a Java application needs to access database:
• open connection to database,
• use JDBC driver to send SQL queries to database,
• process the results that are returned, and
• close the connection.
JDBC uses two architectures to communicate with database:
1) The
driver connects to database and executes SQL statements. Results are
sent back from driver to driver manager and finally to the application.
2) The
JDBC driver communicates with ODBC driver. ODBC driver executes SQL
query and then results are sent back to JDBC driver to driver manager
and then to application.
1. Introduction to JDBC
2. JDBC Architecture
3. Interaction of JDBC with Database
4. Introduction to Hibernate
5. Hibernate Architecture
6. Hibernate Communication with RDBMS
7. Hibernate vs. JDBC
7.1. Advantage of Hibernate over JDBC
7.2. Disadvantages of Hibernate
1. Introduction to JDBC
JDBC stands for Java Database Connectivity allows developers to connect, query and update a database using the Structured Query Language. JDBC API standard provides Java developers to interact with different RDBMS and access table data through Java application without learning RDBMS details and using Database Specific JDBC Drivers.
2. JDBC Architecture
JDBC makes the interaction with RDBMS simple and
intuitive. when a Java application needs to access database:
JDBC uses two architectures to communicate with database:
1) The
driver connects to database and executes SQL statements. Results are
sent back from driver to driver manager and finally to the application.2) The JDBC driver communicates with ODBC driver. ODBC driver executes SQL query and then results are sent back to JDBC driver to driver manager and then to application.
General steps:
1) Load the RDBMS specific JDBC driver because this driver actually communicates with the database.
2) Open the connection to database which is then used to send SQL statements and get results back.
3) Create JDBC Statement object. This object contains SQL query.
4) Execute statement which returns resultset(s). ResultSet contains the tuples of database table as a result of SQL query.
5) Process the result set.
6) Close the connection.
Example: Retrieve list of employees from Employee table using JDBC.
String url = “jdbc:odbc:” + dbName;
List
/* load the jdbc-odbc driver */ class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
/* Open a connection to database */
Connection con = DriverManager.getConnection(url);
/* create Statement object */
Statement stmt = con.createStatement();
/* execute statement */
ResultSet rs = stmt.executeQuery("SELECT * FROM Sells"); while ( rs.next() )
{
EmployeeBean eb = new Employeebean(); eb.setName(rs.getString("name")); eb.setSalary(rs.getFloat("salary")); employeeList.add(eb);
}
Types of JDBC Driver
JDBC drivers are divided into four types or levels. The different types of jdbc drivers are:
Type 1: JDBC-ODBC Bridge driver (Bridge)Type 2: Native-API/partly Java driver (Native)
Type 3: AllJava/Net-protocol driver (Middleware)
Type 4: All Java/Native-protocol driver (Pure)
4 types of jdbc drivers are elaborated in detail as shown below:
Type 1 JDBC Driver
JDBC-ODBC Bridge driver
The Type 1 driver translates all JDBC calls into ODBC calls and sends them to the ODBC driver. ODBC is a generic API. The JDBC-ODBC Bridge driver is recommended only for experimental use or when no other alternative is available.
Type 1: JDBC-ODBC Bridge
2. A performance issue is seen as a JDBC call goes through the bridge to the ODBC driver, then to the database, and this applies even in the reverse process. They are the slowest of all driver types.
3. The client system requires the ODBC Installation to use the driver.
4. Not good for the Web.
The distinctive characteristic of type 2 jdbc drivers are that Type 2 drivers convert JDBC calls into database-specific calls i.e. this driver is specific to a particular database. Some distinctive characteristic of type 2 jdbc drivers are shown below. Example: Oracle will have oracle native api.
Type 2: Native api/ Partly Java Driver
1 and also it uses Native api which is Database specific.
2. Like Type 1 drivers, it’s not written in Java Language which forms a portability issue.
3. If we change the Database we have to change the native api as it is specific to a database
4. Mostly obsolete now
5. Usually not thread safe.
Type 3 database requests are passed through the network to the middle-tier server. The middle-tier then translates the request to the database. If the middle-tier server can in turn use Type1, Type 2 or Type 4 drivers.
Type 3: All Java/ Net-Protocol Driver
2. This driver is fully written in Java and hence Portable. It is suitable for the web.
3. There are many opportunities to optimize portability, performance, and scalability.
4. The net protocol can be designed to make the client JDBC driver very small and fast to load.
5. The type 3 driver typically provides support for features such as caching (connections, query results, and so on), load balancing, and advanced
system administration such as logging and auditing.
6. This driver is very flexible allows access to multiple databases using one driver.
7. They are the most efficient amongst all driver types.
Disadvantage
It requires another server application to install and maintain. Traversing the recordset may take longer, since the data comes through the backend server.
The Type 4 uses java networking libraries to communicate directly with the database server.
Type 4: Native-protocol/all-Java driver
2. Number of translation layers is very less i.e. type 4 JDBC drivers don’t have to translate database requests to ODBC or a native connectivity interface or to pass the request on to another server, performance is typically quite good.
3. You don’t need to install special software on the client or server. Further, these drivers can be downloaded dynamically.
Disadvantage
With type 4 drivers, the user needs a different driver for each database.
Advantage
The JDBC-ODBC Bridge allows access to almost any database, since the database’s ODBC drivers are already available.
Disadvantages
1. Since the Bridge driver is not written fully in Java, Type 1 drivers are not portable.2. A performance issue is seen as a JDBC call goes through the bridge to the ODBC driver, then to the database, and this applies even in the reverse process. They are the slowest of all driver types.
3. The client system requires the ODBC Installation to use the driver.
4. Not good for the Web.
Type 2 JDBC Driver
Native-API/partly Java driverThe distinctive characteristic of type 2 jdbc drivers are that Type 2 drivers convert JDBC calls into database-specific calls i.e. this driver is specific to a particular database. Some distinctive characteristic of type 2 jdbc drivers are shown below. Example: Oracle will have oracle native api.
Advantage
The
distinctive characteristic of type 2 jdbc drivers are that they are
typically offer better performance than the JDBC-ODBC Bridge as the
layers of communication (tiers) are less than that of Type1 and also it uses Native api which is Database specific.
Disadvantage
1. Native API must be installed in the Client System and hence type 2 drivers cannot be used for the Internet.2. Like Type 1 drivers, it’s not written in Java Language which forms a portability issue.
3. If we change the Database we have to change the native api as it is specific to a database
4. Mostly obsolete now
5. Usually not thread safe.
Type 3 JDBC Driver
All Java/Net-protocol driverType 3 database requests are passed through the network to the middle-tier server. The middle-tier then translates the request to the database. If the middle-tier server can in turn use Type1, Type 2 or Type 4 drivers.
Advantage
1. This driver is server-based, so there is no need for any vendor database library to be present on client machines.2. This driver is fully written in Java and hence Portable. It is suitable for the web.
3. There are many opportunities to optimize portability, performance, and scalability.
4. The net protocol can be designed to make the client JDBC driver very small and fast to load.
5. The type 3 driver typically provides support for features such as caching (connections, query results, and so on), load balancing, and advanced
system administration such as logging and auditing.
6. This driver is very flexible allows access to multiple databases using one driver.
7. They are the most efficient amongst all driver types.
Disadvantage
It requires another server application to install and maintain. Traversing the recordset may take longer, since the data comes through the backend server.
Type 4 JDBC Driver
Native-protocol/all-Java driverThe Type 4 uses java networking libraries to communicate directly with the database server.
Advantage
1.
The major benefit of using a type 4 jdbc drivers are that they are
completely written in Java to achieve platform independence and
eliminate deployment administration issues. It is most suitable for the
web.2. Number of translation layers is very less i.e. type 4 JDBC drivers don’t have to translate database requests to ODBC or a native connectivity interface or to pass the request on to another server, performance is typically quite good.
3. You don’t need to install special software on the client or server. Further, these drivers can be downloaded dynamically.
Disadvantage
With type 4 drivers, the user needs a different driver for each database.
No comments:
Post a Comment