The Artima Developer Community
Sponsored Link

Java Seminars by Bill Venners
RMI and JDBC
Lecture Handout

This lecture gives a quick introduction to RMI (Remote Method Invocation) and JDBC (Java Database Connectivity).

Agenda


RMI


Stubs and Skeletons


RMI Steps


The Summer Server Revisited

// In file rmi/ex1/Summer.java
import java.rmi.*;
public interface Summer extends Remote {
    static class InvalidLongException
        extends Exception {
        private String message;
        InvalidLongException(String s) {
            message = s;
        }
        public String getMessage() {
            return message;
        }
    }
    long sumString(String s)
        throws InvalidLongException,
        RemoteException;
}
// In file rmi/ex1/SummerServer.java
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.net.*;
import java.io.*;
import java.util.StringTokenizer;
public class SummerServer
        extends UnicastRemoteObject
        implements Summer {
    public static final int PORT = 1099;
    public SummerServer() throws RemoteException {
    }
    public static void main(String[] args) {

        System.setSecurityManager(
            new RMISecurityManager());
        try {
            LocateRegistry.createRegistry(PORT);
            SummerServer summer = new SummerServer();
            System.out.println("Ready to rebind");
            Naming.rebind("SUMMER", summer);
            System.out.println("Ready to sum");
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    public long sumString(String s)
        throws Summer.InvalidLongException, RemoteException {
        long sum = 0;
        StringTokenizer st = new StringTokenizer(s);
        String token;
        while (st.hasMoreTokens()) {
            token = st.nextToken();
            try {
                sum += Long.parseLong(token);
            }
            catch (NumberFormatException e) {
                throw new Summer.InvalidLongException(
                    "Invalid number: " + token);
            }
        }

        return sum;
    }
}
// In file rmi/ex1/SummerClient.java
import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;
import java.io.*;
public class SummerClient {
    public static void main(String[] args) {
        System.setSecurityManager(
            new RMISecurityManager());
        try {
            Summer summer = (Summer) Naming.lookup(
                "rmi://localhost:1099/SUMMER");
            LineNumberReader stdinReader =
                new LineNumberReader(new BufferedReader(
                new InputStreamReader(System.in)));
            for (;;) {
                String userLine = stdinReader.readLine();
                if (userLine == null
                    || userLine.length() == 0) {
                    break;
                }
                String outString;
                try {
                    long sum = summer.sumString(userLine);
                    outString = Long.toString(sum);
                }
                catch(Summer.InvalidLongException e) {
                    outString = e.getMessage();
                }
                System.out.println(outString);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

JDBC


JDBC Steps


The Student Database


The Student Sorter Application

// In file jdbc/ex1/StudentSorter.java
import java.sql.*;
public class StudentSorter {
    public static void main(String[] args) {
        try {
            Class.forName(
                "sun.jdbc.odbc.JdbcOdbcDriver");
            Connection conn = DriverManager.getConnection(
                "jdbc:odbc:mydb", "", "");
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(
                "SELECT LAST, FIRST, TOTAL " +
                "FROM students.csv mydb " +
                "ORDER BY TOTAL DESC");
            while (rs.next()) {
                String s = rs.getString("FIRST") + " "
                    + rs.getString("LAST") + ": "
                    + rs.getString("TOTAL");
                System.out.println(s);
            }
            stmt.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}
Here's the the output of StudentSorter:
Joe Java: 400
Jane Java: 400
Bart Smart: 360
Lizzy Busy: 200
Joe Slow: 0

Sponsored Links

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use