Java Database Connectivity (JDBC)
Chapter 14: Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC) is a Java API that enables Java programs to interact with relational databases. It provides methods to query and update data in a database using Java.
🔍 What is JDBC?
JDBC stands for Java Database Connectivity. It is a set of Java APIs that allow Java applications to connect and execute queries with databases like MySQL, Oracle, PostgreSQL, etc.
🛠 Features of JDBC
- Platform-independent database access
- Standard Java API
- Supports both procedural and object-oriented access
- Connects with multiple types of databases
🧱 JDBC Architecture
The JDBC architecture consists of two layers:
- JDBC API – Provides the application-to-JDBC Manager connection
- JDBC Driver API – Provides the JDBC Manager-to-Driver connection
🔗 JDBC Components
- DriverManager – Manages a list of database drivers.
- Connection – Interface for a connection to a specific database.
- Statement – Used to execute SQL queries.
- ResultSet – Represents the result returned from a query.
- SQLException – Handles database errors.
🪜 Steps to Connect Java with a Database using JDBC
- Load the JDBC Driver
- Establish the connection
- Create a statement
- Execute the query
- Process the results
- Close the connection
💡 Example: Connect Java to MySQL Database
import java.sql.*;
public class JdbcExample {
public static void main(String[] args) {
try {
// Load Driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Establish Connection
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testdb", "root", "password");
// Create Statement
Statement stmt = con.createStatement();
// Execute Query
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
// Process Result
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2));
}
// Close Connection
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
✔️ Best Practices
- Always close connections in a finally block
- Use PreparedStatement to avoid SQL injection
- Use connection pooling for efficiency
- Handle exceptions properly
📌 JDBC Driver Types
- Type-1: JDBC-ODBC bridge driver
- Type-2: Native-API driver
- Type-3: Network Protocol driver
- Type-4: Thin driver (pure Java driver)
❓ FAQs with Answers
Q1: What is JDBC in Java?
A: JDBC stands for Java Database Connectivity. It is a Java API used to connect and interact with databases from Java programs.
Q2: How many types of JDBC drivers are there?
A: There are 4 types: Type-1 (JDBC-ODBC Bridge), Type-2 (Native-API), Type-3 (Network Protocol), and Type-4 (Thin Driver).
Q3: What is the difference between Statement and PreparedStatement?
A: Statement is used for static SQL queries, while PreparedStatement is used for dynamic and parameterized queries and provides protection from SQL injection.
Q4: Which JDBC driver is best for MySQL?
A: The Type-4 Thin driver (`com.mysql.cj.jdbc.Driver`) is most commonly used for connecting Java applications to MySQL.
Q5: How to handle exceptions in JDBC?
A: Use try-catch blocks to handle SQLExceptions, and always ensure the connection is closed in a `finally` block to prevent memory leaks.
🏁 Conclusion
JDBC is a powerful API that enables Java developers to connect and manipulate relational databases. Understanding JDBC is essential for developing enterprise-level Java applications.
Labels: Java Programming, JDBC, Java with Database, JDBC Tutorial, Java Chapter 14📌 Related Links: Complete Java Course
- Chapter 1: Introduction to Java
- Chapter 2: Java Environment Setup
- Chapter 3: Variables and Data Types
- Chapter 4: Operators in Java
- Chapter 5: Control Statements
- Chapter 6: Loops in Java
- Chapter 7: Arrays and Strings
- Chapter 8: Object-Oriented Programming (OOP)
- Chapter 9: Inheritance and Polymorphism
- Chapter 10: Exception Handling
- Chapter 11: Multithreading
- Chapter 12: File Handling in Java
- Chapter 13: Applets and GUI (AWT/Swing)
- Chapter 14: Java Project Ideas & Practice Questions
Comments
Post a Comment