Java Notes - Unit 5: JDBC, RMI and Servlets
Unit 5: JDBC, RMI, and Servlets
1. JDBC - Java Database Connectivity
JDBC Architecture:
- JDBC is an API for Java to interact with databases.
- Components: DriverManager, Driver, Connection, Statement, ResultSet, SQLException
Structured Query Language (SQL):
- SQL is used to interact with databases: SELECT, INSERT, UPDATE, DELETE.
JDBC Configuration:
Steps:
1. Import JDBC packages.
2. Register JDBC driver.
3. Open a connection.
4. Execute SQL queries.
5. Extract results.
6. Clean up.
Executing SQL Example:
import java.sql.*;
public class JDBCExample {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root",
"password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
while(rs.next())
Java Notes - Unit 5: JDBC, RMI and Servlets
System.out.println(rs.getInt(1) + " " + rs.getString(2));
con.close();
2. RMI - Remote Method Invocation
RMI Architecture:
- RMI allows invoking methods on remote objects.
- Components: Remote Interface, Remote Object, Stub, Skeleton, RMI Registry
Steps to create RMI app:
1. Create Remote Interface
2. Implement Remote Interface
3. Create Server and Client
4. Register objects with RMI registry
Example Remote Interface:
import java.rmi.*;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
Server:
import java.rmi.*;
import java.rmi.server.*;
public class HelloImpl extends UnicastRemoteObject implements Hello {
HelloImpl() throws RemoteException { super(); }
public String sayHello() { return "Hello from Server"; }
public static void main(String[] args) throws Exception {
Java Notes - Unit 5: JDBC, RMI and Servlets
Naming.rebind("rmi://localhost:5000/hello", new HelloImpl());
Client:
import java.rmi.*;
public class Client {
public static void main(String[] args) throws Exception {
Hello h = (Hello) Naming.lookup("rmi://localhost:5000/hello");
System.out.println(h.sayHello());
3. Servlets
Lifecycle of a Servlet:
1. init(): called once when the servlet is first loaded.
2. service(): handles requests (doGet, doPost etc).
3. destroy(): called when servlet is unloaded.
Servlet Packages:
- javax.servlet.*
- javax.servlet.http.*
Handling HTTP Requests and Responses:
- doGet(): handles GET requests.
- doPost(): handles POST requests.
Servlet Example:
import java.io.*;
Java Notes - Unit 5: JDBC, RMI and Servlets
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<h1>Hello from Servlet</h1>");
Important Questions:
1. Explain the JDBC architecture and how it works.
2. Write a JDBC program to retrieve records from a MySQL database.
3. Describe the steps involved in JDBC configuration.
4. Explain RMI architecture with a simple client-server example.
5. Write a Java RMI application to call a method from server.
6. Explain the lifecycle of a Servlet in detail.
7. Differentiate between doGet() and doPost() methods.
8. Write a servlet to handle HTTP GET requests.
9. What are the packages required for creating a servlet?
10. Compare JDBC and RMI.