JDBC INTERVIEW QUESTIONS

1)What is the JDBC?
Java Database Connectivity (JDBC) is a standard Java API to interact with relational databases form Java.The JDBC driver gives out the connection to the database and implements the protocol for transferring the query and result between client and database.

2)Explain Basic Steps in writing a Java program using JDBC?
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.
For detailed Answer refer JDBC Tutorials--->here

3)What are the main components of JDBC ?
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.
4)How many types of JDBC Drivers are present and what are they?   
JDBC drivers are divided into four types or levels. The different types of jdbc drivers are: 
Type 1: JDBC-ODBC Bridge driver (Bridge)
Type 2: Native-API/partly Java driver (Native)
Type 3: All Java/Net-protocol driver (Middleware)
Type 4: All Java/Native-protocol driver (Pure)
For detailed Answer refer JDBC Tutorials--->here
5)What Class.forName will do while loading drivers? 
It is used to create an instance of a driver and register it with the Driver Manager. When you have loaded a driver, it is available for making a connection with a DBMS.

6)What are the different types of statements in JDBC? 
The JDBC API has 3 Statements: 
1. Statement, 2. PreparedStatement, 3. CallableStatement.

The key features of these are as follows: 
Statement 
1. This interface is used for executing a static SQL statement and returning the results it produces.
2. The object of Statement class can be created using Connection.createStatement() method.

PreparedStatement 
1. A SQL statement is pre-compiled and stored in a PreparedStatement object.
2. This object can then be used to efficiently execute this statement multiple times.
3. The object of PreparedStatement class can be created using Connection.prepareStatement()  method. This extends Statement interface.

CallableStatement 
1. This interface is used to execute SQL stored procedures.
2. This extends PreparedStatement interface.
3. The object of CallableStatement class can be created using Connection.prepareCall() method.

For detailed Answer refer JDBC Tutorials--->here

7)What does setAutoCommit do?
When a connection is created, it is in auto-commit mode. This means that each individual SQL statement is treated as a transaction and will be automatically committed right after it is executed. The way to allow two or more statements to be grouped into a transaction is to disable auto-commit mode:
                               con.setAutoCommit(false) 

8)How can you make the connection? 
Connection is made using following Method:
                  getConnection(String url, String userName, String password);

This method establishes a connection to specified database url. It takes following three string types of arguments: 
url: Database url where stored or created your database
userName: User name of your DB
password: Password of DB

Include following code block to make a connection and create Statement.

try {
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          Connection con = DriverManager.getConnection("jdbc:odbc:dumy"; "user", "pass");
          Statement stmt = con.createStatement();
}catch( Exception e ) {
          e.printStackTrace();       
}

9)What is the fastest type of JDBC driver?
Type 4 (JDBC Net pure Java Driver) is the fastest JDBC driver. Type 1 and Type 3 drivers will be slower than Type 2 drivers (the database calls are make at least three translations versus two), and Type 4 drivers are the fastest (only one translation).

10)Is the JDBC-ODBC Bridge multi-threaded?
No. The JDBC-ODBC Bridge does not support multi threading. The JDBC-ODBC Bridge uses synchronized methods to serialize all of the calls that it makes to ODBC. Multi-threaded Java programs may use the Bridge, but they won't get the advantages of multi-threading.

11)How do you handle your own transaction ?
Connection Object has a method called setAutocommit ( boolean flag) . For handling our own transaction we can set the parameter to false and begin your transaction . Finally commit the transaction by calling the commit method.

12)What is a ResultSet ?
A table of data representing a database result set, which is usually generated by executing a statement that queries the database.A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.

13)What is the difference between executeupdate() and executequery() in JDBC ?
executeQuery() is used for Select statements.
executeUodate() is used for insert,update,delete statements..

14)How do I get runtime information about the JDBC Driver?
Use the following DatabaseMetaData methods:
getDriverMajorVersion()
getDriverMinorVersion()
getDriverName()
getDriverVersion()

15)How do I create an updatable ResultSet?
Just as is required with a scrollable ResultSet, the Statement must be capable of returning an updatable ResultSet. This is accomplished by asking the Connection to return the appropriate type of Statement using Connection.createStatement(int resultSetType, int resultSetConcurrency). The resultSetConcurrency parameter must be ResultSet.CONCUR_UPDATABLE. The actual code would look like this:

Statement stmt = con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE );

Note that the spec allows a driver to return a different type of Statement/ResultSet than that requested, depending on capabilities and circumstances, so the actual type returned should be checked with ResultSet.getConcurrency().

16)What causes the "No suitable driver" error?
"No suitable driver" is an error that usually occurs during a call to the DriverManager.getConnection method. The cause can be failing to load the appropriate JDBC drivers before calling the getConnection method, or it can be specifying an invalid JDBC URL--one that isn't recognized by your JDBC driver. Your best bet is to check the documentation for your JDBC driver or contact your JDBC driver vendor if you suspect that the URL you are specifying is not being recognized by your JDBC driver.
In addition, when you are using the JDBC-ODBC Bridge, this error can occur if one or more the the shared libraries needed by the Bridge cannot be loaded. If you think this is the cause, check your configuration to be sure that the shared libraries are accessible to the Bridge.

Comments

Popular posts from this blog

Servlet Advantages and Disadvantages

Session Management

The Deployment Descriptor: web.xml