JDBC Components
JDBC stands for Java Database Connectivity. For connectivity with the database we uses JDBC. It establish connection to access the database. This provides a set of classes in the java.sql package for Java applications to communicate with databases. Mostly databases used a language called SQL. SQL stands for Structured Query Language. JDBC gives you the opportunity to communicate with standard database. JDBC includes four components:
1. The JDBC API
The JDBC API gives access of programming data from the Java. To use this, applications can execute SQL statements and retrieve results and updation to the database. The JDBC API is part of the Java platform, it includes the Java Standard Edition.
2. JDBC Driver Manager
The JDBC DriverManager is the class in JDBC API. The objects of this class can connect Java applications to a JDBC driver. DriverManager is the very important part of the JDBC architecture.
3. JDBC Test Suite
The JDBC driver test suite helps JDBC drivers to run your program. They are not exhaustive,they do exercise with important features in the JDBC API.
4. JDBC-ODBC Bridge
The Java Software bridge provides JDBC access via ODBC drivers.You have to load ODBC binary code for client machines for using this driver. This driver is very important for application server code has to be in Java in a three-tier architecture.
The program below describes how to run the JDBC program with MySql.
JDBCExample.java is the program to understand how to establish the
connection with database. The Url, Connection, Driver, Statement,
Resultset etc. and how to process the java code with database is
mentioned here.
JDBCExample.java
import
java.sql.*;
|
Description
of program
This program
making the connection between MySQL database and java with the help
of many types of APIs interfaces.First connected it and then execute
a query.It shows the result, like in above example it displays the
all employees in the existing table Employee.
Description of code
1.
Connection
An interface in
java.sql package that provides connection with the database like-
MySQL and java files. The SQL statements are executed within the
context of the Connection interface.
2.
Class.forName(String driver)
forName() is static
method of the "Class" class . This loads the driver class and returns
Class
instance. It takes string type value as an argument and matches the
class with string.
3.
DriverManager
This class of
java.sql package controls the JDBC drivers. Each driver has to be
registered with this class.
4.
getConnection(String url, String userName, String password)
This
static method of DriverManager class makes a connection with database url. It takes these given
arguments of string type.
url - Database url where stored or
created your database |
5.
con.close()
This method of Connection
interface is used
for closing the connection. All the resources will be free occupied by
the database.
6.
printStackTrace()
The method is used to show error messages.
If the connection is not connected then it throws the exception and
print the message.
Java Database Connectivity Steps-
Some steps are given below of the JDBC
1. First
import the java.sql package
import java.sql.*; |
The star '*' indicates that all of the classes in the java.sql and java.io packages are to be imported.
2. Loading a database driver
In this step, the
driver class loaded by calling Class.forName() with the Driver class.
Class is a class in java.lang package.
Class.forName("com.mysql.jdbc.Driver"); |
3. Creating a jdbc Connection
The objects
defined by DriverManager class to establish the applications with the
JDBC driver. This class manages the JDBC drivers which is installed
on the system. getConnection() is the method by which it can connect.
It uses the username, password, and a jdbc url to make the connection
with database.
try{ |
4. Creating a
jdbc Statement object
When
an connection is established then we can interact with the database.
Connection interface defines methods for interacting with the
database. It used to instantiate a Statement by using the
createStatement() method.
Statement st = con.createStatement(); |
5. Executing a statement with the Statement object
This interface
defines methods which is used to communicate with database. This
class has three methods to execute the statements- executeQuery(),
executeUpdate(), and execute(). For SELECT statements, executeQuery()
method will be used. For create or modify tables, the executeUpdate() method is
used. executeQuery() method returns the result of the query in the form of
ResultSet object and executeUpdate() method returns the number of rows affected
from execution of the query.
ResultSet rs = st.executeQuery("SELECT * from Employee"); |
6. Getting ResultSet object
Executing the executeQuery()
method returns the result in the form of ResultSet object. We can now operate on
this object to extract the rows values returned from the execution of the query.
Its next() method sets the pointer to the first row and getX() methods can be
used to get different types of data from the result set.
while( rs.next() ) { String data = rs.getString(1); System.out.println( data ); } |