Module 4
Module 4
Module-4
Talking to Database, Essential JDBC program, using prepared Statement Object, Interactive
SQL tool. JDBC in Action Result sets, Batch updates, Mapping, Basic JDBC data types,
Advanced JDBC data types Introduction to EJB, types of EJB
Advantage
o Easy to use
o easy connectivity to all database supported by the ODBC Driver
Disadvantage
o Slow execution time
o Dependent on ODBC Driver.
o Uses Java Native Interface(JNI) to make ODBC call.
Advantage
o faster as compared to Type-1 Driver
o Contains additional features
Disadvantage
o Requires native library
o Increased cost of Application
Advantage
o Does not require any native library to be installed.
o Database Independency.
o Provide facility to switch over from one database to another database
Disadvantage
o Slow due to increase number of network call.
Advantages
o Does not require any native library.
o Does not require any Middleware server.
o Better Performance than other driver.
Disadvantage
o Slow due to increase number of network call.
JDBC Package
o This package is also known as JDBC extension API. It provides classes and
interface to access server-side data.
o Important classes and interface of javax.sql package
importing package
import java.sql.*;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
establish connection
getConnection( ) method of DriverManager class is used to create a
connection
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","
username","password");
create connection
createStatement( ) method is invoked on current Connection object to create
a SQL Statement.
Statement s=con.createStatement();
execute statement
executeQuery( ) method of Statement interface is used to execute SQL statements.
ResultSet rs=s.executeQuery("select * from user");
con.close();
stmt.close();
Improves performance: The performance of the application will be faster if you use
PreparedStatement interface because query is compiled only once.
This means that, the preparedstatement is executed, the DBMS can just run the
PreparedStatement’ s SQL statement without having to compile it
Creating a PreparedStatement
Once a PreparedStatement is created (prepared) for the above SQL statement, you can
insert parameters at the location of the question mark. This is done using the
many setXXX() methods. Here is an example:
preparedStatement.setLong(1, 123);
The first number (1) is the index of the parameter to insert the value for. The second number
(123) is the value to insert into the SQL statement.
Executing the PreparedStatement
Executing the PreparedStatement looks like executing a regular Statement. To execute a
query, call the executeQuery() or executeUpdate method. Here is
an executeQuery() example:
The cursor is movable based on the properties of the ResultSet. These properties are
designated when the corresponding Statement that generates the ResultSet is created.
Type of ResultSet
Type Description
ResultSet.TYPE_FORWARD_ONLY The cursor can only move forward in the
result set.
ResultSet.TYPE_SCROLL_INSENSITIVE The cursor can scroll forward and
backward, and the result set is not
sensitive to changes made by others to the
database that occur after the result set was
created.
ResultSet.TYPE_SCROLL_SENSITIVE. The cursor can scroll forward and
backward, and the result set is sensitive to
changes made by others to the database
that occur after the result set was created.
Batch Processing allows you to group related SQL statements into a batch and submit
them with one call to the database.
Instead of executing a single query, we can execute a batch (group) of queries. It makes
the performance fast.
The java.sql.Statement and java.sql.PreparedStatement interfaces provide methods for
batch processing.
Method Description
void addBatch(String query) It adds query into batch.
int[] executeBatch() It executes the batch of queries.
Example:
1. Statement stmt=con.createStatement();
2. stmt.addBatch("insert into student values(100,”anu”);
3. stmt.addBatch("insert into student values(101,”anil”);
4.7 Mapping
The JDBC driver converts the Java data type to the appropriate JDBC type, before sending
it to the database.
It uses a default mapping for most data types. For example, a Java int is converted to an
SQL INTEGER. Default mappings were created to provide consistency between drivers.
Three sets of method in order to transfer data between a database and a application.
o ResultSet class for retrieving SQL SELECT results as java types.
o PreparedStatement class for sending java types as SQL statement parameters.
o CallableStatement class for retrieving SQL OUT parameters as Java types.
Note:
JDBC- Statements, PreparedStatement, Callable Statement
Once a connection is obtained we can interact with the database. The JDBC Statement,
CallableStatement, and PreparedStatement interfaces define the methods and properties
that enable you to send SQL or PL/SQL commands and receive data from your database.
They also define methods that help bridge data type differences between Java and SQL
data types used in a database.
The following table provides a summary of each interface's purpose to decide on the
interface to use.