SlideShare a Scribd company logo
1 of 16
Download to read offline
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
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();
}
}
}
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;
}
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();
}
}
}
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)
);
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();
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();
}
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;
}
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";
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++) {
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();
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();
}
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";
}
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();
}
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";
}
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();
}

More Related Content

What's hot

System models for distributed and cloud computing
System models for distributed and cloud computingSystem models for distributed and cloud computing
System models for distributed and cloud computing
purplesea
 
Ip spoofing ppt
Ip spoofing pptIp spoofing ppt
Ip spoofing ppt
Anushakp9
 
Virtualization (Distributed computing)
Virtualization (Distributed computing)Virtualization (Distributed computing)
Virtualization (Distributed computing)
Sri Prasanna
 

What's hot (20)

MapReduce in Cloud Computing
MapReduce in Cloud ComputingMapReduce in Cloud Computing
MapReduce in Cloud Computing
 
Scheduling in cloud
Scheduling in cloudScheduling in cloud
Scheduling in cloud
 
System models in distributed system
System models in distributed systemSystem models in distributed system
System models in distributed system
 
Java rmi
Java rmiJava rmi
Java rmi
 
Mobile Cloud Computing
Mobile Cloud ComputingMobile Cloud Computing
Mobile Cloud Computing
 
2 vm provisioning
2 vm provisioning2 vm provisioning
2 vm provisioning
 
CS8791 Unit 2 Cloud Enabling Technologies
CS8791 Unit 2 Cloud Enabling TechnologiesCS8791 Unit 2 Cloud Enabling Technologies
CS8791 Unit 2 Cloud Enabling Technologies
 
Cloud Computing: Virtualization
Cloud Computing: VirtualizationCloud Computing: Virtualization
Cloud Computing: Virtualization
 
Levels of Virtualization.docx
Levels of Virtualization.docxLevels of Virtualization.docx
Levels of Virtualization.docx
 
Cloud Computing Principles and Paradigms: 5 virtual machines provisioning and...
Cloud Computing Principles and Paradigms: 5 virtual machines provisioning and...Cloud Computing Principles and Paradigms: 5 virtual machines provisioning and...
Cloud Computing Principles and Paradigms: 5 virtual machines provisioning and...
 
Virtualization in cloud computing ppt
Virtualization in cloud computing pptVirtualization in cloud computing ppt
Virtualization in cloud computing ppt
 
System models for distributed and cloud computing
System models for distributed and cloud computingSystem models for distributed and cloud computing
System models for distributed and cloud computing
 
Google App Engine
Google App EngineGoogle App Engine
Google App Engine
 
RPC: Remote procedure call
RPC: Remote procedure callRPC: Remote procedure call
RPC: Remote procedure call
 
Cloud computing using virtualization (Virtual Data Center)
Cloud computing using virtualization (Virtual Data Center)Cloud computing using virtualization (Virtual Data Center)
Cloud computing using virtualization (Virtual Data Center)
 
Cloud Security, Standards and Applications
Cloud Security, Standards and ApplicationsCloud Security, Standards and Applications
Cloud Security, Standards and Applications
 
Ip spoofing ppt
Ip spoofing pptIp spoofing ppt
Ip spoofing ppt
 
cloud computing: Vm migration
cloud computing: Vm migrationcloud computing: Vm migration
cloud computing: Vm migration
 
Virtualization (Distributed computing)
Virtualization (Distributed computing)Virtualization (Distributed computing)
Virtualization (Distributed computing)
 
6.Distributed Operating Systems
6.Distributed Operating Systems6.Distributed Operating Systems
6.Distributed Operating Systems
 

Similar to RMI Java Programming Lab Manual 2019

Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdf
arshiartpalace
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programming
Anung Ariwibowo
 

Similar to RMI Java Programming Lab Manual 2019 (20)

Anti patterns
Anti patternsAnti patterns
Anti patterns
 
Painless Persistence with Realm
Painless Persistence with RealmPainless Persistence with Realm
Painless Persistence with Realm
 
Run rmi
Run rmiRun rmi
Run rmi
 
In kor we Trust
In kor we TrustIn kor we Trust
In kor we Trust
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdf
 
Java rmi
Java rmiJava rmi
Java rmi
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programming
 
The... Wonderful? World of Lambdas
The... Wonderful? World of LambdasThe... Wonderful? World of Lambdas
The... Wonderful? World of Lambdas
 
Java VS Python
Java VS PythonJava VS Python
Java VS Python
 
What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
 
My java file
My java fileMy java file
My java file
 
#5 (Remote Method Invocation)
#5 (Remote Method Invocation)#5 (Remote Method Invocation)
#5 (Remote Method Invocation)
 
TechTalk - Dotnet
TechTalk - DotnetTechTalk - Dotnet
TechTalk - Dotnet
 
3 database-jdbc(1)
3 database-jdbc(1)3 database-jdbc(1)
3 database-jdbc(1)
 
Network security mannual (2)
Network security mannual (2)Network security mannual (2)
Network security mannual (2)
 
Java final lab
Java final labJava final lab
Java final lab
 
JDBC Tutorial
JDBC TutorialJDBC Tutorial
JDBC Tutorial
 
IKH331-07-java-rmi
IKH331-07-java-rmiIKH331-07-java-rmi
IKH331-07-java-rmi
 
Jdbc ja
Jdbc jaJdbc ja
Jdbc ja
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional Programming
 

Recently uploaded

Recently uploaded (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 

RMI Java Programming Lab Manual 2019

  • 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(); }