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 [Link].*;
public class JDBCExample {
public static void main(String[] args) throws Exception {
[Link]("[Link]");
Connection con = [Link]("jdbc:mysql://localhost:3306/testdb", "root",
"password");
Statement stmt = [Link]();
ResultSet rs = [Link]("SELECT * FROM students");
while([Link]())
Java Notes - Unit 5: JDBC, RMI and Servlets
[Link]([Link](1) + " " + [Link](2));
[Link]();
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 [Link].*;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
Server:
import [Link].*;
import [Link].*;
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
[Link]("rmi://localhost:5000/hello", new HelloImpl());
Client:
import [Link].*;
public class Client {
public static void main(String[] args) throws Exception {
Hello h = (Hello) [Link]("rmi://localhost:5000/hello");
[Link]([Link]());
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:
- [Link].*
- [Link].*
Handling HTTP Requests and Responses:
- doGet(): handles GET requests.
- doPost(): handles POST requests.
Servlet Example:
import [Link].*;
Java Notes - Unit 5: JDBC, RMI and Servlets
import [Link].*;
import [Link].*;
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<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.