JDBC Interview Questions and Answers

JDBC Interview Questions and Answers

1 what is JDBC?

Ans: JDBC is a layer of abstraction that allows users to choose between databases. JDBC allows you to write database applications in Java without having to concern yourself with the underlying details of a particular database.

2 How many types of JDBC Drivers are present and what are they?

Ans: There are 4 types of JDBC Drivers Type 1: JDBC-ODBC Bridge Driver Type 2: Native API Partly Java Driver Type 3: Network protocol Driver Type 4: JDBC Net pure Java Driver

3 Explain the role of Driver in JDBC?

Ans: The JDBC Driver provides vendor-specific implementations of the abstract classes provided by the JDBC API. Each vendor?s driver must provide implementations of the java.sql.Connection, Statement, PreparedStatement, Callable Statement, ResultSet and Driver.

4 Is java.sql.Driver a class or an Interface?

Ans: It’s an interface.

5 Is java.sql.DriverManager a class or an Interface?

Ans: It’s a class. This class provides the static get Connection method, through which the database connection is obtained.

6 Is java.sql.Connection a class or an Interface?

Ans: java.sql.Connection is an interface. The implmentation is provided by the vendor specific Driver.

7 Is java.sql.Statement a class or an Interface

Ans: java.sql.Statement, java.sql.PreparedStatement and java.sql.Callable Statement are interfaces.

8 which interface do Prepared Statement extend?

Ans:java.sql.Statement

9 which interface do Callable Statement extend?

Ans: Callable Statement extends Prepared Statement.

10 what is the purpose Class.forName (“”) method?

Ans: The Class.forName (“”) method is used to load the driver.

11 Do you mean that Class.forName (“”) method can only be used to load a driver?

Ans: The Class.forName (“”) method can be used to load any class, not just the
database vendor driver class..

12 which statement throws ClassNotFoundException in SQL code block? and why?

Ans: Class.for Name (“”) method throws ClassNotFoundException. This exception is thrown when the JVM is not able to find the class in the class path.

13 what exception does Class.for Name () throw?

Ans: ClassNotFoundException.

14 what is the return type of Class.for Name () method?

Ans: java.lang.Class

15 Can an Interface be instantiated?

Ans: If not, justify and explain the following line of code:

Connection con = DriverManager.getConnection (“dsd”,”sds”,”adsd”);
An interface cannot be instantiated. But reference can be made to a interface. When a reference is made to interface, the refered object should have implemented all the abstract methods of the interface. In the above mentioned line, DriverManager.getConnection method returns Connection Object with implementation for all abstract methods.