Top Interview Questions & Answers | Learn Now

Jdbc Interview Questions and Answers | Basic and Advanced Levels

Written by Bhavya Sri | Mar 29, 2018 5:38:40 AM

Q1. What is JDBC?

Ans: JDBC stands for Java Database Connectivity.It is a J2EE API that provides cross-Database connectivity to a wide range of SQL databases.With a JDBC technology enabled driver we can connect to all corporate data even if it is in a heterogeneous environment.
 

Q2. Explain JDBC Driver?

 
Ans: The JDBC Driver provides specific implementations of the abstract classes provided by the JDBC API. The driver is used to connect to the database.
 

Q3. What is the difference between Database and Database management system?

Ans: Database is a collection of interrelated data. Database management system is a software which can be used to manage the data by storing it on to the data base and by retrieving it from the data base.  And  DBMS is a collection of interrelated data and some set of programs to access the data.

There are 3 types of Database Management Systems:

  1. Relational DataBase Management Systems(RDBMS):   It is a software system, which can be used to represents data in the form of tables. RDBMS will use SQL2 as a Queries  language.
  2. Object Oriented DataBase Management Systems(OODBMS):  It is a software system, which can be used to represent the data in the form of objects. This DBMS will use OQL as a Query language.
  3. Object Relational DataBase Management Systems(ORDBMS):  It is a DBMS which will represents some part of the data in the form of tables and some other part of the data in the form of objects. This management system will use SQL3 as a Query Language, it is a combination of  SQL2 and OQL.

Q4. How to load a JDBC driver?

Ans:

  • In general sun Microsystems  has provided Driver interface for this all the database vendors has provided their own implementation.
  • If we want to use the database vendor provided Driver implementation to our jdbc application, first we need to make the availability of the respective Driver’s   .class file to JVM, for this we need to set class path environment variable to the location where we have the driver implementation.
  • Sun Microsystems is also provided an implementation to the Driver interface in the form of JdbcOdbcDriver class as part of the java software.
  • If we want to use JdbcOdbcDriver in our jdbc applications no need to set class path environment variable. Why because it was already available in the java software’s pre-defined library.
  • JdbcOdbcDriver internally depends on the mocrosoft product Odbc driver. If we want to use the JdbcOdbcDriver in our jdbc applications first we must configure Odbc driver, for this we will use the following path.
  • Start/ conrlol panel / performance and maintenance / administrative tools / data source(Odbc)/ user dsn / click on Add / select microsofr Odbc for oracle / finish / provide data source name only / click on ok / ok.
  • To load the driver’s class byte code to the memory we will use the following method.

Q5. Name the main components of JDBC ? 

Ans: The main components of JDBC 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.

Q6. What is JDBC and What are the steps to write a JDBC application?

Ans:

  • The process of interacting with the database from a java application is called as JDBC(Java Database Connectivity)
  • To interact with the database from a java application we will use the following five steps.
  • load and register the driver.
  • Establish a connection between java application and the database.
  • prepare either statement object or PreparedStatement object or CallebleStatement object as per the application requirements.
  • write and executer the sql queries.
  • terminate the connection which we have established.

Q7. Which JDBC driver is fastest?

Ans: The JDBC Net pure Java driver is the fastest driver.It converts the JDBC calls into vendor specific calls and thus it directly interacts with the database and eventually it is fastest.

Q8. Name the different types of JDBC drivers?

Ans: Following are the different types of different:
a) Network protocol Driver. 
b) JDBC Net pure Java Driver. 
c) Native API Partly Java Driver. 
d) JDBC-ODBC Bridge Driver.

Q9. How to establish a Database connection between java application and Database?

Ans:

  • If we want to establish a connection between java application and the database we will the following piece of code.
  • Connection con= DriverManager.getConnection(“jdbc:odbc:nag”,”nag”,”system”,”manager”);
  • Where getConnectin() is a static method from DriverManager class, which can be used to return connection object.

Q10. What is the requirement to use Statement object?

Ans:

  • After establishing a connection between java application and the database we need to write the sql queries and we need to execute them.
  • To execute the sql queries we will use some pre-defined library, which was defined in the form of Statement object, PreparedStattement object and CallableStatement object.
  • As per the application requirements we need to create either Statement object or CallableStatement object and PreparedStatement object.
  • To create Statement object dwe will use the following method from connection object.

