What is Java JDBC and how does it work

๐Ÿ’ก Concept: Java JDBC

JDBC (Java Database Connectivity) is a Java API that allows Java applications to interact with relational databases using SQL queries.

๐Ÿ“˜ Quick Intro

JDBC provides classes and interfaces to connect to databases, send queries, and retrieve results. It's part of Java SE and supports all major databases via JDBC drivers.

๐Ÿง  Analogy

Think of JDBC as a phone line between your Java app and a database. You dial (connect), speak (send SQL), and listen (get results). JDBC is the protocol that enables this conversation.

๐Ÿ”ง Technical Explanation

  • JDBC uses DriverManager to load the database driver.
  • Connection object represents an active connection to the database.
  • Statement or PreparedStatement is used to run SQL queries.
  • ResultSet is used to iterate over results.
  • All JDBC resources must be closed to prevent leaks (can use try-with-resources).

๐ŸŽฏ Use Cases

  • โœ… Connecting Java apps to MySQL, PostgreSQL, Oracle, etc.
  • โœ… Executing SQL queries: SELECT, INSERT, UPDATE, DELETE
  • โœ… Processing and displaying database results in GUIs or APIs

๐Ÿ’ป Example: JDBC Connection


import java.sql.*;

public class JdbcExample {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/testdb";
        String user = "root";
        String password = "root123";

        try (Connection conn = DriverManager.getConnection(url, user, password);
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT * FROM users")) {

            while (rs.next()) {
                System.out.println(rs.getString("username"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

โ“ Interview Q&A

Q1: What is JDBC?
A: JDBC is a Java API to interact with relational databases using SQL.

Q2: What are the main interfaces in JDBC?
A: Connection, Statement, PreparedStatement, ResultSet.

Q3: How do you connect to a database using JDBC?
A: Use DriverManager.getConnection() with URL, username, and password.

Q4: What is DriverManager?
A: A class that manages JDBC drivers and connections.

Q5: Difference between Statement and PreparedStatement?
A: PreparedStatement is precompiled and supports parameters.

Q6: How to prevent SQL injection?
A: Use PreparedStatement instead of Statement.

Q7: What is ResultSet used for?
A: To retrieve and iterate over query results.

Q8: What are JDBC drivers?
A: Libraries that implement JDBC API to talk to a specific database.

Q9: What must be closed in JDBC?
A: Connection, Statement, and ResultSet.

Q10: Is JDBC part of Java SE?
A: Yes.

๐Ÿ“ MCQs

Q1. What does JDBC stand for?

  • Java Direct Connect
  • Java Database Connectivity
  • Java Data Compiler
  • Join DB Client

Q2. Which interface represents a DB connection?

  • Statement
  • ResultSet
  • Connection
  • Driver

Q3. Which class loads the database driver?

  • DataLoader
  • DriverManager
  • DBConnector
  • SqlHandler

Q4. How to prevent SQL injection?

  • Use Statement
  • Use ResultSet
  • Use PreparedStatement
  • Disable SQL logs

Q5. Which object holds query results?

  • Statement
  • ResultSet
  • Connection
  • PreparedStatement

Q6. What method is used to connect to DB?

  • open()
  • init()
  • getConnection()
  • fetch()

Q7. Which of these is a JDBC driver type?

  • Type A
  • Type 2.5
  • Type 4
  • JavaX

Q8. Where should JDBC resources be closed?

  • Never
  • Only in catch
  • In finally or try-with-resources
  • In constructor

Q9. Can JDBC work with Oracle DB?

  • No
  • Only with MySQL
  • Yes
  • Only with SQLite

Q10. Which JDBC method runs SELECT queries?

  • run()
  • execute()
  • executeQuery()
  • get()

๐Ÿ’ก Bonus Insight

JDBC can be used with connection pooling libraries like HikariCP to improve performance in large-scale applications.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: