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
Description of code
getConnection(String url, String userName, String password)
Java Database Connectivity Steps-
Some steps are given below of the JDBC
import the java.sql package
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.
3. Creating a jdbc Connection
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
4. Creating a
jdbc Statement object
5. Executing a statement with the Statement object
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.
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.