Java Database Connectivity
Java Database Connectivity
Java Database Connectivity
JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and
execute the query with the database. It is a specification from Sun Microsystems that
provides a standard abstraction(API or Protocol) for Java applications to
communicate with various databases. It provides the language with Java database
connectivity standards. It is used to write programs required to access databases.
JDBC, along with the database driver, can access databases and spreadsheets. The
enterprise data stored in a relational database(RDB) can be accessed with the help of
JDBC APIs.
Purpose of JDBC
Enterprise applications created using the JAVA EE technology need to interact with
databases to store application-specific information. So, interacting with a database
requires efficient database connectivity, which can be achieved by using
the ODBC(Open database connectivity) driver. This driver is used with JDBC to
interact or communicate with various kinds of databases such as Oracle, MS Access,
Mysql, and SQL server database.
Components of JDBC
There are generally four main components of JDBC through which it can interact
with a database. They are as mentioned below:
1. JDBC API: It provides various methods and interfaces for easy communication
with the database. It provides two packages as follows, which contain the java SE
and Java EE platforms to exhibit WORA(write once run anywhere) capabilities.
The java.sql package contains interfaces and classes of JDBC API.
Architecture of JDBC
Description:
1. Application: It is a java applet or a servlet that communicates with a data
source.
2. The JDBC API: The JDBC API allows Java programs to execute SQL
statements and retrieve results. Some of the important classes and
interfaces defined in JDBC API are as follows:
3. DriverManager: It plays an important role in the JDBC architecture. It
uses some database-specific drivers to effectively connect enterprise
applications to databases.
4. JDBC drivers: To communicate with a data source through JDBC, you
need a JDBC driver that intelligently communicates with the respective
data source.
package com.vinayak.jdbc;
import java.sql.*;
String driverClassName
= "sun.jdbc.odbc.JdbcOdbcDriver";
String query
Class.forName(driverClassName);
// Obtain a connection
Statement st = con.createStatement();
System.out.println(
+ count);
con.close();
} // class
The above example demonstrates the basic steps to access a database using JDBC.
The application uses the JDBC-ODBC bridge driver to connect to the database. You
must import java.sql package to provide basic SQL functionality and use the classes
of the package.
What is the need of JDBC?
JDBC is a Java database API used for making connection between java applications
with various databases. Basically, JDBC used for establishing stable database
connection with the application API. To execute and process relational database
queries (SQL or Oracle queries), multiple application can connect to different types
of databases which supports both standard (SE) and enterprise (EE) edition of java.
https://www.youtube.com/watch?v=qDznViPYu-8&t=1444s
: Joining,
Manipulating, Databases with JDBC,
https://www.youtube.com/watch?v=VzSQTEsiIQ0&t=1132s
Types of Statements in JDBC
The statement interface is used to create SQL basic statements in Java it provides
methods to execute queries with the database. There are different types of statements
that are used in JDBC as follows:
Create Statement
Prepared Statement
Callable Statement
1. Create a Statement: From the connection interface, you can create the object for
this interface. It is generally used for general–purpose access to databases and is
useful while using static SQL statements at runtime.
Syntax:
Statement statement = connection.createStatement();
Implementation: Once the Statement object is created, there are three ways to
execute it.
Implementation: Once the PreparedStatement object is created, there are three ways
to execute it:
execute(): This returns a boolean value and executes a static SQL
statement that is present in the prepared statement object.
executeQuery() : Returns a ResultSet from the current prepared statement.
executeUpdate() : Returns the number of rows affected by the DML
statements such as INSERT, DELETE, and more that is present in
the current Prepared Statement.
3. Callable Statement are stored procedures which are a group of statements that
we compile in the database for some task, they are beneficial when we are dealing
with multiple tables with complex scenario & rather than sending multiple queries to
the database, we can send the required data to the stored procedure & lower the logic
executed in the database server itself. The Callable Statement interface provided by
JDBC API helps in executing stored procedures.
Syntax: To prepare a CallableStatement
CallableStatement cstmt = con.prepareCall("{call
Procedure_name(?, ?}");
Implementation: Once the callable statement object is created
execute() is used to perform the execution of the statement.