Monday, July 2, 2012

Java Database Connectivity JDBC Interview Questions Part 2

As the article on JDBC Interview Questions part 1 was becoming too long, I stopped with 25 questions. So, now in this article, we are going to look at some more questions on the topic of Java Database Connectivity.

Before we proceed, you can re-visit the article titled Java Database Connectivity - JDBC if you want to refresh your memory on JDBC concepts.

Questions – Part 2:

1. How will you retreive database warnings from a Connection object in JDBC?


//Retrieving warning from connection object
SQLWarning warning = conn.getWarnings();

//Retrieving next warning from warning object itself
SQLWarning nextWarning = warning.getNextWarning();


2. How will you retreive database warnings from a Statement object in JDBC?


//Retrieving warning from statement object
stmt.getWarnings();

//Retrieving next warning from warning object itself
SQLWarning nextWarning = warning.getNextWarning();



3. How will you retreive database warnings from a ResultSet object in JDBC?

//Retrieving warning from resultset object
rs.getWarnings();

//Retrieving next warning from warning object itself
SQLWarning nextWarning = warning.getNextWarning();


4. What does the clearWarnings() method do?

A call to clearWarnings() method clears all warnings reported for this object. After a call to this method, the method getWarnings returns null until a new warning is reported for this object.

5. What happens when I try to call the getWarning() method on a connection/statement/resultset after it has been closed?

Trying to call the getWarning() method on either of these 3 objects after they are closed will cause an SQLException to be thrown.

6. Let us say that I just closed my Statement object so, I cannot access the getWarning() on my statement. Can I still access the getWarning() on my ResultSet?

No. Closing a Statement automatically closes the ResultSet connected to it. So, you will get the same SQLException if you try to do so.

7. What is DatabaseMetaData?

JDBC API has 2 Metadata interfaces DatabaseMetaData & ResultSetMetaData. The DatabaseMetaData provides Comprehensive information about the database as a whole. This interface is implemented by driver vendors to let users know the capabilities of a Database Management System (DBMS) in combination with the driver based on JDBC technology ("JDBC driver") that is used with it. Use DatabaseMetaData to find information about your database, such as its capabilities and structure.

8. How will you use the DatabaseMetaData? Can you write a sample example code?


DatabaseMetaData md = conn.getMetaData();
System.out.println("Database Name: " + md.getDatabaseProductName());
System.out.println("Database Version: " + md.getDatabaseProductVersion());
System.out.println("Driver Name: " + md.getDriverName());
System.out.println("Driver Version: " + md.getDriverVersion());


9. What is ResultSetMetaData?

JDBC API has 2 Metadata interfaces DatabaseMetaData & ResultSetMetaData. The ResultSetMetaData is an object that can be used to get information about the types and properties of the columns in a ResultSet object. Use ResultSetMetaData to find information about the results of an SQL query, such as size and types of columns.

10. How will you use the ResultSetMetaData? Can you write a sample example code?


ResultSet rs = stmt.executeQuery("SELECT * FROM TABLE_NAME");
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
boolean b = rsmd.isSearchable(1);



11. What is rowset?

A RowSet is an object that encapsulates a set of rows from either Java Database Connectivity (JDBC) result sets or tabular data sources like a file or spreadsheet.

12. Why do we need a RowSet?

RowSet is a interface that adds support to the JDBC API for the JavaBeans component model. A rowset, which can be used as a JavaBeans component in a visual Bean development environment, can be created and configured at design time and executed at run time. The RowSet interface provides a set of JavaBeans properties that allow a RowSet instance to be configured to connect to a JDBC data source and read some data from the data source. A group of setter methods (setInt, setBytes, setString, and so on) provide a way to pass input parameters to a rowset's command property. This command is the SQL query the rowset uses when it gets its data from a relational database, which is generally the case. Rowsets are easy to use since the RowSet interface extends the standard java.sql.ResultSet interface so it has all the methods of ResultSet

13. What are the advantages of using RowSet over ResultSet?

There are two clear advantages of using RowSet over ResultSet:

