JDBC (Java Database Connectivity)
Definition
JDBC (Java Database Connectivity) is a standard Java API used to connect Java applications with
relational databases such as MySQL, Oracle, and PostgreSQL. It allows programs to execute SQL
queries and retrieve or update data.
Architecture
Java Application → JDBC API → JDBC Driver → Database.
Steps to Connect to MySQL
1. Import [Link] package.
2. Load JDBC driver (modern drivers auto-load).
3. Create Connection.
4. Create Statement/PreparedStatement.
5. Execute SQL.
6. Process ResultSet.
7. Close resources.
Example Code
import [Link].*;
public class JDBCExample {
public static void main(String[] args){
String url="jdbc:mysql://localhost:3306/studentdb";
String user="root";
String pass="password";
try{
Connection con=[Link](url,user,pass);
Statement st=[Link]();
ResultSet rs=[Link]("SELECT * FROM students");
while([Link]()){
[Link]([Link]("id")+" "+[Link]("name"));
}
[Link](); [Link](); [Link]();
}catch(SQLException e){ [Link](); }
}
}
Advantages
Database independent API, secure with PreparedStatement, easy CRUD operations.
Important Interfaces
DriverManager, Connection, Statement, PreparedStatement, ResultSet.
Exam Questions
What is JDBC? List JDBC steps. Difference between Statement and PreparedStatement. What is
ResultSet?