SlideShare a Scribd company logo
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

Program security
Program securityProgram security
Program security
G Prachi
 
Networking in java, Advanced programming
Networking in java, Advanced programmingNetworking in java, Advanced programming
Networking in java, Advanced programming
Gera Paulos
 
NETWORK PENETRATION TESTING
NETWORK PENETRATION TESTINGNETWORK PENETRATION TESTING
NETWORK PENETRATION TESTING
Er Vivek Rana
 
Transport Layer Security (TLS)
Transport Layer Security (TLS)Transport Layer Security (TLS)
Transport Layer Security (TLS)
Arun Shukla
 
Distributed system architecture
Distributed system architectureDistributed system architecture
Distributed system architecture
Yisal Khan
 
Multichannel User Interfaces
Multichannel User InterfacesMultichannel User Interfaces
Multichannel User Interfaces
Icinetic
 
Middleware
MiddlewareMiddleware
Middleware
Dr. Uday Saikia
 
Remote Procedure Call in Distributed System
Remote Procedure Call in Distributed SystemRemote Procedure Call in Distributed System
Remote Procedure Call in Distributed System
PoojaBele1
 
Uml Common Mechanism
Uml Common MechanismUml Common Mechanism
Uml Common Mechanism
Satyamevjayte Haxor
 
Seven step model of migration into the cloud
Seven step model of migration into the cloudSeven step model of migration into the cloud
Seven step model of migration into the cloud
Raj Raj
 
Remote invocation
Remote invocationRemote invocation
Remote invocation
ishapadhy
 
Java RMI
Java RMIJava RMI
Java RMI
Ankit Desai
 
Wireshark Basic Presentation
Wireshark Basic PresentationWireshark Basic Presentation
Wireshark Basic Presentation
MD. SHORIFUL ISLAM
 
Tools and methods used in cyber crime
Tools and methods used in cyber crimeTools and methods used in cyber crime
Tools and methods used in cyber crime
shubhravrat Deshpande
 
Middleware in Distributed System-RPC,RMI
Middleware in Distributed System-RPC,RMIMiddleware in Distributed System-RPC,RMI
Middleware in Distributed System-RPC,RMI
Prajakta Rane
 
Iot Service Layer Evolution
Iot Service Layer EvolutionIot Service Layer Evolution
Iot Service Layer Evolution
oneM2M
 
Public key cryptography and message authentication
Public key cryptography and message authenticationPublic key cryptography and message authentication
Public key cryptography and message authentication
CAS
 
Types of attacks
Types of attacksTypes of attacks
Types of attacks
Vivek Gandhi
 
Distributed System ppt
Distributed System pptDistributed System ppt

What's hot (20)

Program security
Program securityProgram security
Program security
 
Networking in java, Advanced programming
Networking in java, Advanced programmingNetworking in java, Advanced programming
Networking in java, Advanced programming
 
NETWORK PENETRATION TESTING
NETWORK PENETRATION TESTINGNETWORK PENETRATION TESTING
NETWORK PENETRATION TESTING
 
Transport Layer Security (TLS)
Transport Layer Security (TLS)Transport Layer Security (TLS)
Transport Layer Security (TLS)
 
Distributed system architecture
Distributed system architectureDistributed system architecture
Distributed system architecture
 
Multichannel User Interfaces
Multichannel User InterfacesMultichannel User Interfaces
Multichannel User Interfaces
 
Middleware
MiddlewareMiddleware
Middleware
 
Remote Procedure Call in Distributed System
Remote Procedure Call in Distributed SystemRemote Procedure Call in Distributed System
Remote Procedure Call in Distributed System
 
Uml Common Mechanism
Uml Common MechanismUml Common Mechanism
Uml Common Mechanism
 
Seven step model of migration into the cloud
Seven step model of migration into the cloudSeven step model of migration into the cloud
Seven step model of migration into the cloud
 
Remote invocation
Remote invocationRemote invocation
Remote invocation
 
IoT Protocol Stack.pdf
IoT Protocol Stack.pdfIoT Protocol Stack.pdf
IoT Protocol Stack.pdf
 
Java RMI
Java RMIJava RMI
Java RMI
 
Wireshark Basic Presentation
Wireshark Basic PresentationWireshark Basic Presentation
Wireshark Basic Presentation
 
Tools and methods used in cyber crime
Tools and methods used in cyber crimeTools and methods used in cyber crime
Tools and methods used in cyber crime
 
Middleware in Distributed System-RPC,RMI
Middleware in Distributed System-RPC,RMIMiddleware in Distributed System-RPC,RMI
Middleware in Distributed System-RPC,RMI
 
Iot Service Layer Evolution
Iot Service Layer EvolutionIot Service Layer Evolution
Iot Service Layer Evolution
 
Public key cryptography and message authentication
Public key cryptography and message authenticationPublic key cryptography and message authentication
Public key cryptography and message authentication
 
Types of attacks
Types of attacksTypes of attacks
Types of attacks
 
Distributed System ppt
Distributed System pptDistributed System ppt
Distributed System ppt
 

Similar to RMI Java Programming Lab Manual 2019

Painless Persistence with Realm
Painless Persistence with RealmPainless Persistence with Realm
Painless Persistence with Realm
Christian Melchior
 
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-programmingAnung Ariwibowo
 
The... Wonderful? World of Lambdas
The... Wonderful? World of LambdasThe... Wonderful? World of Lambdas
The... Wonderful? World of Lambdas
Esther Lozano
 
Java VS Python
Java VS PythonJava VS Python
Java VS Python
Simone Federici
 
What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
Sandeep Kr. Singh
 
#5 (Remote Method Invocation)
#5 (Remote Method Invocation)#5 (Remote Method Invocation)
#5 (Remote Method Invocation)
Ghadeer AlHasan
 
3 database-jdbc(1)
3 database-jdbc(1)3 database-jdbc(1)
3 database-jdbc(1)
hameedkhan2017
 
Network security mannual (2)
Network security mannual (2)Network security mannual (2)
Network security mannual (2)
Vivek Kumar Sinha
 
Java final lab
Java final labJava final lab
Java final lab
Vivek Kumar Sinha
 
Jdbc ja
Jdbc jaJdbc ja
Jdbc ja
DEEPIKA T
 

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

Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 

Recently uploaded (20)

Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 

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