JDBC Interview Questions



1) What is JDBC?
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?
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?
The JDBC Driver provides vendor-specific implementations of the abstract classes provided by the JDBC API. Each vendors driver must provide implementations of the java.sql.Connection,Statement,PreparedStatement, CallableStatement, ResultSet and Driver.

4) Is java.sql.Driver a class or an Interface ?
It's an interface.

5) Is java.sql.DriverManager a class or an Interface ?
It's a class. This class provides the static getConnection method, through which the database connection is obtained.

6) Is java.sql.Connection a class or an Interface ?
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 ?
java.sql.Statement,java.sql.PreparedStatement and java.sql.CallableStatement are interfaces.

8) Which interface do PreparedStatement extend?
java.sql.Statement

9) Which interface do CallableStatement extend?
CallableStatement extends PreparedStatement.

10) What is the purpose Class.forName("") method?
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?
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?
Class.forName("") method throws ClassNotFoundException. This exception is thrown when the JVM is not able to find the class in the classpath.

13) What exception does Class.forName() throw?
ClassNotFoundException.

14) What is the return type of Class.forName() method ?
java.lang.Class

15) Can an Interface be instantiated? 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.

16) What type of a method is getConnection()?
static method.

17) What is the return type for getConnection() method?
Connection object.

18) What is the return type for executeQuery() method?
ResultSet

19) What is the return type for executeUpdate() method and what does the return type indicate?
int. It indicates the no of records affected by the query.

20) What is the return type for execute() method and what does the return type indicate?
boolean. It indicates whether the query executed sucessfully or not.
21) is Resultset a Classor an interface?
Resultset is an interface.

22) What is the advantage of PrepareStatement over Statement?
PreparedStatements are precompiled and so performance is better. PreparedStatement objects can be reused with passing different values to the queries.

23) What is the use of CallableStatement?
CallableStatement is used to execute Stored Procedures.

24) Name the method, which is used to prepare CallableStatement?
CallableStament.prepareCall().

25) What do mean by Connection pooling?
Opening and closing of database connections is a costly excercise. So a pool of database connections is obtained at start up by the application server and maintained in a pool. When there is a request for a connection from the application, the application server gives the connection from the pool and when closed by the application is returned back to the pool. Min and max size of the connection pool is configurable. This technique provides better handling of database connectivity.