JDBC Basics
Most real-world Java applications do not store data only in memory. They interact with databases to store, retrieve, and update information.
JDBC (Java Database Connectivity) is the API that allows Java applications to communicate with databases in a standard and reliable way.
What Is JDBC?
JDBC is a Java API that enables Java programs to:
- Connect to a database
- Execute SQL queries
- Retrieve and manipulate data
- Work with multiple database systems
It acts as a bridge between Java code and the database.
Why JDBC Is Important
Almost every backend system depends on databases. JDBC is the foundation for database access in Java.
It is widely used in:
- Enterprise applications
- Web applications
- Banking and financial systems
- ERP and CRM platforms
How JDBC Works (High-Level)
JDBC follows a simple workflow:
- Load the database driver
- Establish a connection
- Create a statement
- Execute SQL queries
- Process results
- Close the connection
JDBC Architecture
JDBC consists of the following key components:
- Driver – connects Java to the database
- Connection – represents an active database session
- Statement – sends SQL queries to the database
- ResultSet – stores query results
Loading the JDBC Driver
Before connecting to a database, the appropriate JDBC driver must be loaded.
Class.forName("com.mysql.cj.jdbc.Driver");
Modern JDBC versions often load drivers automatically.
Establishing a Database Connection
The DriverManager class is used to create a database connection.
import java.sql.Connection;
import java.sql.DriverManager;
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testdb",
"username",
"password"
);
This connection allows Java to communicate with the database.
Creating a Statement
Once connected, a Statement object is used to execute SQL queries.
import java.sql.Statement;
Statement stmt = con.createStatement();
Executing a Query
You can retrieve data from the database using SQL SELECT queries.
import java.sql.ResultSet;
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
while (rs.next()) {
System.out.println(rs.getInt("id") + " " + rs.getString("name"));
}
Closing JDBC Resources
Always close database resources to avoid memory leaks.
rs.close();
stmt.close();
con.close();
Real-World Example
Consider a Java application fetching employee records from a database.
ResultSet rs = stmt.executeQuery("SELECT name, salary FROM employees");
while (rs.next()) {
System.out.println(rs.getString("name") + " - " + rs.getDouble("salary"));
}
This pattern is used across enterprise applications.
Common JDBC Errors
- Incorrect database URL
- Wrong username or password
- Missing JDBC driver
- SQL syntax errors
Best Practices
- Always close connections properly
- Use PreparedStatement for security (covered next)
- Avoid hardcoding credentials
- Handle SQL exceptions carefully
Key Takeaways
- JDBC connects Java applications to databases
- It is the foundation of data-driven Java apps
- Used heavily in enterprise systems
In the next lesson, we will move deeper into JDBC and implement CRUD Operations.