1. Assosa University
College of Computing and Informatics
Department of Computer Science
Int. to Distriburted Systems
Remote Method Invocation Lab. Manual
B.Sc. in Computer Science
Prepared by Gebreigziabher A. (M.Sc)
May, 2018
2. Assosa University College of Computing and Informatics
Department of Computer Science Distributed Systems Lab Manuallllllllllllll2
RMI Programming Lab. Manual
RMI Programming Lab 1 – Basics - Hello World!
1. RMI Interface
//RMIInterface.java
public interface RMIInterface extends Remote {
public String displayMessage() throws RemoteException;
}
2. RMI Server
//RMIServer.java
public class RMIServer extends UnicastRemoteObject implements RMIInterface {
public RMIServer() throws RemoteException {
//Default Constructor
}
public String displayMessage() throws RemoteException {
return "Hello World! This is Response from Server!";
}
public static void main(String[] args) {
try {
Registry r = LocateRegistry.createRegistry(3232);
RMIServer s = new RMIServer();
r.rebind("x", s);
System.out.println("The Server is Running!");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
3. Assosa University College of Computing and Informatics
Department of Computer Science Distributed Systems Lab Manuallllllllllllll3
3. RMI Client
//RMIClient.java
public class RMIClient {
public static void main(String[] args) {
// TODO code application logic here
try {
RMIInterface ri = (RMIInterface) Naming.lookup("rmi://localhost:3232/x");
String res = ri.displayMessage(); //Method Invocation
System.out.println(res);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
RMI Programming Lab 2 – Basics - Mathematical Operations
1. RMI Interface
//RMIInterface.java
public interface RMIInterface extends Remote {
public double add(double a, double b) throws RemoteException;
public double subtract(double a, double b) throws RemoteException;
public double computeSquareRoot(double a) throws RemoteException;
public double raise(double a, double b) throws RemoteException;
}
2. RMI Server
//RMIServer.java
public class RMIServer extends UnicastRemoteObject implements RMIInterface {
public RMIServer() throws RemoteException {
//Default Constructor
}
public double add(double a, double b) throws RemoteException {
return a + b;
}
4. Assosa University College of Computing and Informatics
Department of Computer Science Distributed Systems Lab Manuallllllllllllll4
public double subtract(double a, double b) throws RemoteException {
return a - b;
}
public double computeSquareRoot(double a) throws RemoteException{
return Math.sqrt(a);
}
public double raise(double a, double b) throws RemoteException{
return Math.pow(a,b);
}
public static void main(String[] args) {
try {
Registry r = LocateRegistry.createRegistry(3232);
RMIServer s = new RMIServer();
r.rebind("x", s);
System.out.println("Server is Ready!");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
3. RMI Client
//RMIClient.java
public class RMIClient {
public static void main(String[] args) {
// TODO code application logic here
try {
double a = 9, b = 3;
RMIInterface ri = (RMIInterface) Naming.lookup("rmi://localhost:3232/x");
double sum = ri.add(a, b);
System.out.println("The Sum = " + sum);
double diff = ri.subtract(a, b);
System.out.println("The Diff =" + diff);
double sq = ri.computeSquareRoot(a);
System.out.println("The SQRT =" + sq);
double p = ri.raise(a, b);
System.out.println("Power = " + p);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
5. Assosa University College of Computing and Informatics
Department of Computer Science Distributed Systems Lab Manuallllllllllllll5
RMI Lab Programming – JDBC
RMI Programming Lab 3 – JDBC – Database Design
CREATE DATABASE phonedir;
USE phonedir;
CREATE TABLE IF NOT EXISTS contact (
contactname varchar(20) NOT NULL,
phoneno varchar(20) NOT NULL,
PRIMARY KEY (phoneno)
);
6. Assosa University College of Computing and Informatics
Department of Computer Science Distributed Systems Lab Manuallllllllllllll6
RMI Programming Lab 3 – JDBC – INSERT OPERATION
1. RMI Interface
//RMIInterface.java
public interface RMIInterface extends Remote {
public String addContact(String contactName, String phoneNo) throws RemoteException;
}
2. RMI Server
//RMIServer.java
public class RMIServer extends UnicastRemoteObject implements RMIInterface {
static final String DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/phonedir";
Connection conn = null;
Statement stmt = null;
ResultSet rs;
public RMIServer() throws RemoteException {
}
public String addContact(String contactName, String phoneNo) throws RemoteException {
String phoneNumber = "", val = "";
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(DB_URL, "root", "");
stmt = conn.createStatement();
String sql = "SELECT * FROM contact WHERE phoneno = '" + phoneNo + "'";
rs = stmt.executeQuery(sql);
while (rs.next()) {
phoneNumber = rs.getString("phoneno");
}
if (phoneNo.intern().equals(phoneNumber)) {
val = "0"; //Duplicate
} else if (!phoneNo.intern().equals(phoneNumber)) {
stmt = conn.createStatement();
String sql1 = "INSERT INTO contact(contactname, phoneno) VALUES('" +
contactName + "','" + phoneNo + "')";
stmt.executeUpdate(sql1);
val = "1"; //Successfully Registered
}
} catch (Exception ex) {
ex.printStackTrace();
7. Assosa University College of Computing and Informatics
Department of Computer Science Distributed Systems Lab Manuallllllllllllll7
}
return val;
}
public static void main(String[] args) {
try {
Registry r = LocateRegistry.createRegistry(1010);
RMIServer rs = new RMIServer();
r.rebind("x", rs);
System.out.println("Server Ready!");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
3. RMI Client
//RMIClient.java: Inside btnRegisterActionPerformed
try {
String contactName = txtName.getText();
String phoneNo = txtPhoneNo.getText();
RMIInterface ri = (RMIInterface) Naming.lookup("rmi://localhost:1010/x");
String res = ri.addContact(contactName, phoneNo);
if (res.equals("1")) {
JOptionPane.showMessageDialog(this, "Contact has been Registered!", "Phone
Dir: Contact Registered.", JOptionPane.INFORMATION_MESSAGE);
} else if (res.equals("0")) {
JOptionPane.showMessageDialog(this, "Contact already Exist!", "Phone Dir:
Contact Exist.", JOptionPane.INFORMATION_MESSAGE);
}
} catch (Exception ex) {
ex.printStackTrace();
}
8. Assosa University College of Computing and Informatics
Department of Computer Science Distributed Systems Lab Manuallllllllllllll8
RMI Programming Lab 4 – JDBC – SELECT OPERATION
1. RMI Interface
//RMIInterface.java
public interface RMIInterface extends Remote {
public ArrayList searchContact(String phoneNo) throws RemoteException;
}
2. RMI Server
//RMIServer.java
public class RMIServer extends UnicastRemoteObject implements RMIInterface {
static final String DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/phonedir";
Connection conn = null;
Statement stmt = null;
ResultSet rs;
public RMIServer() throws RemoteException {
}
public ArrayList searchContact(String phoneNo) throws RemoteException {
ArrayList array = new ArrayList();
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(DB_URL, "root", "");
stmt = conn.createStatement();
String sql = "SELECT *FROM contact WHERE phoneno ='" + phoneNo + "'";
rs = stmt.executeQuery(sql);
while (rs.next()) {
for (int i = 0; i < 2; i++) {
array.add(rs.getString(i + 1));
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return array;
}
9. Assosa University College of Computing and Informatics
Department of Computer Science Distributed Systems Lab Manuallllllllllllll9
public static void main(String[] args) {
try {
Registry r = LocateRegistry.createRegistry(1010);
RMIServer rs = new RMIServer();
r.rebind("x", rs);
System.out.println("Server Ready!");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
3. RMI Client
//RMIClient.java: Inside btnSearchActionPerformed
ArrayList array = new ArrayList();
try {
String phoneNo = txtSearchPhone.getText();
RMIInterface ri = (RMIInterface) Naming.lookup("rmi://localhost:1010/x");
for (int i = 0; i < 2; i++) {
array = ri.searchContact(phoneNo);
}
if (!array.isEmpty()) {
txtName.setText(array.get(0).toString());
txtPhoneNo.setText(array.get(1).toString());
} else if (array.isEmpty()) {
JOptionPane.showMessageDialog(this, "No record found.", "Phone Dire: No
Record Found", JOptionPane.INFORMATION_MESSAGE);
}
} catch (Exception ex) {
ex.printStackTrace();
}
RMI Programming Lab 5 – JDBC – SELECT OPERATION
1. RMI Interface
//RMIInterface.java
public interface RMIInterface extends Remote {
public ArrayList viewAllContacts(int i) throws RemoteException;
}
2. RMI Server
//RMIServer.java
public class RMIServer extends UnicastRemoteObject implements RMIInterface {
static final String DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/phonedir";
10. Assosa University College of Computing and Informatics
Department of Computer Science Distributed Systems Lab Manuallllllllllllll10
Connection conn = null;
Statement stmt = null;
ResultSet rs;
public RMIServer() throws RemoteException {
}
public ArrayList viewAllContacts(int i) throws RemoteException {
ArrayList array = new ArrayList();
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(DB_URL, "root", "");
stmt = conn.createStatement();
String sql = "SELECT contactname, phoneno FROM contact ORDER BY contactname DESC";
stmt.execute(sql);
rs = stmt.getResultSet();
while (rs.next()) {
array.add(rs.getObject(i + 1));
}
} catch (Exception ex) {
ex.printStackTrace();
}
return array;
}
public static void main(String[] args) {
try {
Registry r = LocateRegistry.createRegistry(1010);
RMIServer rs = new RMIServer();
r.rebind("x", rs);
System.out.println("Server Ready!");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
3. RMI Client
//RMIClient.java: Inside btnViewAllActionPerformed
DefaultTableModel model = (DefaultTableModel) tblContacts.getModel();
ArrayList array = new ArrayList();
try {
RMIInterface ri = (RMIInterface) Naming.lookup("rmi://localhost:1010/x");
int total = 0;
for (int i = 0; i < 2; i++) {
11. Assosa University College of Computing and Informatics
Department of Computer Science Distributed Systems Lab Manuallllllllllllll11
array = ri.viewAllContacts(i);
for (int j = 0; j < array.size(); j++) {
model.setValueAt(array.get(j), j, i);
}
total = array.size();
}
if (total == 0) {
JOptionPane.showMessageDialog(this, "No Contact Found!", "Tele Directory: No
Contact Exist.", JOptionPane.INFORMATION_MESSAGE);
}
} catch (Exception ex) {
ex.printStackTrace();
}
RMI Programming Lab 6 – JDBC – UPDATE OPERATION
1. RMI Interface
//RMIInterface.java
public interface RMIInterface extends Remote {
public String updateContact(String contactName, String phoneNo) throws
RemoteException;
}
2. RMI Server
//RMIServer.java
public class RMIServer extends UnicastRemoteObject implements RMIInterface {
static final String DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/phonedir";
Connection conn = null;
Statement stmt = null;
PreparedStatement prst;
ResultSet rs;
public RMIServer() throws RemoteException {
}
public String updateContact(String contactName, String phoneNo) throws
RemoteException {
String val = "", CPHONE = "";
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(DB_URL, "root", "");
stmt = conn.createStatement();
12. Assosa University College of Computing and Informatics
Department of Computer Science Distributed Systems Lab Manuallllllllllllll12
String sql = "SELECT *FROM contact WHERE phoneno = '" + phoneNo + "'";
rs = stmt.executeQuery(sql);
while (rs.next()) {
CPHONE = rs.getString("phoneno");
}
if (!phoneNo.intern().equals(CPHONE)) {
val = "0";
} else if (phoneNo.intern().equals(CPHONE)) {
prst = conn.prepareStatement("UPDATE contact SET contactname = '" +
contactName + "' WHERE phoneno = '" + phoneNo + "'");
prst.executeUpdate();
val = "1";
}
} catch (Exception ex) {
ex.printStackTrace();
}
return val;
}
public static void main(String[] args) {
try {
Registry r = LocateRegistry.createRegistry(1010);
RMIServer rs = new RMIServer();
r.rebind("x", rs);
System.out.println("Server Ready!");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
3. RMI Client
//RMIClient.java: Inside btnUpdateActionPerformed
try {
String contactName = txtName.getText();
String phoneNo = txtPhoneNo.getText();
RMIInterface ri = (RMIInterface) Naming.lookup("rmi://localhost:1010/x");
String result = ri.updateContact(contactName, phoneNo);
if (result.equals("1")) {
JOptionPane.showMessageDialog(this, "Contact Updated Successfully!", "Tele Dir:
Contact Updated.", JOptionPane.INFORMATION_MESSAGE);
} else if (result.equals("0")) {
JOptionPane.showMessageDialog(this, "Contact Doesn't Exist!", "Tele Dir: No
Contact.", JOptionPane.INFORMATION_MESSAGE);
}
} catch (Exception e) {
e.printStackTrace();
}
13. Assosa University College of Computing and Informatics
Department of Computer Science Distributed Systems Lab Manuallllllllllllll13
RMI Programming Lab 7 – JDBC – DELETE OPERATION
1. RMI Interface
//RMIInterface.java
public interface RMIInterface extends Remote {
public String deleteContact(String phoneNo) throws RemoteException;
}
2. RMI Server
//RMIServer.java
public class RMIServer extends UnicastRemoteObject implements RMIInterface {
static final String DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/phonedir";
Connection conn = null;
Statement stmt = null;
ResultSet rs;
public RMIServer() throws RemoteException {
}
public String deleteContact(String phoneNo) throws RemoteException {
String val = "", CPHONE = "";
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(DB_URL, "root", "");
stmt = conn.createStatement();
String sql = "SELECT *FROM contact WHERE phoneno = '" + phoneNo + "'";
rs = stmt.executeQuery(sql);
while (rs.next()) {
CPHONE = rs.getString("phoneno");
}
if (!phoneNo.intern().equals(CPHONE)) {
val = "0";
}
else if (phoneNo.intern().equals(CPHONE)) {
stmt = conn.createStatement();
String sql2 = "DELETE FROM contact WHERE phoneno = '"+phoneNo+"'";
stmt.executeUpdate(sql2);
val = "1";
}
14. Assosa University College of Computing and Informatics
Department of Computer Science Distributed Systems Lab Manuallllllllllllll14
} catch (Exception ex) {
ex.printStackTrace();
}
return val;
}
public static void main(String[] args) {
try {
Registry r = LocateRegistry.createRegistry(1010);
RMIServer rs = new RMIServer();
r.rebind("x", rs);
System.out.println("Server Ready!");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
3. RMI Client
//RMIClient.java: Inside btnDeleteActionPerformed
try {
String phoneNo = txtPhoneNo.getText();
RMIInterface ri = (RMIInterface) Naming.lookup("rmi://localhost:1010/x");
String result = ri.deleteContact(phoneNo);
if (result.equals("1")) {
JOptionPane.showMessageDialog(this, "Contact Deleted Successfully!", "Tele Dir:
Contact Deleted.", JOptionPane.INFORMATION_MESSAGE);
} else if (result.equals("0")) {
JOptionPane.showMessageDialog(this, "Contact Doesn't Exist!", "Tele Dir: No
Contact.", JOptionPane.INFORMATION_MESSAGE);
}
} catch (Exception e) {
e.printStackTrace();
}
15. Assosa University College of Computing and Informatics
Department of Computer Science Distributed Systems Lab Manuallllllllllllll15
RMI Programming Lab 8 – JDBC – SELECT OPERATION - LOGIN
1. RMI Interface
//RMIInterface.java
public interface RMIInterface extends Remote {
public String login(String username, String password) throws RemoteException;
}
2. RMI Server
//RMIServer.java
public class RMIServer extends UnicastRemoteObject implements RMIInterface {
static final String DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/phonedirectory";
Connection conn = null;
Statement stmt = null;
ResultSet rs;
public RMIServer() throws RemoteException {
}
public String login(String userName, String password) throws RemoteException {
String uname = "", pass = "", val="";
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(DB_URL, "root", "");
stmt = conn.createStatement();
String query = "SELECT * FROM useraccount WHERE username ='" + userName + "'";
rs = stmt.executeQuery(query);
while (rs.next()) {
uname = rs.getString("username");
pass = rs.getString("password");
}
if ((userName.intern().equals(uname.intern()) &&
(password.intern().equals(pass.intern())))) {
val = "1";
} else if ((!userName.intern().equals(uname.intern()) &&
(!password.intern().equals(pass.intern())))) {
val = "0";
}
16. Assosa University College of Computing and Informatics
Department of Computer Science Distributed Systems Lab Manuallllllllllllll16
conn.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return val;
}
public static void main(String[] args) {
try {
Registry r = LocateRegistry.createRegistry(1010);
RMIServer rs = new RMIServer();
r.rebind("x", rs);
System.out.println("Server Ready!");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
4. RMI Client
//RMIClient.java: Inside btnDeleteActionPerformed
try {
String userName = txtUserName.getText();
String password = txtPassword.getText();
RMIInterface ri = (RMIInterface) Naming.lookup("rmi://localhost:1010/x");
String res = ri.login(userName, password);
if (res.equals("1")) {
frmViewAllContacts vac = new frmViewAllContacts();
vac.setVisible(true);
this.dispose();
} else if (res.equals("0")) {
JOptionPane.showMessageDialog(this, "Incorrect User Name or Password! Please
Try Again!", "Phone Directory.", JOptionPane.INFORMATION_MESSAGE);
}
} catch (Exception ex) {
ex.printStackTrace();
}