Web Technologies
Comprehensive exam revision notes for BCA-303N Unit 4 (Database Access with JDBC, PHP-MySQL & MVC Frameworks). Includes Section A (2M), Section B (5M), and Section C (15M) questions, answers, and diagrams.
Web Technologies
Database Access with JDBC, PHP-MySQL & MVC Frameworks
Core Definitions & Key Concepts
What is JDBC and what layer does it occupy in Java Enterprise architecture
JDBC (Java Database Connectivity) is a standard Java API (java.sql, javax.sql) that defines interfaces for connecting Java applications to relational databases, abstracting vendor-specific database protocols.
Name the four types of JDBC Drivers
- Type 1: JDBC-ODBC Bridge Driver (Deprecated)
- Type 2: Native-API Driver (Partially Java, requires client library)
- Type 3: Network-Protocol Driver (Pure Java, uses middleware)
- Type 4: Thin Driver (Direct-to-Database Pure Java - Industry standard)
The difference between Statement and PreparedStatement
| Feature | Statement | PreparedStatement |
|---|---|---|
| Query Compilation | Compiled by DB engine every time | Pre-compiled once, executed repeatedly |
| Performance | Slower for repetitive queries | Fast due to compiled execution plan |
| SQL Injection | Vulnerable to SQL injection | Immune (parameters bound safely) |
| Syntax | String concatenation ('" + id + "') | Placeholders (?) |
What is a CallableStatement in JDBC
CallableStatement extends PreparedStatement to execute Stored Procedures and stored functions located in the database, allowing IN, OUT, and INOUT parameters.
What does ACID stand for in Database Transactions
- A - Atomicity: All operations complete successfully or all are rolled back.
- C - Consistency: Database moves from one valid state to another.
- I - Isolation: Concurrent transactions do not interfere with each other.
- D - Durability: Committed transaction results survive system crashes.
How do you manage transactions programmatically in JDBC
- Disable auto-commit:
connection.setAutoCommit(false); - Execute DML queries.
- Commit on success:
connection.commit(); - Roll back on exception:
connection.rollback();
What is Connection Pooling
Connection Pooling maintains a cache of pre-created, reusable physical database connections, eliminating the high latency of repeatedly opening and closing TCP socket database handshakes for every user request.
The three tiers in MVC (Model-View-Controller) Architecture
- Model: Represents business data, entities, and database logic.
- View: Generates presentation/UI displayed to the client (HTML, JSP, React).
- Controller: Intercepts client requests, updates Model, and picks next View.
What is PDO in PHP and why is it preferred over mysqli
PDO (PHP Data Objects) provides a uniform, database-agnostic data access abstraction layer supporting 12+ relational database systems, whereas mysqli only works with MySQL.
What is an ORM (Object-Relational Mapping)
An ORM (e.g., Hibernate, JPA, Doctrine) automatically maps object-oriented classes and properties to relational database tables and foreign keys, abstracting raw SQL queries.
Conceptual & Architectural Analysis
Compare the 4 Types of JDBC Drivers with an Architecture Diagram
+-------------------------------------------------------------+
| Type 1: JDBC-ODBC Bridge | Java App -> JDBC -> ODBC -> DB |
| Type 2: Native-API Driver | Java App -> JDBC -> C/C++ API |
| Type 3: Network Protocol | Java App -> Middleware Server |
| Type 4: Pure Java Thin | Java App -> Direct TCP/IP -> DB|
+-------------------------------------------------------------+| Driver Type | Pure Java? | Client Installation Required? | Performance |
|---|---|---|---|
| Type 1 | No | Requires native ODBC driver | Slowest |
| Type 2 | No | Requires native vendor client library | Moderate |
| Type 3 | Yes | No client installation | Good |
| Type 4 | Yes | No client installation (Direct socket) | Fastest & Preferred |
The 5 Standard Steps to Execute a Query in JDBC
5-Step JDBC Execution Lifecycle
- Load Driver Class:
Class.forName("com.mysql.cj.jdbc.Driver");(Modern JDBC 4.0 does this automatically viaDriverManagerSPI). - Establish Connection:
Connection conn = DriverManager.getConnection(url, user, pass); - Create Statement:
PreparedStatement ps = conn.prepareStatement("SELECT * FROM users WHERE id = ?"); - Execute Query:
ResultSet rs = ps.executeQuery();orint rows = ps.executeUpdate(); - Close Resources: Close
ResultSet,Statement, andConnection(or usetry-with-resources).
Transaction Management in JDBC with Rollback
1Connection conn = null;2try {3 conn = dataSource.getConnection();4 conn.setAutoCommit(false); // Begin Transaction5
6 // Debit Account A7 PreparedStatement psDebit = conn.prepareStatement("UPDATE accounts SET balance = balance - ? WHERE id = ?");8 psDebit.setDouble(1, 5000.0);9 psDebit.setInt(2, 101);10 psDebit.executeUpdate();11
12 // Credit Account B13 PreparedStatement psCredit = conn.prepareStatement("UPDATE accounts SET balance = balance + ? WHERE id = ?");14 psCredit.setDouble(1, 5000.0);15 psCredit.setInt(2, 202);16 psCredit.executeUpdate();17
18 conn.commit(); // Commit Transaction19 System.out.println("Transfer successful!");20} catch (SQLException e) {21 if (conn != null) conn.rollback(); // Rollback on failure!22 System.err.println("Transaction rolled back: " + e.getMessage());23} finally {24 if (conn != null) conn.setAutoCommit(true);25}The MVC (Model-View-Controller) Flow in Web Applications
[ Client Browser ]
| ^
1. HTTP Request| | 6. Rendered HTML View
v |
[ CONTROLLER (Servlet) ]
| ^
2. Updates| | 5. Selects View & passes Data
v |
[ MODEL (JavaBean) ] [ VIEW (JSP) ]
| ^
3. SQL | | 4. Reads Model
v |
[ DATABASE ] ----------------+Database Metadata in JDBC (DatabaseMetaData and ResultSetMetaData)
- `DatabaseMetaData`: Obtains comprehensive information about the database software itself (product name, version, driver name, list of tables, supported SQL dialects).
- `ResultSetMetaData`: Obtains schema information about the returned query columns (column count, column names, column data types, nullability).
In-Depth Analysis & Implementation
Develop a Complete Enterprise JDBC Data Access Object (DAO) Pattern Implementation with PreparedStatement and Connection Pooling
package com.mjpru.bca.dao;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
// Student Entity Model
class Student {
private int rollNo;
private String name;
private double marks;
public Student(int rollNo, String name, double marks) {
this.rollNo = rollNo;
this.name = name;
this.marks = marks;
}
public int getRollNo() { return rollNo; }
public String getName() { return name; }
public double getMarks() { return marks; }
}
// DAO Interface
interface StudentDAO {
void addStudent(Student s) throws SQLException;
Student getStudentByRoll(int rollNo) throws SQLException;
List<Student> getAllStudents() throws SQLException;
}
// DAO Implementation
public class StudentDAOImpl implements StudentDAO {
private final String jdbcURL = "jdbc:mysql://localhost:3306/mjpru_exam";
private final String jdbcUser = "bca_admin";
private final String jdbcPass = "exam2025";
private Connection getConnection() throws SQLException {
return DriverManager.getConnection(jdbcURL, jdbcUser, jdbcPass);
}
@Override
public void addStudent(Student s) throws SQLException {
String sql = "INSERT INTO students (roll_no, name, marks) VALUES (?, ?, ?)";
try (Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, s.getRollNo());
ps.setString(2, s.getName());
ps.setDouble(3, s.getMarks());
ps.executeUpdate();
System.out.println("Student inserted successfully: " + s.getName());
}
}
@Override
public Student getStudentByRoll(int rollNo) throws SQLException {
String sql = "SELECT roll_no, name, marks FROM students WHERE roll_no = ?";
try (Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, rollNo);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
return new Student(rs.getInt("roll_no"), rs.getString("name"), rs.getDouble("marks"));
}
}
}
return null; // Not found
}
@Override
public List<Student> getAllStudents() throws SQLException {
List<Student> list = new ArrayList<>();
String sql = "SELECT roll_no, name, marks FROM students ORDER BY roll_no ASC";
try (Connection conn = getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
list.add(new Student(rs.getInt("roll_no"), rs.getString("name"), rs.getDouble("marks")));
}
}
return list;
}
}Database Connection Pooling Architecture and Compare HikariCP vs Traditional DriverManager
Connection Pooling vs DriverManager Architecture
[ TRADITIONAL DriverManager ]
Request 1 ---> Open Socket -> DB Handshake -> Auth -> Run SQL -> Close Socket (HIGH LATENCY!)
Request 2 ---> Open Socket -> DB Handshake -> Auth -> Run SQL -> Close Socket (HIGH LATENCY!)
[ CONNECTION POOLING (HikariCP / DBCP) ]
Initial Startup: Pre-allocates 10-50 physical persistent TCP connections to DB
Request 1 ---> Borrow Connection from Pool -> Run SQL -> Return to Pool (LOW LATENCY! <1ms)
Request 2 ---> Borrow Connection from Pool -> Run SQL -> Return to PoolWhy HikariCP Outperforms Others:
- Bytecode Engineering: Extremely lean assembly and bytecode optimization.
- Zero Overhead: Eliminates lock contention via lock-free collections (
FastList,ConcurrentBag). - Thread Safety: Prevents connection leakage and connection stall through rigorous timeouts (
connectionTimeout,idleTimeout,maxLifetime).
The Complete MVC Web Architecture with a Working End-to-End Case Study in Java Servlets and JSP
Enterprise MVC Case Study
1. Model: StudentBean.java
Represents state and database records with getter/setter properties.
2. Controller: StudentControllerServlet.java
- Intercepts requests like
/students?action=list. - Calls
StudentDAOto retrieve list of student objects. - Places student list into request scope:
request.setAttribute("studentList", list);. - Dispatches to View:
request.getRequestDispatcher("student_view.jsp").forward(request, response);.
3. View: student_view.jsp
- Uses JSTL
<c:forEach>to loop through${studentList}and render formatted HTML tables. - Contains zero business or database code, guaranteeing clean separation of concerns and maintainability.
Important Exam Points & Quick Revision
Examiner high-yield focus: 4 types of JDBC drivers, Statement vs PreparedStatement, and DAO Pattern with Transaction rollback.
100% left on this page — finishing it closes the gap for Database Access with JDBC, PHP-MySQL & MVC Frameworks.
Database Access with JDBC, PHP-MySQL & MVC Frameworks still counts as a weak topic until you finish this note and answer its questions.
Practise this unitTest Your Understanding
Now that you've read through these notes, practice unit MCQs to identify weak points and solidify your learning before exams.