Kyūkei Master Hub
SCS2108 | Phase Study Card
👁️ -- opens | 0%

Phase 8: JDBC Database Connectivity & Servlets

JDBC (Java Database Connectivity) allows Java applications to execute SQL statements. Learn the 5 steps of JDBC, Driver Types 1-4, and Java Servlet web basics.

1. The 4 JDBC Driver Types (Frequent Exam Question)

Driver Type Name Architecture & Performance
Type 1 JDBC-ODBC Bridge Converts JDBC calls to ODBC calls. Requires ODBC driver installed on client machine. (Deprecated, slowest).
Type 2 Native-API Driver Converts JDBC calls to native C/C++ client API of the database vendor. Requires vendor client software installed.
Type 3 Network-Protocol Driver Middleware server converts JDBC calls into database-independent network protocol, then into vendor database protocol.
Type 4 Pure Java Thin Driver Direct conversion of JDBC calls into vendor-specific network protocol written 100% in Java. (Fastest, no client installation required).

2. The 5 Steps of JDBC Connection Workflow

// Step 1: Load and register JDBC Driver Class.forName("com.mysql.cj.jdbc.Driver"); // Step 2: Establish Connection Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/school_db", "javauser", "java1234"); // Step 3: Create Statement (or PreparedStatement) String sql = "SELECT * FROM system_users WHERE username = ?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, "student1"); // Step 4: Execute Query ResultSet rs = pstmt.executeQuery(); while (rs.next()) { System.out.println("User: " + rs.getString("username")); } // Step 5: Close Connection & Cleanup rs.close(); pstmt.close(); conn.close();
💡 Examiner Distinction Tip: Statement vs PreparedStatement
Always use PreparedStatement over Statement! Why?
1. Security: Prevents SQL Injection attacks by escaping parameter inputs.
2. Performance: The SQL query is pre-compiled by the database engine, allowing faster repeated execution with parameters.

3. Java Servlets & Web Application Basics

Servlets are Java server-side components that process HTTP client requests and return dynamic responses.

Servlet Lifecycle Methods:

⚡ Quick Self-Test

Q: Why is Type 4 JDBC driver preferred over Type 1?

Answer: Type 4 is 100% Pure Java and communicates directly with the database protocol.
It requires no native client software or ODBC drivers to be pre-installed on the client machine, providing maximum portability and speed.