Answers to Important Questions - Unit 5
1. Explain the JDBC architecture and how it works.
JDBC comprises DriverManager (selects an appropriate driver), Driver (translates Java calls to DB-specific
calls), Connection (session with DB), Statement/PreparedStatement (executes SQL), ResultSet (represents
query result). The application loads driver, obtains Connection, sends SQL via Statement, processes
ResultSet, then closes resources.
2. Write a JDBC program to retrieve records from a MySQL database.
See code:
import [Link].*;
class Fetch {
public static void main(String[] a)throws Exception {
[Link]("[Link]");
try(Connection c=[Link]("jdbc:mysql://localhost:3306/test","root","pwd");
Statement s=[Link]();
ResultSet rs=[Link]("SELECT id,name FROM students");){
while([Link]())
[Link]([Link]("id")+" "+[Link]("name"));
3. Describe the steps involved in JDBC configuration.
(i) Add JDBC driver JAR to classpath. (ii) Load driver with [Link](). (iii) Create Connection with
[Link](). (iv) Create Statement/PreparedStatement. (v) Execute SQL. (vi) Process
ResultSet. (vii) Close resources.
4. Explain RMI architecture with a simple client?server example.
RMI uses: remote interface, remote object implementation (server), stub (client proxy), skeleton (server side,
JDK ?1.1), registry. Server binds remote object; client looks it up and invokes methods which travel via stub ?
Answers to Important Questions - Unit 5
transport ? skeleton ? object and back. Example classes Hello (interface), HelloImpl (server), Client (client)
as in notes.
5. Write a Java RMI application to call a method from server.
Refer to earlier code: after compiling and generating stub (rmic HelloImpl), start rmiregistry & server, then run
Client to print ?Hello from Server?.
6. Explain the lifecycle of a Servlet in detail.
Container loads servlet class and calls init(ServletConfig) once. For each request, container spawns thread
and calls service(), which dispatches to doGet/doPost/etc. Shared resources should be thread?safe. Finally,
when webapp stops or servlet is unloaded, container calls destroy() to release resources.
7. Differentiate between doGet() and doPost() methods.
doGet: parameters in URL query string, length limited, idempotent, cacheable, used for read?only operations.
doPost: parameters in request body, no length limit, not cached, used for operations causing state change
(insert/update).
8. Write a servlet to handle HTTP GET requests.
public class GreetServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException{
[Link]("text/html");
[Link]().println("<h2>Welcome "+[Link]("name")+"</h2>");
9. What are the packages required for creating a servlet?
[Link].*, [Link].*, [Link].* (since Servlet 3.0).
10. Compare JDBC and RMI.
JDBC provides database connectivity for executing SQL on local/remote DBMS; RMI enables invoking
Answers to Important Questions - Unit 5
methods on remote Java objects across JVMs. JDBC is data?centric, uses SQL; RMI is object?centric, uses
Java method calls. JDBC communicates with DB server via network protocol (TCP, DB driver), RMI
communicates between JVMs using Java RMI protocol.