JDBC Interview Questions and Answers
Question - 1 : - What Is the Jdbc?
Answer - 1 : -
Java Database Connectivity (JDBC) is a standard Java API to interact with relational databases form Java. JDBC has set of classes and interfaces which can use from Java application and talk to database without learning RDBMS details and using Database Specific JDBC Drivers.
Question - 2 : - What Are The New Features Added To Jdbc 4.0?
Answer - 2 : -
The major features added in JDBC 4.0 include :
- Auto-loading of JDBC driver class
- Connection management enhancements
- Support for RowId SQL type
- DataSet implementation of SQL using Annotations
- SQL exception handling enhancements
- SQL XML support
Question - 3 : - Explain Basic Steps In Writing A Java Program Using Jdbc?
Answer - 3 : -
JDBC makes the interaction with RDBMS simple and intuitive. When a Java application needs to access database :
- Load the RDBMS specific JDBC driver because this driver actually communicates with the database (Incase of JDBC 4.0 this is automatically loaded).
- Open the connection to database which is then used to send SQL statements and get results back.
- Create JDBC Statement object. This object contains SQL query.
- Execute statement which returns resultset(s). ResultSet contains the tuples of database table as a result of SQL query.
- Process the result set.
- Close the connection.
Question - 4 : - What Are The Main Components Of Jdbc ?
Answer - 4 : -
The life cycle of a servlet consists of the following phases:
- DriverManager: Manages a list of database drivers. Matches connection requests from the java application with the proper database driver using communication subprotocol. The first driver that recognizes a certain subprotocol under JDBC will be used to establish a database Connection.
- Driver: The database communications link, handling all communication with the database. Normally, once the driver is loaded, the developer need not call it explicitly.
- Connection : Interface with all methods for contacting a database.The connection object represents communication context, i.e., all communication with database is through connection object only.
- Statement : Encapsulates an SQL statement which is passed to the database to be parsed, compiled, planned and executed.
- ResultSet: The ResultSet represents set of rows retrieved due to query execution.
Question - 5 : - What Is Jdbc Driver Interface?
Answer - 5 : -
The JDBC Driver interface provides vendor-specific implementations of the abstract classes provided by the JDBC API. Each vendor driver must provide implementations of the java.sql.Connection, Statement, Prepared Statement, CallableStatement, ResultSet and Driver.
Question - 6 : - What Does The Connection Object Represents?
Answer - 6 : -
The connection object represents communication context, i.e., all communication with database is through connection object only.
Question - 7 : - What Is Statement ?
Answer - 7 : -
- Statement acts like a vehicle through which SQL commands can be sent. Through the connection object we create statement kind of objects.
- Through the connection object we create statement kind of objects.
- Statement stmt = conn.createStatement();
- This method returns object which implements statement interface.
Question - 8 : - What Is Preparedstatement?
Answer - 8 : -
A prepared statement is an SQL statement that is precompiled by the database. Through precompilation, prepared statements improve the performance of SQL commands that are executed multiple times (given that the database supports prepared statements). Once compiled, prepared statements can be customized prior to each execution by altering predefined SQL parameters.
PreparedStatement pstmt = conn.prepareStatement("UPDATE EMPLOYEES SET SALARY = ? WHERE ID = ?");
pstmt.setBigDecimal(1, 153833.00);
pstmt.setInt(2, 110592);
Here: conn is an instance of the Connection class and "?" represents parameters.These parameters must be specified before execution.
Question - 9 : - What Are Callable Statements ?
Answer - 9 : -
Callable statements are used from JDBC application to invoke stored procedures and functions.
Question - 10 : - How To Call A Stored Procedure From Jdbc ?
Answer - 10 : -
PL/SQL stored procedures are called from within JDBC programs by means of the prepareCall() method of the Connection object created. A call to this method takes variable bind parameters as input parameters as well as output variables and creates an object instance of the CallableStatement class. The following line of code illustrates this:
CallableStatement stproc_stmt = conn.prepareCall("{call procname(?,?,?)}");
Here conn is an instance of the Connection class.