JDBC Interview Questions

  • Facebook
  • Google
  • LinkedIn
  • Twitter

Q1. What is JDBC?

Ans. JDBC is a set of Java API for executing SQL statements. This API consists of a set of classes and interfaces to enable programs to write pure Java Database applications.

Q2. What are drivers available for JDBC?

Ans. Four types of driver are available for JDBC.

  • JDBC-ODBC Bridge driver
  • Native API Partly-Java driver
  • JDBC-Net Pure Java driver
  • Native-Protocol Pure Java driver

Q3. What is the difference between JDBC and ODBC?

Ans. Below are some differences between JDBC and ODBC

  • JDBC is for Java applications and ODBC is for Microsoft application.
  • ODBC can`t be directly used with Java because it uses a C interface.
  • ODBC makes use of pointers which have been removed totally from Java.
  • JDBC is designed to keep things simple while allowing advanced capabilities when required but ODBC has complex options for simple queries.
  • JDBC drivers are written in Java and JDBC code is automatically installable, secure, and portable on all platforms but ODBC requires manual installation of the ODBC driver manager and driver on all client machines.
  • JDBC API is a natural Java interface and is built on ODBC. JDBC retains some of the basic features of ODBC.

Q4. What are the types of JDBC Driver Models?

Ans. There are two types of JDBC Driver Models

Two tier model: Java applications interact directly with the database in this model. A JDBC driver is required to communicate with the particular database management system that is being accessed. SQL statements are sent to the database and the results are given to user. This model is referred to as client/server configuration where user is the client and the machine that has the database is called as the server.

Three tier model: A middle tier is required in this model and in this tier of this model are done as below:

  • Collection of SQL statements from the client and handing it over to the database
  • Receiving results from database to the client and maintaining control over accessing and updating the records.

Q5. How do you connect Java application with a database?

Ans. Below are the steps for connection Java application with database.

Loading the driver : To load the driver, Class.forName( ) method is used.

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

When the driver is loaded, it registers itself with the java.sql.DriverManager class as an available database driver.

Making a connection with database : To open a connection to a given database, DriverManager.getConnection( ) method is used.

Connection con = DriverManager.getConnection(“jdbc:odbc:somedb”, “user”,  “password”);

Executing SQL statements : To execute a SQL query, java.sql.statements class is used, createStatement( ) method of Connection to obtain a new Statement object.

  
Statement stmt  = con.createStatement( );
ResultSet rs = stmt.executeQuery(“SELECT * FROM some table”);

A query that returns data can be executed using the executeQuery( ) method of Statement. This method executes the statement and returns a java.sql.ResultSet that encapsulates the retrieved data:

Process the results : ResultSet returns one row at a time. Next( ) method of ResultSet object can be called to move to the next row. The getString( ) and getObject( ) methods are used for retrieving column values:

while(rs.next( ) ) {
  String event = rs.getString(“event”);
  Object count = (Integer) rs.getObject(“count”);
}
      Advertisements