Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views10 pages

JDBC Notes

The document provides an overview of JDBC statements, including types such as Statement, PreparedStatement, and CallableStatement, along with their differences. It explains methods like execute, executeQuery, and executeUpdate, as well as the distinctions between ResultSet and RowSet. Additionally, it covers JDBC architecture, transaction management, connection pooling, and various other JDBC components and functionalities.

Uploaded by

Valeria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views10 pages

JDBC Notes

The document provides an overview of JDBC statements, including types such as Statement, PreparedStatement, and CallableStatement, along with their differences. It explains methods like execute, executeQuery, and executeUpdate, as well as the distinctions between ResultSet and RowSet. Additionally, it covers JDBC architecture, transaction management, connection pooling, and various other JDBC components and functionalities.

Uploaded by

Valeria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

7)What are the JDBC statements?

In JDBC, Statements are used to send SQL commands to the database and
receive data from the database. There are various methods provided by
JDBC statements such as execute(), executeUpdate(), executeQuery, etc.
which helps you to interact with the database.

There is three type of JDBC statements given in the following table.

Statements Explanation

Statement It executes a static SQL query at runtime.

PreparedStatem The PreparedStatement is used when we need to provide input parameters t


ent query at runtime.

CallableStateme CallableStatement is used when we need to access the database stored proced
nt It can also accept runtime parameters.

Difference of statement and preparedstatements:

o The PreparedStatement performs faster as compare to Statement


because the Statement needs to be compiled everytime we run the
code whereas the PreparedStatement compiled once and then
execute only on runtime.
o PreparedStatement can execute Parameterized query whereas
Statement can only run static queries.

9) What are the differences between execute, executeQuery,


and executeUpdate?

execute executeQuery executeUpdate

The execute method can The executeQuery The executeUpdate method


be used for any SQL method can be used can be used to update/delete/insert
statements(Select and only with the select operations in the database.
Update both). statement.

The execute method The executeQuery() The executeUpdate() method returns


returns a boolean type method returns a representing the number of records
value where true indicates ResultSet object affected where 0 indicates that query
that the ResultSet s which contains the returns nothing.
returned which can later data retrieved by the
be extracted and false select statement.
indicates that the integer
or void value is returned.

12) What are the differences between ResultSet and RowSet?

ResultSet RowSet

ResultSet cannot be serialized as RowSet is disconnected from the database and can
it maintains the connection with be serialized.
the database.

ResultSet object is not a ResultSet Object is a JavaBean object.


JavaBean object

ResultSet is returned by the Rowset Interface extends ResultSet Interface a


executeQuery() method of calling the RowSetProvider.newFactory().cr
Statement Interface. method.

ResultSet object is non-scrollable RowSet object is scrollable and updatable by defau


and non-updatable by default.
JAVA BEEN:

The beans of javabeans are classes that encapsulate one or more


into a single standardized object.

13) Explain what DatabaseMetaData is and why would you use it?

The DatabaseMetaData is an interface that provides methods to gain


information about the database. This interface helps in obtaining
database-related information, such as database name, driver name,
database version, the total number of tables or views, and so on.

14)Explain the differences between JDBC and ODBC?


JDBC (Java Database
ODBC (Open Database Connectivity)
Connectivity)

ODBC can be used for things such as C, C++, Java,


JDBC is used for the Java
language.
and so on.

JDBC can be used on any ODBC can be used only for the Windows platform;
platform; thus, making it
platform-independent. thus, making it platform-dependent.

Most of the ODBC Drivers have been developed


JDBC drivers are developed using
using the Java language.
native languages such as C, C++
It is object orinted
it is the procedure orinted

.15) Explain the types of JDBC Architecture?

There are two kinds of architecture models to access the database while
using JDBC.

They are:

 Two-tier Architecture: In this model, Java programs exclusively


connect with the database and there is no requirement of any
mediator like applications servers to connect with the database.
This model is also known as the client-server architecture.
 Three-tier Architecture: This model is the complete opposite of
two-tier architecture. An application server is used as a mediator
between the Java program or JDBC driver and the database. The
Java program sends a request to the application server. The
server then forwards this request to the database and waits for a
response from the database.

16. Explain what DatabaseMetaData is and why would you use it?

The DatabaseMetaData is an interface that provides methods to gain


information about the database. This interface helps in obtaining
database-related information, such as database name, driver name,
database version, the total number of tables or views, and so on.

17) ResultSetMetaData: The object of ResultSetMetaData interface


cotains the information about the data (table) such as numer of columns,
column name, column type, etc. The getMetaData() method of ResultSet
returns the object of ResultSetMetaData.

18) Connection: The Connection object is created by using


getConnection() method of DriverManager class. DriverManager is the
factory for connection.

19) Which interface is responsible for transaction management


in JDBC?
The Connection interface provides methods for transaction
management such as commit(), rollback() etc.

More details.

20)

Stored Procedure Function

Is used to perform business Is used to perform the calculation.


logic.

Must not have the return type. Must have the return type.
May return 0 or more values. May return only one value.

The procedure supports input The function supports only input parameter.
and output parameters.

Exception handling using Exception handling using


try/catch block can be used in try/catch can't be used in user-defined functions.
stored procedures.

