JDBC Word Document
JDBC Word Document
Java JDBC is a java API to connect and execute query with the database. JDBC API uses jdbc drivers
to connect with the database.
Before JDBC, ODBC API was the database API to connect and execute query with the database. But,
ODBC API uses ODBC driver which is written in C language. That is why Java has defined its own API
(JDBC API) that uses JDBC drivers.
API is a document that contains description of all the features of a product or software. It represents
classes and interfaces that software programs can follow to communicate with each other.
JDBC Driver is a software component that enables java application to interact with the database.
JDBC-ODBC bridge driver: Uses ODBC Driver to connect to the Db. It Converts JDBC method calls
to ODBC function calls.
Native-API driver (partially java 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.
Network Protocol driver (fully java driver): uses middleware (application server) that converts
JDBC calls directly or indirectly into the vendor-specific db protocol. It is fully written in java.
Thin driver (fully java 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.
Better Performance than all the Drivers and No S/w is required at client side or server side.
Drivers Depends on the Database.
There are 5 steps to connect any java application with the database in java using JDBC.
Register the Driver Class: forName() of Class class is used to register the driver class. This method
is used to dynamically load the driver class.
public static void forName(String className)throws ClassNotFoundException
Class.forName("oracle.jdbc.driver.OracleDriver");
Creating Connection: getConnection() of DriverManager class is used to establish connection with
the db.
public static Connection getConnection(String url)throws SQLException
public static Connection getConnection(String url,String name,String password)
throws SQLException
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","password");
Creating Statement: createStatement() of Connection interface is used to create statement. The
object of statement is responsible to execute queries with the database.
public Statement createStatement()throws SQLException
Statement stmt=con.createStatement();
Executing Queries: executeQuery() of Statement interface is used to execute queries to the
database. This method returns the object of ResultSet that can be used to get all the records of a
table.
public ResultSet executeQuery(String sql)throws SQLException
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
Closing Connection: By closing connection object statement & ResultSet will be closed automatically.
The close() method of Connection interface is used to close the connection.
public void close()throws SQLException
con.close();
For connecting java application with the database, we need to follow 5 steps to perform database
connectivity.
Driver class is oracle.jdbc.driver.OracleDriver(or)com.mysql.jdbc.Driver
Connection URL is jdbc:oracle:thin:@localhost:1521:xe(or)jdbc:mysql://localhost:3306/db
Username & Password
DriverManager class: Acts as an interface between user & drivers. It keeps track of the drivers
that are available and handles establishing a connection between a db & the appropriate driver. The
DriverManager class maintains a list of Driver classes that have registered themselves by calling the
method DriverManager.registerDriver().
public static void registerDriver(Driver driver)
public static void deregisterDriver(Driver driver)
public static Connection getConnection(String url)
public static Connection getConnection(String url,String userName,String password)
Connection interface: is the session between java application and database. The Connection
interface is a factory of Statement, PreparedStatement, and DatabaseMetaData i.e. object of
Connection can be used to get the object of Statement and DatabaseMetaData. The Connection
interface provide many methods for transaction management like commit(),rollback() etc.
Statement interface: Provides methods to execute queries with the database. The statement
interface is a factory of ResultSet i.e. it provides factory method to get the object of ResultSet.
public ResultSet executeQuery(String sql)
public int executeUpdate(String sql)
public boolean execute(String sql)
public int[] executeBatch()
ResultSet interface: object of ResultSet maintains a cursor pointing to a particular row of data.
Initially, cursor points to before the first row.
>You can store images in the database in java by the help of PreparedStatement interface.
The setBinaryStream() of PreparedStatement is used to set Binary information into parameterIndex.
Syn:
For storing image into the database, BLOB (Binary Large Object) datatype is used in the table.
CREATE TABLE "IMGTABLE" ("NAME" VARCHAR2(4000), "PHOTO" BLOB);
>By the help of PreparedStatement we can retrieve and store the image in the database.
getBlob() method of PreparedStatement is used to get Binary information, it returns the instance of
Blob. After calling the getBytes() method on the blob object, we can get the array of binary
information that can be written into the image file.
public Blob getBlob()throws SQLException
)throws SQLException//Blob interface
Function
Batch Processing: Instead of executing a single query, we can execute a batch (group) of
queries. The java.sql.Statement and java.sql.PreparedStatement interfaces provide methods for batch
processing.
Fast Performance
Methods:
JdbcRowSet
CachedRowSet
WebRowSet
JoinRowSet
FilteredRowSet
To perform event handling with JdbcRowSet, you need to add the instance of RowSetListener in
the addRowSetListener method of JdbcRowSet.
public void cursorMoved(RowSetEvent event)
public void rowChanged(RowSetEvent event)
public void rowSetChanged(RowSetEvent event)
Event Handling support in Statement for Connection Pooling Now Connection Pooling can listen
statement error and statement closing events.
Servlet