Advance Java with Web Application (Module 2)
1. Explain JDBC and its importance in Java applications.
Answer:
JDBC (Java Database Connectivity) is a standard Java API used to connect Java applications to
relational databases. It provides methods to execute SQL queries, retrieve results, and manage database
transactions. JDBC acts as a bridge between Java programs and database systems such as MySQL,
Oracle, or PostgreSQL.
Importance:
• Enables database-driven applications
• Platform-independent connectivity
• Supports transaction management
• Ensures secure and efficient data handling
2. Describe the JDBC architecture with a neat explanation.
Answer:
JDBC architecture consists of two layers:
1. JDBC API Layer – Provides interfaces and classes such as Connection, Statement,
ResultSet.
2. JDBC Driver Layer – Communicates with the database.
Components:
• Java Application
• JDBC API
• DriverManager
• JDBC Driver
• Database
Flow Diagram:
3. Explain different types of JDBC drivers in detail.
Answer:
Type 1: JDBC-ODBC Bridge Driver
• Uses ODBC driver to connect
• Platform dependent
• Obsolete now
Type 2: Native API Driver
• Uses database native libraries
• Faster than Type 1
• Platform dependent
Type 3: Network Protocol Driver
• Uses middleware server
• Converts JDBC calls into database protocol
• Platform independent
Type 4: Thin Driver
• Pure Java driver
• Direct communication with database
• Platform independent and most widely used
4. Explain the steps required to connect a Java application to a database using JDBC.
Answer:
Steps:
1. Import JDBC packages
2. Load and register driver
3. Establish connection using DriverManager
4. Create Statement/PreparedStatement
5. Execute SQL query
6. Process results
7. Close connection
Example:
Connection con = [Link](url, user, pass);
5. Discuss how SQL queries are executed in JDBC.
Answer:
JDBC provides three main methods:
• executeQuery() – Used for SELECT statements
• executeUpdate() – Used for INSERT, UPDATE, DELETE
• execute() – Used when result type is unknown
Example:
Statement stmt = [Link]();
ResultSet rs = [Link]("SELECT * FROM student");
6. Explain the Statement interface in JDBC.
Answer:
Statement is used to execute static SQL queries. It is created using:
Statement stmt = [Link]();
Features:
• Executes simple SQL queries
• Less secure (vulnerable to SQL injection)
• No parameter support
7. Explain PreparedStatement and its advantages.
Answer:
PreparedStatement is a precompiled SQL statement that supports parameters.
Example:
PreparedStatement ps = [Link]("INSERT INTO student VALUES(?,?)");
[Link](1, 101);
[Link](2, "John");
[Link]();
Advantages:
• Prevents SQL Injection
• Better performance
• Reusable and efficient
8. Differentiate between Statement and PreparedStatement.
Answer:
Statement PreparedStatement
Used for static queries Used for dynamic queries
No parameters Supports parameters (?)
Less secure More secure
Slower Faster (precompiled)
9. Explain ResultSet in detail.
Answer:
ResultSet stores the result returned from a SELECT query. It acts like a cursor that points to rows of
data.
Important methods:
• next() – Moves to next row
• getInt(), getString() – Retrieve data
• previous() – Move backward (if scrollable)
Types:
• Forward-only
• Scrollable
• Updatable
10. What is ResultSetMetaData? Explain its use.
Answer:
ResultSetMetaData provides information about columns in a ResultSet.
Methods:
• getColumnCount()
• getColumnName()
• getColumnType()
It is useful when column structure is unknown.
11. Explain transaction management in JDBC.
Answer:
A transaction is a group of SQL statements executed as a single unit. JDBC supports transaction
control using:
• setAutoCommit(false)
• commit()
• rollback()
Transactions ensure:
• Data consistency
• Atomicity
• Integrity
12. Explain commit(), rollback(), and savepoint in JDBC.
Answer:
• commit() – Saves all changes permanently
• rollback() – Undoes all changes since last commit
• Savepoint – Marks a point within a transaction to rollback partially
Example:
Savepoint sp = [Link]();
[Link](sp);
13. What is batch processing in JDBC? Explain with example.
Answer:
Batch processing allows multiple SQL statements to execute together to improve performance.
Example:
Statement stmt = [Link]();
[Link]("INSERT INTO student VALUES(1,'A')");
[Link]("INSERT INTO student VALUES(2,'B')");
[Link]();
Advantages:
• Reduces database round trips
• Improves performance
14. Explain auto-commit mode in JDBC.
Answer:
By default, JDBC operates in auto-commit mode, meaning every SQL statement is committed
automatically after execution.
To disable:
[Link](false);
This allows manual transaction control.
15. Explain the complete life cycle of a JDBC application.
Answer:
1. Load Driver - [Link]("[Link]");
2. Establish Connection - [Link]()
3. Create Statement/PreparedStatement - PreparedStatement ps = [Link]("INSERT
INTO student VALUES(?, ?)");
4. Execute Query - ResultSet rs = [Link]("SELECT * FROM student");
5. Process ResultSet -
6. Handle Transactions
7. Close Resources (ResultSet, Statement, Connection)
Proper resource closing ensures better performance and avoids memory leaks.