JDBC Interview Questions and Answers for internship

JDBC Internship Interview Questions and Answers
  1. What is JDBC?

    • Answer: JDBC (Java Database Connectivity) is an API that allows Java programs to interact with relational databases. It provides a standard way to execute SQL statements, query data, and update databases.
  2. What are the core interfaces of JDBC?

    • Answer: The core interfaces include `Connection`, `Statement`, `PreparedStatement`, `CallableStatement`, `ResultSet`, and `DriverManager`.
  3. Explain the role of the `DriverManager` class.

    • Answer: `DriverManager` manages a list of registered JDBC drivers. It's used to establish a connection to a database using the `getConnection()` method, which takes a JDBC URL, username, and password as arguments.
  4. What is a JDBC driver? Name some common types.

    • Answer: A JDBC driver is a software component that allows Java applications to connect to and interact with a specific database system. Common types include Type 1 (JDBC-ODBC Bridge), Type 2 (Native-API), Type 3 (Network Protocol), and Type 4 (Thin Driver).
  5. What is the difference between `Statement`, `PreparedStatement`, and `CallableStatement`?

    • Answer: `Statement` executes simple SQL queries. `PreparedStatement` is used for parameterized queries, improving performance and security by preventing SQL injection. `CallableStatement` executes stored procedures.
  6. Explain the use of `ResultSet`.

    • Answer: `ResultSet` holds the data retrieved from a database query. It provides methods to navigate through the result set (e.g., `next()`, `getInt()`, `getString()`), accessing the data row by row.
  7. How do you handle database exceptions in JDBC?

    • Answer: Use `try-catch` blocks to handle `SQLExceptions`. Always close database resources (connections, statements, result sets) in a `finally` block to prevent resource leaks.
  8. What is a connection pool and why is it used?

    • Answer: A connection pool is a technique that pre-creates a set of database connections, which are reused by applications to reduce the overhead of establishing new connections each time.
  9. How do you prevent SQL injection?

    • Answer: Use parameterized queries (`PreparedStatement`) instead of directly concatenating user input into SQL strings. This prevents malicious code from being executed.
  10. What is the JDBC URL format? Give an example.

    • Answer: The general format is `jdbc::`. An example for MySQL is: `jdbc:mysql://localhost:3306/mydatabase`.
  11. Explain the different transaction isolation levels in JDBC.

    • Answer: JDBC supports various isolation levels (e.g., `TRANSACTION_READ_UNCOMMITTED`, `TRANSACTION_READ_COMMITTED`, `TRANSACTION_REPEATABLE_READ`, `TRANSACTION_SERIALIZABLE`) that control how transactions interact and see changes made by other transactions.
  12. How do you perform batch updates in JDBC?

    • Answer: Use the `addBatch()` method of `Statement` or `PreparedStatement` to add multiple SQL statements to a batch. Then, execute the batch using `executeBatch()`.
  13. What are stored procedures and how do you call them using JDBC?

    • Answer: Stored procedures are pre-compiled SQL code stored in the database. You call them using `CallableStatement`.
  14. How do you handle large result sets efficiently in JDBC?

    • Answer: Use techniques like fetching data in batches (`setFetchSize()`) or using cursors to process large result sets incrementally instead of loading everything into memory at once.
  15. What is the difference between `executeUpdate()` and `executeQuery()`?

    • Answer: `executeUpdate()` is used for SQL statements that modify data (INSERT, UPDATE, DELETE). `executeQuery()` is used for SQL statements that retrieve data (SELECT).

Thank you for reading our blog post on 'JDBC Interview Questions and Answers for internship'.We hope you found it informative and useful.Stay tuned for more insightful content!