JDBC interview questions and answers
1.
What is the JDBC?
A.
Java Database Connectivity (JDBC) is a standard Java API to interact with
relational database 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.
2.
What are the new features added to JDBC 4.0?
A.
The major features added in JDBC 4.0 includes:
• Connection
management enhancements
• Support
for RowId SQL type
• provides
SQL DataSet implementation using annotations
• SQL
exception handling enhancements
• SQL
XML support
3.
Explain basic steps in writing a java program using JDBC?
A.
JDBC makes the interaction with RDBMS simple and interactive. When a java
application needs to access database,
- Loads RDBMS specific JDBC driver because this driver actually communicates with the database.
- Opens the connection to database which is then used to send SQL statements and get results back.
- Create JDBC statements object. This object contains SQL query.
- Execute statement which returns ResultSet. ResultSet contains couple of database table as a result of SQL query.
- Process the ResultSet.
- Close the connection.
4.
Explain the JDBC architecture?
A.
JDBC architecture consists of two layers:
- The JDBC API, which will provides the application-to-JDBC Manager connection.
- The JDBC Driver API, which supports JDBC Manager-to-Driver Connection.
The
JDBC API uses a driver manager and database-specific drivers to provide
transparent connectivity to heterogeneous database. The JDBC driver manager
ensures that the correct driver is used access each DataSource. The driver
manager is capable of supporting multiple concurrent drivers connected to
multiple heterogeneous databases.
5.
What are the main components of JDBC?
A.
The life cycle of a JDBC consists of the following phases,
- DriverManager: manages a list of database drivers. Matches connection requests from Java application with the proper database driver using communication sub-protocol. The first driver that recognizes certain sub-protocol under JDBC will be used to establish a database connection.
- Driver: The database communication link, handling all communications with databases. Normally, once the driver is loaded, developer need not call it explicitly.
- Connection: Interface with all methods for contacting the database. The connection object represents communication context, i.e., all communications with database is through connection object only.
- Statement: Encapsulates SQL statements which are passed to the database to be parsed, complied, planned and executed.
- ResultSet: ResultSet represents set of row's retrieved due to query execution.
6.
How the JDBC application works?
A.
JDBC application can be logically divided into two layers:
1.
Driver layer
2.
Application layer
- Driver layer consists of the DriverManager class and the available JDBC drivers.
- An application begins with requesting the DriverManager for the connection.
- An appropriate driver is choosen and is used for establishing the connection. connection is given to the application which fails under the application layer.
- The application uses the connection to create statement king of objects, through which SQL commands are sent to backend and obtain the results.
7.
How do I load a database driver with Java/JDBC 4.0 ?
A.
JDBC provides the jar file containing the driver is properly configured; just
place the jar file in the classpath. Java developer no longer need to
explicitly load JDBC drivers using code, like Class.forName() to register with
JDBC driver. The DriverManager class takes care of this by automatically
locating a suitable driver when the DriverManager.getConnection() method
called. This feature is backward-compatible, So no changes are needs to the
existing JDBC code.
8.
What is JDBC Driver interface?
A.
JDBC Driver interface provides vendor-specific implementations of the abstract
classes provided by the JDBC API. Each vendor driver must provide
implementation of the Connection, Statement, PreparedStatement,
CallableStatement, ResultSet and Driver which will present under the java.sql
path.
9.
What does the Connection object represents?
A.
The connection object represents the communications context, i.e., all
communications with database is through connection object only.
10.
What is Statement?
A.
Statement act like a vehicle through which SQL commands can sent, Through the
connection object we create statement kind of objects.
Statement
stmt = conn.createStatement();
This method returns object we create
statement king of objects.
11.
What is PreparedStatement?
A.
PreaptedStatement is an SQL statement that is precompiled by the database.
Through pre-compilation, PreapredStatement improves the performance of SQL
commands that are executed multiple times. Once compiled, PreparedStatement can
be customized prior to each execution by altering predefined SQL parameters.
PreparedStatement
pst = conn.prepareStatement("UPDATE EMP SET SALARY=? WHERE ID=?");
pst.setBigDecimal(1,100000.00);
pst.setInt(2,1545);
Here,
conn is an instance of the Connection class and “?” represents parameters.
These parameters must be specified before execution.
12.What is the difference between a Statement
and PreparedStatement?
A:
Statement
|
PreparedStatement
|
A standard
statement is used to create a Java representation of a literal SQL statement
and execute it on the database.
|
A
PreparedStatement is a precompiled statement. This means that when the
PreparedStatement is executed, the RDBMS can just run the PreparedStatement
SQL statement without having to compile it first.
|
Statement has to
verify its metadata against the database every time.
|
While a
PreparedStatement has to verify it metadata against the database only once.
|
If you want to
execute the SQL statement once go for STATEMENT.
|
If you want to
execute a single SQL statement multiple number of times, then go for
PREPAREDSTATEMENT. PreparedStatement objects can be reused with passing
different values to the queries.
|
13.
What are callable statements?
A.
CallableStatement are used from JDBC application to invoke stored procedures
and functions.
14.
How to call a stored procedure from JDBC?
A.
PL/SQL stored procedures are called from with in 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 and object instance of the CallableStatement class. The following
line of code illustrates this,
CallableStatement
cst = conn.prepareCall("{call proname(?,?,?)}");
15.
What are types of JDBC driver?
A.
There are four types of drivers defined by JDBC as follows,
- Type 1: JDBC/ODBC – these require an ODBC (Open DataBase Connectivity) driver for the database to be installed. This driver works by translating the submitted queries into equivalent ODBC queries and forwards them via native API calls directly to ODBC driver. This provides no host redirection capability.
- Type2: Native API (partly-java driver) – this type of driver uses a vendor-specific driver or database API to interact with the database. For an example of such an API is Oracle OCI (Oracle Call Interface). This is also provides no host redirection.
- Type3: Open Protocol-Net – This is not vendor specific and works by forwarding database requests to a remote database source using a net-server component. Net-server component accesses the database is transparent to the client. Client driver communicates with the net server using a database-independent protocol and the server translates this protocol into database calls. Type3 driver can access any database.
- Type4: Proprietary Protocol-Net (Pure Java driver) – this has a same configuration as a type3 driver but uses a wire protocol specific to a particular vendor and hence can access only that vendor database. Again this all transparent to the client.
16.
Which type of JDBC driver is the fastest one?
A.
JDBC Net pure java driver (Type4) is the fastest driver because it converts the
JDBC calls into vendor specific protocol calls and it directly interacts with
the database.
17.
Does JDBC-ODBC Bridge support multiple concurrent open statements per
connection?
A.
You can open only one statement object per connection when you are using the
JDBC-ODBC Bridge.
18.
Which is the right type of driver to use and when?
- Type1 driver is handy for prototyping.
- Type2 driver adds security, caching and connection control.
- Type3 and Type4 drivers need no pre-installation.
19.
What are the standard isolation levels defined by JDBC?
A.
The values are declared in the class java.sql.Connection and are,
- TRANSACTION_NONE
- TRANSACTION_READ_COMMITTED
- TRANSACTION_READ_UNCOMMITTED
- TRANSACTION_REPEATABLE_READ
- TRANSACTION_SERIALIZABLE
20.
What is ResultSet?
A.
ResultSet represents set of rows retrieved due to query execution.
ResultSet
rs = st.executeQuery(sqlQuery);
21.
What are the types of ResultSet?
A.
The values are defined in Connection class, that are
- TYPE_FORWARD_ONLY specifies that the resultset is not scrollable, rows with in it can be advanced only in the forward direction. This is always insensitive.
- TYPE_SCROLL_INSENSITIVE specifies that the resultset is scrollable in either direction but is insensitive to changes committed by other transactions or other statements in the same transaction.
- TYPE_SCROLL_SENSITIVE specifies that the resultset is scrollable in either direction and is affected by changes committed by other transaction or statement within the same transaction.
21.What’s the difference between
TYPE_SCROLL_INSENSITIVE and TYPE_SCROLL_SENSITIVE?
TYPE_SCROLL_INSENSITIVE
|
TYPE_SCROLL_SENSITIVE
|
An insensitive
resultset is like the snapshot of the data in the database when query was
executed.
|
A sensitive
resultset does NOT represent a snapshot of data, rather it contains points to
those rows which satisfy the query condition.
|
After we get the
resultset the changes made to data are not visible through the resultset, and
hence they are known as insensitive.
|
After we obtain
the resultset if the data is modified then such modifications are visible
through resultset.
|
Performance not
effected with insensitive.
|
Since a trip is
made for every ‘get’ operation, the performance drastically get affected.
|
22.
What is RowSet?
A.
A rowset is an object that encapsulates a set of rows from either Java Database
Connectivity ResultSet or tabular data sources like a file or spreadsheet.
Rowset support component-based development models like JavaBean, with a
standard set of properties and an event notification mechanism.
24.
What are the different types of RowSet?
A.
There are two types of RowSet are there. That are,
- Connected – A connected RowSet object connects to the database once and remains connected until the application terminates.
- Disconnected – A disconnected RowSet object to database, executes the query to retrieve data from the database and then closes the connection. The program may change the data in a disconnected RowSet while disconnecting. Modified data can be updated in the database after a disconnected RowSet re-establishes the connection with the database.
25.
What is the need of BatchUpdates?
A.
The BatchUpdates features allow us to group SQL statements together and send to
database server in one single trip.
26.
What is a DataSource?
A.
DataSource object is representation of the data source in the Java programming
language. In basic terms,
- DataSource is the facility to store data.
- DataSource can be refereced by JNDI.
- DataSource may points to RDBMS, file system, any DBMS etc,.
27.
What are the advantages of DataSource?
A.
Few advantages of using DataSource. That are,
- An implementation does not need to hardcore driver information, as it does with the DriverManager.
- A DataSource implementation can easily change the properties of Data Source. For example: there is no need to modify application code when making changes to the database details.
- DataSource facility allow developers to implement a DataSource class to take advantages of features like connection pooling and distributed transactions.
28.
What is connection pooling? What is the main advantage of using connection
pooling?
A.
A connection pool is mechanism to reuse connections created. Connection pooling
can increase the performance dramatically by reusing connection rather than
creating a new physical connection each time a connection is requested.
Comments
Post a Comment