Advanced Java Programming · Unit 5
Database Connectivity with JDBC
Exam-focused notes for Database Connectivity with JDBC (Advanced Java Programming, BIT401): what the TU syllabus asks and how it has actually been tested, with 6 solved past questions from this unit.
Database Connectivity with JDBC in short
JDBC (Java Database Connectivity) is the Java API for talking to relational databases. It lets one program work with many databases by changing only the driver.
What this unit covers
- JDBC architecture and driver types
- Steps of JDBC connection
- Statement and PreparedStatement
- Differences between Statement and PreparedStatement
- Handling multiple result sets
- Database connection from JSP
- Type 0 JDBC driver
Database connection from JSP
Handling multiple result sets
2082 · 5 marks
How do you handle multiple results in JDBC? Explain. [5]
Show solved answerHide answer
Handling Multiple Results in JDBC
Answer
Handling multiple results in JDBC involves managing situations where a single SQL statement execution returns more than one result set. This typically occurs with stored procedures or batch operations. Here's how to handle them:
1. Using execute() Method
The execute() method returns a boolean indicating whether the first result is a ResultSet:
Statement stmt = connection.createStatement();
boolean isResultSet = stmt.execute(sqlQuery);
if (isResultSet) {
ResultSet rs = stmt.getResultSet();
// Process first result set
} else {
int updateCount = stmt.getUpdateCount();
// Process update count
}
2. Processing Multiple Result Sets
Use getMoreResults() to move through subsequent result sets:
Statement stmt = connection.createStatement();
stmt.execute(sqlQuery);
int resultIndex = 0;
while (true) {
ResultSet rs = stmt.getResultSet();
if (rs != null) {
// Process current result set
while (rs.next()) {
// Handle row data
}
resultIndex++;
}
// Move to next result
if (!stmt.getMoreResults()) {
break;
}
}
3. Handling Update Counts
Distinguish between result sets and update counts:
int result = stmt.getUpdateCount();
if (result == -1) {
// No more results
} else if (result == 0) {
// Update/Insert/Delete executed
} else {
// Number of rows affected
}
4. Key Methods
| Method | Purpose |
|---|---|
execute() | Returns true if result is ResultSet, false if update count |
getResultSet() | Retrieves current ResultSet |
getUpdateCount() | Gets number of rows affected |
getMoreResults() | Moves to next result; returns true if ResultSet exists |
5. Best Practice
Always close resources properly:
try {
// Process multiple results
} finally {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
}
This approach ensures efficient handling of complex SQL operations returning multiple result sets.
Type 0 JDBC driver
2082 · 5 marks
What is Type 0 JDBC driver? Discuss about UDP. [5]
Show solved answerHide answer
Model Answer: Type 0 JDBC Driver and UDP
Type 0 JDBC Driver
Type 0 JDBC Driver (also called the JDBC-ODBC Bridge Driver) is a bridge-based driver that acts as a middleware between Java applications and databases through ODBC (Open Database Connectivity).
Key Characteristics:
- Architecture: Uses ODBC drivers installed on the client machine to communicate with the database
- Connection Path: Java Application → JDBC Driver → ODBC Driver → Database
- Platform Dependency: Requires ODBC drivers to be installed and configured on the client system
- Performance: Slower due to multiple layers of translation and bridging
- Usage: Primarily used for legacy systems and when ODBC drivers are already available
Limitations:
- Not suitable for web applications (ODBC not available on web servers)
- Performance overhead from bridge translation
- Deprecated in modern Java versions (removed from Java 8 onwards)
UDP (User Datagram Protocol)
UDP is a connectionless, unreliable transport layer protocol (Layer 4 of OSI model) used for fast data transmission.
Key Characteristics:
| Feature | Details |
|---|---|
| Connection | Connectionless - no handshake required |
| Reliability | Unreliable - no guarantee of delivery |
| Ordering | Packets may arrive out of order |
| Speed | Fast transmission with minimal overhead |
| Header Size | 8 bytes (smaller than TCP) |
| Flow Control | No flow control mechanism |
Applications:
- Video/audio streaming
- Online gaming
- DNS queries
- VoIP
- Real-time applications where speed matters more than reliability
Advantages:
- Low latency and overhead
- Suitable for real-time communication
- Broadcast and multicast capable
Disadvantages:
- No error checking or retransmission
- No congestion control
- Packets may be lost or duplicated
JDBC architecture and driver types
2081 · 5 marks
Explain the architecture of JDBC and list the JDBC driver types. [5]
Differences between Statement and PreparedStatement
2080 · 5 marks
How prepared statements differs from statements? Explain use of prepared statements with example. [5]
Steps of JDBC connection
Make Unit 5 stick
Practice BIT401 with flashcards & quizzes