What is JDBC?

JDBC stands for java database connectivity.It ia basically an API (Application Programming Interface) which allows you to connect multiple databases to your java application.It is a technology which is used to connect java application with database.
Java Database Connectivity (JDBC) API: Provides tools for developers to write applications with access to remote relationship databases, flat files and spreadsheets.
Need for JDBC -
Before JDBC, ODBC API was used to connect java applications to other databases.But the issue was ODBC was written in C language, to overcome this problem JDBC was developed.JDBC is written in Java.
Some common JDBC API classes and interfaces -

JDBC Statements

JDBC statements are used to create sql objects while interacting with database.They are used when we want to pass a SQL query to database.
Different types of statements are --

Prepared Statements - If we want to run some query multiple times .The queries written through it requires parameters value to pass. Use in the case when we have parameterised sql query.Thses statements are pre-compiled.Hence, prevent SQL injection.

Statements

Statements are use to execute a simple sql query.No parameters are requred in this case.Statements are less efficient than prepared statements as prepared statements allows pre-compilation of SQL queries.
It encapsulates an sql statements which is passed to the database to be parsed , compiled , planned and executed.
Difference between statements and prepared statemets -

Callable Statements

When we are dealing with procedures we go with creating callable objects.It is used to call the SQL stored procedures in the database.The stored procedures are similar to functions as they perform some specific tasks , except that they are only available in the database. The callableStatements can return either a single ResultSet object or multiple ResultSet objects.

JDBC Drivers

These drivers are responsible for getting Connection to the database.They are used as an interface between JDBC API and underlying database. JDBC driver is an interface enabling a java application to interact with a database. To connect with individual database JDBC requires drivers for each database.The JDBC Driver gives out the connection to the database and implements the protocol for transferring the query and result between client and database.
Sun declared 4 types of JDBC drivers -

Type 1 JDNC Driver are the JDBC-ODBC bridge.They support multiple concurrent open statements per connection.
Type 3 & Type 4 JDBC Driver can be used in either applet or servlet code.and are used over communication channel.
Type 4 JDBC Driver converts JDBC calls into the network protocol used by the database management system directly.These drivers are 100% java compatible.They are pure java driver.They are highly efficient and always preferable for using JDBC applications.(also called thin driver in JDBC).It is the fastest driver among all.

ResultSet

They are responsible for retrieving and manipulating data.When we retrieve any information from database it is saved in ResultSet Object.These objects hold data retrieved from a database after you execute an SQL query using statements objects.It acts as an iterator to allow you to move through its data.The java.sql.ResultSet interface represents the result set of a database query.
Types of resultSet -

Class.forName() Vs RegisterDriver class

These 2 classes can be use for loading a database driver in JDBC.
Class.forName() - This method dynamically loads the driver class file into memory , which automatically registers it.This methos is preferable because it allows you to make the driver registration configurable and portable.
DriverManager.registerDriver - This static method is used incase you are using a non-JDK compliant JVM.

JDBC Architecture

JDBC architecture gives a flow to connect your Java application to the underlying databases using JDBC drivers.General JDBC architecture consists of 2 layers -
JDBC API(This provides the application-to-jdbc manager connection)
JDBC Driver API(this supports the JDBC manager-to-Driver connection)
It's architecture composed of --

Types of files we can acsess using JDBC.

JDBC API allows java application to access --

Java Syntax for making DB Connection


import 'java.sql.x';
main()
{
Class.forName("com.mysql.jdbcDriver");
Connection con=DriverManager.getConnection("URL","Username","password");
Statement st =con.CreateStatement();
ResultSet rs=st.executeQuery("seelct * from student");
while(re.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
st.close();
con.close();
}