Curriculum
JDBC Connectivity is one of the most important topics for Java Backend Engineers because it serves as the bridge between Java applications and relational databases. While SQL allows developers to interact with databases manually, real-world applications require Java programs to communicate with databases automatically. JDBC makes this possible.
Modern enterprise applications, Spring Boot services, banking systems, e-commerce platforms, ERP software, healthcare systems, and cloud applications all rely on database connectivity. Before frameworks such as Hibernate, JPA, and Spring Data JPA became popular, JDBC was the primary technology used for database communication in Java. Even today, understanding JDBC Connectivity is essential because higher-level frameworks are built on top of JDBC concepts.
By mastering JDBC Connectivity, developers learn how Java applications establish database connections, execute SQL queries, process results, and manage transactions.
JDBC stands for:
Java Database Connectivity
It is a Java API that enables Java applications to interact with relational databases.
JDBC allows developers to:
Without JDBC, Java applications would not be able to communicate directly with relational databases.
JDBC provides several benefits.
Connect Java applications with databases.
Perform CRUD operations.
Supports multiple databases.
Used in enterprise development.
Hibernate and Spring Data JPA build upon JDBC concepts.
These benefits make JDBC a critical technology for backend development.
JDBC follows a layered architecture.
Java Application
|
JDBC API
|
JDBC Driver
|
Database
Example:
Java Program
|
JDBC
|
MySQL Driver
|
MySQL Database
This architecture enables communication between Java and databases.
JDBC consists of several components.
Provides interfaces and classes.
Examples:
Connection
Statement
PreparedStatement
ResultSet
Translates Java calls into database-specific commands.
Stores actual information.
Together, these components enable database interaction.
A JDBC Driver is software that allows Java applications to communicate with a database.
Think of it as:
Translator Between Java and Database
Without a JDBC driver, Java cannot understand database-specific communication protocols.
Historically, JDBC supported four driver types.
JDBC-ODBC Bridge.
Obsolete today.
Native API Driver.
Rarely used.
Network Protocol Driver.
Less common.
Pure Java Driver.
Most widely used.
Example:
MySQL Connector/J
Modern applications primarily use Type 4 drivers.
For MySQL:
MySQL Connector/J
is required.
This driver enables Java applications to communicate with MySQL databases.
Common Maven dependency:
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>latest</version>
</dependency>
This dependency is frequently used in Java projects.
The typical JDBC workflow consists of several steps.
Load JDBC Driver.
Establish Database Connection.
Create Statement.
Execute SQL Query.
Process Results.
Close Resources.
These steps form the foundation of JDBC programming.
Example:
import java.sql.*;
This package contains all JDBC classes and interfaces.
Example:
String url =
"jdbc:mysql://localhost:3306/studentdb";
String username =
"root";
String password =
"password";
Connection:
Connection connection =
DriverManager.getConnection(
url,
username,
password
);
The application is now connected to the database.
Example:
jdbc:mysql://localhost:3306/studentdb
Components:
jdbc
JDBC protocol.
mysql
Database type.
localhost
Server location.
3306
MySQL port number.
studentdb
Database name.
Understanding JDBC URLs is important.
A Statement executes SQL commands.
Example:
Statement statement =
connection.createStatement();
The statement object is now ready to execute SQL.
Example:
ResultSet resultSet =
statement.executeQuery(
"SELECT * FROM Student"
);
The query retrieves records from the Student table.
Example:
while(resultSet.next()) {
System.out.println(
resultSet.getInt("Student_ID")
);
}
Output:
101
102
103
The ResultSet stores query results.
Example:
resultSet.close();
statement.close();
connection.close();
Closing resources prevents memory leaks.
Always close database resources.
Example:
import java.sql.*;
public class Demo {
public static void main(String[] args)
throws Exception {
Connection connection =
DriverManager.getConnection(
"jdbc:mysql://localhost:3306/studentdb",
"root",
"password"
);
Statement statement =
connection.createStatement();
ResultSet resultSet =
statement.executeQuery(
"SELECT * FROM Student"
);
while(resultSet.next()) {
System.out.println(
resultSet.getString("Name")
);
}
connection.close();
}
}
This example demonstrates a complete JDBC workflow.
JDBC supports all CRUD operations.
Example:
String sql =
"INSERT INTO Student VALUES(101,'Rahul','Java')";
Execute:
statement.executeUpdate(sql);
The record is inserted.
Example:
SELECT * FROM Student
Executed using:
executeQuery()
Results are returned through ResultSet.
Example:
UPDATE Student
SET Course='Spring Boot'
WHERE Student_ID=101
Execute:
statement.executeUpdate(sql);
The record is updated.
Example:
DELETE FROM Student
WHERE Student_ID=101
Execute:
statement.executeUpdate(sql);
The record is removed.
PreparedStatement is an improved version of Statement.
Example:
PreparedStatement ps =
connection.prepareStatement(
"SELECT * FROM Student WHERE Student_ID=?"
);
Set parameter:
ps.setInt(1,101);
Execute:
ResultSet rs =
ps.executeQuery();
PreparedStatement is preferred in professional applications.
Queries can be reused.
Protects against SQL Injection.
Supports parameterized queries.
Reduces query complexity.
These benefits make PreparedStatement the preferred choice.
SQL Injection is a security vulnerability.
Unsafe code:
String query =
"SELECT * FROM users WHERE username='"
+ username + "'";
Attackers may manipulate input.
PreparedStatement prevents this issue.
This is one reason why PreparedStatement is widely used.
ResultSet stores query results.
Example:
ResultSet rs
Methods:
next()
Move to next row.
getInt()
Retrieve integer values.
getString()
Retrieve text values.
ResultSet is fundamental to JDBC programming.
Database operations can fail.
Example:
try {
}
catch(SQLException e) {
}
Common causes:
Proper exception handling improves reliability.
A transaction is a group of operations treated as a single unit.
Example:
Debit Account
Credit Account
Both operations must succeed together.
Default:
connection.setAutoCommit(true);
Every statement commits automatically.
Disable auto commit:
connection.setAutoCommit(false);
Commit:
connection.commit();
Rollback:
connection.rollback();
Transactions ensure data consistency.
JDBC is used in:
Transaction Processing
Order Management
Patient Records
Employee Data
Student Management
Enterprise software relies heavily on database connectivity.
Spring Boot simplifies JDBC through:
JdbcTemplate
Example:
jdbcTemplate.query()
Benefits:
Understanding JDBC helps developers understand Spring Boot internals.
Can cause resource leaks.
Reduces security.
Creates maintenance issues.
Leads to application failures.
Avoiding these mistakes improves code quality.
These practices improve performance and security.
Simple and powerful.
Supports multiple databases.
Widely adopted.
Supports custom SQL queries.
Used by Hibernate and Spring Data JPA.
These advantages make JDBC essential for Java developers.
JDBC Connectivity enables Java applications to communicate with relational databases. JDBC provides a standardized way to establish connections, execute SQL queries, process results, and manage transactions.
Key concepts covered include:
Mastering JDBC Connectivity is essential for Java Backend Development, Spring Boot, Hibernate, JPA, enterprise software engineering, and database-driven applications.
JDBC stands for Java Database Connectivity.
JDBC allows Java applications to communicate with relational databases.
A JDBC Driver acts as a translator between Java applications and databases.
PreparedStatement improves performance, security, and maintainability while preventing SQL Injection.
ResultSet is an object that stores and processes query results returned from a database.
Want to explore additional programming and software development topics? Click here for more free courses
WhatsApp us