public  Statement createStatement()

Eg:     Statement st = con.createStatement();

Q11. How to  execute SQL Queries from a java application?

Ans: st.executeQuery(…)

st.executeUpdate(…)

st.execute(…)

To execute the sql queries we will use the following methods from Statement object.

Q12. What is resultset ?

Ans: The ResultSet represents set of rows retrieved due to query execution. ResultSet rs = stmt.executeQuery(sqlQuery);

Q13. What are the different types of RowSet ?

Ans: There are two types of RowSet are there. They are:

a) Connected: A connected RowSet object connects to the database once and remains connected until the application terminates.
b)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.

 

Q14. What is a jdbc Drivermanager?

Ans:AThe DriverManager class acts as an interface between user and drivers. It keeps track of the drivers that are available and handles establishing a connection between a database and the appropriate driver. The DriverManager class maintains a list of Driver classes that have registered themselves by calling the method DriverManager.registerDriver().

 

Q15. What is rowset? 

Ans: 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. RowSets support component-based development models like JavaBeans, with a standard set of properties and an event notification mechanism.
 

Q16. How to create a table dynamically from a jdbc application?

Ans:   

 import java.sql.*;

import java.io.*;

public class CreateTableEx

{

public static void main(String[] args)throws Exception

{

//create buffered reader object

BufferedReader br = new BufferedReader(new       InputStreamReader(System.in));

//load and register the driver

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

//establish connection

Connection con = DriverManager.getConnection(“jdbc:odbc:nag”,”system”,”durga”);

//create statement object

Statement st = con.createStatement();

//take table name as dynamic input

System.out.println(“Enter table name”);

String tname = br.readLine();

//execute sql query

St.executeUpdate(“create table”+tname+”(eno number,ename varchar2(10),esal number,eaddr varchar2(10))”);

System.out.println(“table created successfully”);

//closing the connection

con.close();

}

}

//import section

Q17. How to insert records into a table from a JDBC application?

Ans:

import java.io.*;

public class InsertTableEx

{

public static void main(String[] args) throws Exception

{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

Class.forName(“oracle.jdbc.driver.OracleDriver”);

Connection con  =               DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”,”system”,”durga”);

Statement st = con.createStatement();

while(true)

{

System.out.println(“Enter emp number”);

Int eno = Integer.parseInt(br.readLine());

System.out.println(“Enter emp name”);

String ename = br.readLine();

System.out.println(“Enter emp sal”);

Float esal = Float.parseFloat(br.readLine());

System.out.println(“Enter emp address”);

String eaddr = br.readLine();

st.executeUpdate(“insert into emp1 values(“+eno+”,’”+ename+”’,”+esal+”,’”+eaddr+”’)”);

System.out.println(“read successfully inserted”);

System.out.println(“one more record[y/n]);

String option = br.readLine();

If(option.equals(“n”))

break;

}

}

}

import java.sql.*;

Q18. How to update a table  from a jdbc application?.

Ans:

import java.sql.*;

public class UpdateTableEx

{

public static void main(String[] args)throws Exception

{

//load n register the driver in alternative way to Class.forName

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

Connection con = DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:xee”,”system”,”durga”);

Statement st = con.createStatement();

int updateCount = st.executeUpdate(“update emp1 set esal = esal+500 where esal<9000”);

System.out.println(“records updated……..”+updateCount);

con.close();

}

}

Q19.  Write a sample join query and its type?

Ans: INNER JOIN: Returns all rows when there is at least one match in BOTH tables
LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table
FULL JOIN: Return all rows when there is a match in ONE of the tables

Q20. What are the different types of jdbc Statements?

Ans: There are 3 types of Statements, as given below:

Statement:

It can be used for general-purpose access to the database. It is useful when you are using static SQL statements at runtime.

PreparedStatement:

It can be used when you plan to use the same SQL statement many times. The PreparedStatement interface accepts input parameters at runtime.

CallableStatement:

CallableStatement can be used when you want to access database stored procedures.