* RowSet makes it possible to use the ResultSet object as a JavaBeans component.
* RowSet be used to make a ResultSet object scrollable and updatable. All RowSet objects are by default scrollable and updatable. If the driver and database being used do not support scrolling and/or updating of result sets, an application can populate a RowSet object implementation (e.g. JdbcRowSet) with the data of a ResultSet object and then operate on the RowSet object as if it were the ResultSet object.

14. What are the different types of RowSet ?
There are two types of RowSet are there. They are:
* Connected - A connected RowSet object connects to the database once and remains connected until the application terminates.
* Disconnected - A disconnected RowSet object connects to the database, executes a query to retrieve the data from the database and then closes the connection. A program may change the data in a disconnected RowSet while it is disconnected. Modified data can be updated in the database after a disconnected RowSet reestablishes the connection with the database.

15. Can you give an example of Connected RowSet?

A JdbcRowSet object is a example of connected RowSet, which means it continually maintains its connection to a database using a JDBC technology-enabled driver.

16. Can you give an example of Disconnected RowSet?

A CachedRowSet object is a example of disconnected rowset, which means that it makes use of a connection to its data source only briefly. It connects to its data source while it is reading data to populate itself with rows and again while it is propagating changes back to its underlying data source. The rest of the time, a CachedRowSet object is disconnected, including while its data is being modified. Being disconnected makes a RowSet object much leaner and therefore much easier to pass to another component. For example, a disconnected RowSet object can be serialized and passed over the wire to a thin client such as a personal digital assistant (PDA).

17. What are the benefits of having JdbcRowSet implementation?

The JdbcRowSet implementation is a wrapper around a ResultSet object that has following advantages over ResultSet
* This implementation makes it possible to use the ResultSet object as a JavaBeans component. A JdbcRowSet can be used as a JavaBeans component in a visual Bean development environment, can be created and configured at design time and executed at run time.
* It can be used to make a ResultSet object scrollable and updatable. All RowSet objects are by default scrollable and updatable. If the driver and database being used do not support scrolling and/or updating of result sets, an application can populate a JdbcRowSet object with the data of a ResultSet object and then operate on the JdbcRowSet object as if it were the ResultSet object.

18. What is the need of BatchUpdates feature in JDBC?

The BatchUpdates feature allows us to group SQL statements together and send to database server in one single shot instead of multiple calls.

19. What is a DataSource?

A DataSource object is the representation of a source of data in the Java programming language.

In basic terms:
* A DataSource is a facility for storing data.
* DataSource can be referenced by JNDI.
* Data Source may point to RDBMS, file System , any DBMS etc..

Typically in enterprise application perspective, the term DataSource can be used interchangeably with a RDBMS database because in almost all cases, our DataSource will be pointing to an RDBMS database.

20. What are the advantages of DataSource?

The few advantages of using data source are :
* An application does not need to hardcode driver information, as it does with the DriverManager.
* The DataSource implementations can easily change the properties of data sources. For example: There is no need to modify the application code when making changes to the database details.
* The DataSource facility allows developers to implement a DataSource class to take advantage of features like connection pooling and distributed transactions.

21. What is the difference between a Statement and a PreparedStatement?

Some of the main differences between a statement & PreparedStatement are:

Statement PreparedStatment
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. A prepared statement has to verify its 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.
22. What’s the difference between TYPE_SCROLL_INSENSITIVE and TYPE_SCROLL_SENSITIVE in ResultSets?

Some of the main differences between a TYPE_SCROLL_INSENSITIVE & TYPE_SCROLL_SENSITIVE are:

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.
If you have any questions that you want answer for - please leave a comment on this page and I will answer them.

If you have any more questions on JDBC that you have faced during your interviews and wish to add them to this collection - pls drop a note to anandvijayakumar007@gmail.com and I shall be glad to add them to this list.

JDBC Interview Questions - Part 1

Java/J2EE Interview Questions - Home Page

5 comments:

  1. Can we use non-parametrized queries in PreparedStatmets? If yes, then why do we really need Statements?

    ReplyDelete
    Replies
    1. Yes you can.

      It is the parent class based on which the prepared statement is built. Without statement there can be no prepared statement. So it is needed.

      Delete
  2. Can you change your blog background picture to a
    more stable color, it very dificult to read the text this way

    ReplyDelete
  3. nice sir . . Awesome post . Me checking other post now

    ReplyDelete

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers