JDBC interview questions [ Part 1 ]

1.Write a jdbc program to insert a data into table?

import java.sql.*;  
class InsertPrepared { 
    public static void main(String args[]){ 
        try { 
        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle"); 
        PreparedStatement stmt=con.prepareStatement("insert into Emp values(?,?)"); 
        stmt.setInt(1,101);//1 specifies the first parameter in the query 
        int i=stmt.executeUpdate(); 
        System.out.println(i+" records inserted"); 
        } catch(Exception e){

2.Diff between preparedstatement and statement?
In case of Statement, query is complied each time whereas in case of PreparedStatement, query is complied only once. So performance of PreparedStatement is better than Statement.

3.Explain transaction management in jdbc?
Transaction represents a single unit of work.
The ACID properties describes the transaction management well. ACID stands for Atomicity, Consistency, isolation and durability.
Atomicity means either all successful or none.
Consistency ensures bringing the database from one consistent state to another consistent state.
Isolation ensures that transaction is isolated from other transaction.
Durability means once a transaction has been committed, it will remain so, even in the event of errors, power loss etc.

4.What is mean by batch update in jdbc?

A batch update is a batch of updates grouped together, and sent to the database in one "batch", rather than sending the updates one by one.

5.What is a jdbc Drivermanager?

The 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().

6.What is a connection?
There are 5 steps to connect any java application with the database in java using JDBC. They are as follows:
Register the driver class
Creating connection
Creating statement
Executing queries
Closing connection

7.What is a Resultset?
A ResultSet object maintains a cursor that points to the current row in the result set. The term "result set" refers to the row and column data contained in a ResultSet object.
The methods of the ResultSet interface can be broken down into three categories −
Navigational methods: Used to move the cursor around.
Get methods: Used to view the data in the columns of the current row being pointed by the cursor.
Update methods: Used to update the data in the columns of the current row. The updates can then be updated in the underlying database as well.

8.What are the different types of jdbc Statements?
There are 3 types of Statements, as given below:


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


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


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

9.How can you view the data present in a result set?
if (!resultSet.next() ) { System.out.println("no data"); }

10.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:


Once auto-commit mode is disabled, no SQL statements will be committed until you call the method commit explicitly.

PreparedStatement updateSales = con.prepareStatement( "UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ?");
updateSales.setInt(1, 50);
updateSales.setString(2, "Colombian");
PreparedStatement updateTotal = con.prepareStatement("UPDATE COFFEES SET TOTAL = TOTAL + ? WHERE COF_NAME LIKE ?");
updateTotal.setInt(1, 50);
updateTotal.setString(2, "Colombian");

11.What is Connectionpooling?

It's a technique to allow multiple clinets to make use of a cached set of shared and reusable connection objects providing access to a database. Connection Pooling feature is supported only on J2SDK 1.4 and later releases.

12.Write a sample join query and its type?
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

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;

Related Post