21)How can we maintain the integrity of a database by using


JDBC?
To maintain the integrity of a database, we need to ensure the ACID
properties. ACID properties mean Atomicity, Consistency, Isolation, and
durability. In JDBC, Connection interface provides methods like
setAutoCommit(), commit(), and rollback() which can be used to manage
transaction. Let's see an example of transaction management in JDBC.

22) What is the JDBC Rowset?


JDBC Rowset is the wrapper of ResultSet. It holds tabular data like
ResultSet, but it is easy and flexible to use. The implementation classes of
RowSet interface are as follows:

o JdbcRowSet
o CachedRowSet
o WebRowSet
o JoinRowSet
o FilteredRowSet

23) What is the major difference between java.util.Date and


java.sql.Date data type?
The major difference between java.util.Date and java.sql.Date is that,
java.sql.Date represents date without time information whereas,
java.util.Date represents both date and time information.

24) What does JDBC setMaxRows method do?


The setMaxRows(int i) method limits the number of rows the database can
return by using the query. This can also be done within the query as we
can use the limit cause in MySQL.

25) Explain the term connection pooling.

This is another one of the popular JDBC interview questions. Connection


pooling is the technique by which we reuse the resource-like connection
objects that are required to establish a connection with the database. It is
a technique of managing and creating a pool of connections that can be
used by any thread that requires them. Each time an application tries to
access a backend store (such as a database), it needs resources to
develop, maintain, and release a connection to that datastore. Thus,
connection pooling can greatly increase the performance of a Java
application while reducing the overall resource usage.

26) 20. What are the main components of JDBC?

Following are the four major components available in JDBC:

 JDBC Driver Manager

 JDBC API

 JDBC Test Suite

 JDBC-ODBC Bridge

27. Explain the meaning of hot backup and cold backup.

While this question is not related to JDBC directly, it may still be asked in
an interview. The cold back is the backup process in which the backup of
files is taken before restarting the database. In the hot back, backup
process the backup of tables and files is taken at the time when the
database is running.
28. What are the steps to connect with JDBC?

There are 2 types of locking available in JDBC by which we can manage


various user issues. If two users are trying to access the same record then
there is no issue but the problem arises when multiple users are trying to
update the same record simultaneously. To address this issue, there are
two locking modes available in JDBC. They are:

1. Optimistic Locking: Locks the record only when an update is


taking place. This kind of locking does not use exclusive locks
while reading.

2. Pessimistic Locking: In this system, the records are locked as the


row to update is selected.

29. What are database warnings in JDBC and explain how we can
manage database warnings in JDBC?

The database warning or SQL warning is nothing but the subclass of the
SQLException class. You can manage or handle this warning by using the
getWarnings() method on a statement, connection, and ResultSet.

30. Explain why you would use setAutoCommit(false) in JDBC?

The setAutoCommit(false) is used to turn off the Auto Commit.

31. Explain the role of Class.forName while loading drivers in


brief?

The Class.forName generates an instance of JDBC driver and a register


with DriverManager.

32. What are the JDBC API components?

The interfaces and the classes of JDBC API are mentioned below:
Different Interfaces

 Connection: Using the getConnection()method, a connection


object is created.

 Statement: Using the createStatement() method of the


connection class, the statement object is created.

 PrepareStatement: The prepareStatement() method of connection


class, the prepareStatement object is created.

 DatabaseMetaData: The Connection interface's getMetaData()


method returns an object of DatabaseMetaData.

 CallableStatement: The stored procedures and functions are


called using the CallableStatement interface.

Different Classes

 DriverManager: The class's function is to serve as a gateway


between users and drivers.

 Blob: The binary term for a huge object. It stands for a set of
binary data that is kept together in the database management
system as a single entity.

 Clob: Character Large Object is referred to as Clob. Different


database management systems utilize it as a data type to store
character files.

 SQLException: It is an exception class that offers details on


problematic database access.

33. Mention the return type of Class.forName() method?

The object of java.lang.Class Object is the return type of the


Class.forName() method.

34. Mention the functions of the JDBC Connection interface?


The database session is maintained by the Connection interface. The
management of transactions is possible with it. To return the instance of
statement, Prepared Statement, Callable Statement, and
DatabaseMetaData, the factory method is provided by the JDBC
connection interface.

35. Mention the interface which enables transaction management


in JDBC.

The Connection interface offers transaction management methods like


commit(), rollback(), and others.

36. Explain batch processing and method to perform batch


processing in JDBC?

We can run many queries by utilizing JDBC's batch processing method. It


speeds up the performance. Batch processing is supported through
java.sql.Statement and java.sql.Prepared Statement interfaces. The
actions listed below are necessary for batch processing in JDBC.

Load the driver class

Create Connection

Create Statement

Add query in the batch

Execute the Batch

Close Connection

37. What are the different types of lockings in JDBC?

The four different types of locks offered by JDBC are discussed below.
 Row and Key Locks: We utilize this kind of lock when updating the
rows.

 Page locks: A page can have one of these locks applied to it.

 Table locks: The table is protected by table locks.

 Database locks: When a database is open, a lock is used to stop


other transactions from reading or updating data.

You might also like