Tuesday, January 17, 2012

JDBC Interview Questions

1.What is the JDBC?
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.

2. How to communicate with data base?
(A) To connect to data base we have set of interfaces provided by Java.sql.*; First you need to load JDBC driver and then Make JDBC connection.

3. What are the different types of database drivers?
(A) Four types of drivers available
Type -1: (JDBC-ODBC Bridge Driver)
In this type, you have java API which talks to a native API (JDBC-ODBC Driver) which then talks to a database.
Type-2 (Thick/Native driver)
In this type, you have a native API which talks to a Database.
Type-3 (Network protocol driver)
It`s a three tier architecture, the client talks to server with network protocol diver and server uses any one from other three type of drivers. This type of driver is used when lot of concurrency can be done.
Type-4 (Pure or Thin Driver)
It`s complete Java API which talk to a Database, this is most often used driver.

4. How to Load the driver?
(A) Use Class.forName(), forName() is a method in Class which returns a class object, to the forName() you provide a string parameter that is the Class that you want to load. To load the Class you need to provide total path including package.
For type-1 Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
For type-2 using Oracle database Class.forName(“oracle.jdbc.driver.OracleDriver”);

5. How to fetch the connection?
(A) Connection is the interface in Java.sql package.
For Type-1
Connection con=DriverManager.getConnection(“jdbc:odbc:[odcb dsn]“:”username”:”password”);

6. What is a ResultSet ?
(A) ResultSet is a Java Object, it contains the results of executing an SQL query. In other words it is a table of data representing a database result set, which is usually generated by executing a statement that queries the database.
Sample : ResultSet rs = stmt.executeQuery(“SELECT a, b, c FROM Table1″);

7. What do you mean by fastest type of JDBC driver?
(A) JDBC driver performance or fastness depends on a number of issues: Quality of the driver code, size of the driver code, database server and its load, Network topology, Number of times your request is translated to a different API.

8. What are stored procedures?
A : A stored procedure is a set of statements/commands which reside in the database. The stored procedure is precompiled. Each Database has it's own stored procedure language.

9. What is JDBC Driver ?
A The JDBC Driver provides vendor-specific implementations of the abstract classes provided by the JDBC API. This driver is used to connect to the database.

10. What are the steps required to execute a query in JDBC?
A First we need to create an instance of a JDBC driver or load JDBC drivers, then we need to register this driver with DriverManager class. Then we can open a connection. By using this connection , we can create a statement object and this object will help us to execute the query.

11. What is DriverManager ?
A DriverManager is a class in java.sql package. It is the basic service for managing a set of JDBC drivers.

12. Is the JDBC-ODBC Bridge multi-threaded?
A 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.

13 . What is cold backup, hot backup, warm backup recovery?
A : Cold backup means all these files must be backed up at the same time, before the database is restarted. Hot backup (official name is 'online backup' ) is a backup taken of each tablespace while the database is running and is being accessed by the users

14 . What is the advantage of denormalization?
A : Data denormalization is reverse procedure, carried out purely for reasons of improving performance. It maybe efficient for a high-throughput system to replicate data for certain data.

15 . How do you handle your own transaction ?
A :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.



11. 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).

12. 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.

No comments: