Java Architecture: MVC, JDBC, RMI, and SQL Essentials

MVC Design Pattern

MVC is a design pattern that divides an application into three interconnected components to separate concerns, improve maintainability, and support scalable development.

Components of MVC

  • 1. Model: Represents business logic and data. It performs calculations, validations, and database operations. It is independent of the UI. Example: A Java class handling data.
  • 2. View: Represents the user interface (UI). It displays data from the Model and contains no business logic. Example: Swing GUI, JSP page.
  • 3. Controller: Acts as an intermediary between the Model and the View. It handles user input and updates the Model/View accordingly. Example: ActionListener in Swing, Servlet in web apps.

How MVC Separates Concerns

  • UI changes do not affect business logic.
  • Business logic can be reused.
  • Easier testing and maintenance.
  • Parallel development (UI and logic separately).

MVC Implementation Example

Model (Student.java)

public class Student {
    private String name;
    private int rollNo;

    public Student(String name, int rollNo) {
        this.name = name;
        this.rollNo = rollNo;
    }

    public String getName() {
        return name;
    }

    public int getRollNo() {
        return rollNo;
    }
}

View (StudentView.java)

public class StudentView {
    public void displayStudentDetails(String name, int rollNo) {
        System.out.println("Student Name: " + name);
        System.out.println("Roll Number: " + rollNo);
    }
}

Controller (StudentController.java)

public class StudentController {
    private Student model;
    private StudentView view;

    public StudentController(Student model, StudentView view) {
        this.model = model;
        this.view = view;
    }

    public void updateView() {
        view.displayStudentDetails(model.getName(), model.getRollNo());
    }
}

Main Class (MVCExample.java)

public class MVCExample {
    public static void main(String[] args) {
        Student model = new Student("Ram", 101);
        StudentView view = new StudentView();
        StudentController controller = new StudentController(model, view);

        controller.updateView();
    }
}

JDBC Drivers

A JDBC (Java Database Connectivity) driver is a software component that enables Java applications to connect and interact with a database. It acts as a bridge between the Java program and the database.

Types of JDBC Drivers

  • Type 1: JDBC-ODBC Bridge Driver
    • Uses an ODBC driver to connect to the database.
    • Converts JDBC calls into ODBC calls.
    • Pros: Can connect to any database with ODBC.
    • Cons: Slow, requires ODBC setup, not recommended now.
  • Type 2: Native-API Driver
    • Converts JDBC calls into database-specific native calls.
    • Pros: Faster than Type 1.
    • Cons: Database-specific, platform-dependent.
  • Type 3: Network Protocol Driver
    • JDBC calls → middleware → database.
    • Middleware translates calls to the database protocol.
    • Pros: Database-independent, good for internet applications.
    • Cons: Needs a middleware server.
  • Type 4: Thin Driver / Pure Java Driver
    • Converts JDBC calls directly into the database-specific protocol.
    • Pros: Fast, platform-independent, widely used.
    • Cons: Database-specific (one driver per database type).

Java Remote Method Invocation (RMI)

Java RMI allows an object running on one JVM to invoke methods on an object running on another JVM (possibly on a different machine).

Java RMI Architecture Components

  1. Client: Requests remote services and uses a stub to communicate.
  2. Server: Hosts the remote object and implements the remote interface.
  3. Remote Interface: Declares remote methods and extends java.rmi.Remote.
  4. Stub: Client-side proxy that sends method requests to the server.
  5. Skeleton: Handled internally in modern Java.
  6. RMI Registry: A naming service that stores references to remote objects.

How RMI Enables Distributed Communication

  • The client calls a method on the stub.
  • The stub serializes the request and sends it to the server.
  • The server executes the method on the remote object.
  • The result is sent back to the client.
  • The client receives the response transparently.

RMI Implementation Example

Remote Interface (Hello.java)

import java.rmi.Remote;
import java.rmi.RemoteException;

// Remote interface must extend Remote
public interface Hello extends Remote {
    String sayHello() throws RemoteException;
}

Implementation (HelloImpl.java)

import java.rmi.server.UnicastRemoteObject; // Makes this object accessible over the network.
import java.rmi.RemoteException;

// Implementation of remote interface
public class HelloImpl extends UnicastRemoteObject implements Hello {
    protected HelloImpl() throws RemoteException {
        super();
    }

    @Override
    public String sayHello() throws RemoteException {
        return "Hello from RMI Server!";
    }
}

Server (Server.java)

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Server {
    public static void main(String[] args) {
        try {
            // Create remote object
            HelloImpl obj = new HelloImpl();

            // Start RMI registry on port 1099
            Registry registry = LocateRegistry.createRegistry(1099);

            // Bind remote object to registry
            registry.rebind("Hello", obj);

            System.out.println("RMI Server is ready.");
        } catch (Exception e) {
            System.out.println("Server exception: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Client (Client.java)

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Client {
    public static void main(String[] args) {
        try {
            // Connect to RMI registry on localhost
            Registry registry = LocateRegistry.getRegistry("localhost", 1099);

            // Lookup the remote object
            Hello stub = (Hello) registry.lookup("Hello");

            // Call remote method
            String response = stub.sayHello();
            System.out.println("Response from server: " + response);
        } catch (Exception e) {
            System.out.println("Client exception: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

SQL and Database Interaction

SQL is the standard language for relational databases. JDBC uses SQL to interact with databases.

Common SQL Statements

  • DDL (Data Definition Language): CREATE, ALTER, DROP
  • DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE
  • DCL (Data Control Language): GRANT, REVOKE
  • TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT