What Is JDBC?: Java Database Connectivity Api Allows Us To Work With Relational
What Is JDBC?: Java Database Connectivity Api Allows Us To Work With Relational
· Load the Driver: First step is to load the database specific driver which
communicates with database.
· Make Connection: Next step is get connection from the database using
connection object, which is used to send SQL statement also and get result
back from the database.
· Get Statement object: From connection object we can get statement
object which is used to query the database
· Execute the Query:Using statement object we execute the SQL or
database query and get result set from the query.
· Close the connection:After getting resultset and all required operation
performed the last step should be closing the database connection.
There are four types of JDBC drivers. Any java program that works with
database has two parts, first part is the JDBC API and second part is the
driver that does the actual work.
1. JDBC-ODBC Bridge plus ODBC Driver (Type 1): It uses ODBC driver to
connect to database. We should have ODBC drivers installed to connect to
database, that’s why this driver is almost obsolete.
2. Native API partly Java technology-enabled driver (Type 2): This driver
converts JDBC class to the client API for the database servers. We should
have database client API installed. Because of extra dependency on database
client API drivers, this is also not preferred driver.
3. Pure Java Driver for Database Middleware (Type 3): This driver sends the
JDBC calls to a middleware server that can connect to different type of
databases. We should have a middleware server installed to work with this
driver. This adds to extra network calls and slow performance and that’s why
not widely used JDBC driver.
4. Direct-to-Database Pure Java Driver (Type 4): This driver converts the
JDBC calls to the network protocol understood by the database server. This
solution is simple and suitable for database connectivity over the network.
However for this solution, we should use database specific drivers, for
example OJDBC jars by Oracle for Oracle DB and MySQL Connector/J for
MySQL databases.
JDBC Connection is like a Session created with the database server. You can
also think Connection is like a Socket connection from the database server.
Creating a JDBC Connection is very easy and requires two steps:
package com.journaldev.jdbc.statements;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
JDBC DriverManager is the factory class through which we get the Database
Connection object. When we load the JDBC Driver class, it registers itself to
the DriverManager, you can look up the JDBC Driver classes source code to
check this.
Then when we call DriverManager.getConnection() method by passing the
database configuration details, DriverManager uses the registered drivers to
get the Connection and return it to the caller program.
Statement execute(String query) is used to execute any SQL query and it returns TRUE if the
result is an ResultSet such as running Select queries. The output is FALSE when there is no
ResultSet object such as running Insert or Update queries. We can use getResultSet() to get the
ResultSet and getUpdateCount() method to retrieve the update count.
Statement executeQuery(String query) is used to execute Select queries and returns the
ResultSet.
ResultSet returned is never null even if there are no records matching the query. When
executing select queries we should use the executeQuery method so that if someone tries to
execute insert/update statement it will throw java.sql.SQLException with message
“executeQuery method cannot be used for update”.
You should use execute() method only when you are not sure about the type of statement else
use executeQuery or executeUpdate method.
What is JDBC ResultSet?
JDBC ResultSet is like a table of data representing a database result set, which
is usually generated by executing a statement that queries the database.
ResultSet object maintains a cursor pointing to its current row of data. Initially,
the cursor is positioned before the first row. The next() method moves the
cursor to the next row. If there are no more rows, next() method returns false
and it can be used in a while loop to iterate through the result set.
A default ResultSet object is not updatable and has a cursor that moves
forward only. Thus, you can iterate through it only once and only from the first
row to the last row. It is possible to produce ResultSet objects that are
scrollable and/or updatable using below syntax.
There are different types of ResultSet objects that we can get based on the
user input while creating the Statement. If you will look into the Connection
methods, you will see that createStatement() and prepareStatement() method
are overloaded to provide ResultSet type and concurrency as input argument.
There are three types of ResultSet object.
1. ResultSet.TYPE_FORWARD_ONLY: This is the default type and cursor can
only move forward in the result set.
2. ResultSet.TYPE_SCROLL_INSENSITIVE: The cursor can move forward and
backward, and the result set is not sensitive to changes made by others to the
database after the result set was created.
3. ResultSet.TYPE_SCROLL_SENSITIVE: The cursor can move forward and
backward, and the result set is sensitive to changes made by others to the
database after the result set was created.