You are on page 1of 64

RMI

*.Create distributed name server like DNS using RMI

//DNS PROGRAM
//DnsInter.java: import java.rmi.*; public interface DnsInt extends Remote { public String find(String str) throws RemoteException; } //DnsImpl.java import java.rmi.*; import java.rmi.server.*; //import java .awt.*; public class DnsServer extends UnicastRemoteObject implements DnsInt { public DnsServer() throws RemoteException { TreeMap tm=new TreeMap(); tm.put("Google.com","200.199.255.56"); tm.put("Vce.in","224.186.222.07"); tm.put("Gmail.com","185.145.200.03"); Set st=tm.emptySet(); Iterator itr=st.iterator(); while(itr.hasNext()) { while.Entry.me=(Map.Entry) itr.next(); if(me.getKey()==Str) return "IP address:" + me.getvalue(); } return "No Matching IP"; } //DnsServermain.java import java.net.*; import java.rmi.*; public class DnsServerMain { public static void main(String args[]) { try

{ DnsServer ds=new DnsServer(); Naming.rebind('rmi//idea/host:1029/"DnsServer"',ds); } catch(Exception e) { System.out.println(e); } catch(exception e) { System.out.println(error); } } } //DnsClient.java import java.rmi.*; public class DnsClient { public static void main(String args[]) { try { String DnsServerURL="rmi//localhost:1029/DsnServer"; DnsServerInt dsi=(DnsInt)Naming.lookup(dsi); String m= DnsInt.find(args[0]); System.out.println(m); } catch(Exception e) { System.out.println("error",e); } } }

*.Program to develop a Remote Object for a Bank Application to find Balance Amount from the Bank Account The method should authenticate the user by Mens of account-no and password.Use AWT/SWING to create user interface and JDBC to connect to the database BankInter.java import java.rmi.*; public interface Rem extends Remote { public String validate(String acc,String passwd) throws RemoteException; } BankImpl.java import java.sql.*; import java.io.*; import java.rmi.*; import java.rmi.server.*; public class Rmip extends UnicastRemoteObject implements Rem { public Rmip() throws RemoteException { super(); } public String validate(String acc,String passwd) throws RemoteException { String bal=""; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:mydsn","scott","tiger"); Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery("Select * from bank where acno="+acc); System.out.println("Account no.:"+acc); String pwd1=""; while(rs.next()) { pwd1=rs.getString(2); } rs.close(); if(passwd.equals(pwd1)) { ResultSet rs1=stmt.executeQuery("Select * from bank where acno="+acc);

while(rs1.next()) { bal=rs1.getString(3); } rs1.close(); } else bal="Enter correct combination "; con.close(); } catch(Exception e) { e.printStackTrace(); } return bal; } } BankServer.java import java.rmi.*; import java.net.*; public class RmiServer { public static void main(String args[]) throws RemoteException { try { Rmip rmip=new Rmip(); Naming.rebind("MyServer",rmip); } catch(MalformedURLException e) { e.printStackTrace(); } System.out.println("Waiting for client Request...."); } } BankClient.java import java.awt.*; import java.awt.event.*; import java.sql.*; import java.io.*; import java.rmi.*; import java.rmi.server.*;

import java.rmi.registry.*; public class RmiClient extends Frame implements ActionListener { Button b11,b22; TextField t1,t2; Label l1,l2,l3,l4; public RmiClient() { setLayout(new FlowLayout(FlowLayout.LEFT,225,50)); t1=new TextField(15); l1= new Label("Account Number"); t2=new TextField(15); l2=new Label("Password "); t2.setEchoChar('*'); l3= new Label(" "); l4= new Label(" "); b11=new Button("Submit"); b22=new Button("Cancel"); add(l1); add(t1); add(l2); add(t2); add(b11); b11.addActionListener(this); add(b22); b22.addActionListener(this); add(l3); add(l4); setVisible(true); } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==b11) { try { Rem rem=(Rem)Naming.lookup("MyServer"); String str=rem.validate(t1.getText(),t2.getText()); l3.setText("Balance:"); l4.setText(str); } catch(Exception e) { e.printStackTrace(); }

} if(ae.getSource()==b22) { t1.setText(""); t2.setText(""); l3.setText(" "); l4.setText(" "); } } public static void main(String args[]) { RmiClient rc=new RmiClient(); } }

*.Develop Remote Object for an Employee application having a remote method to find employee details such as empid,ename,date_of_joining,service,salary with RMI EmployeeInter.java import javax.ejb.*; import java.rmi.*; public interface EmployeeInter extends Remote { String getDetails (int empid) throws Exception; String Increament(int salary) throws Exception; } EmployeeImpl.java import java.sql.*; import java.rmi.*; import java.rmi.server.*; public class EmployeeImpl extends UnicastRemoteObject implements EmplyeeInter { public String getDetails (int empid) throws RemoteException { try { Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); Connection con=DriverManager.getConnection(get DBURL()); Statement s=con.createStatement(select ename,date_of_join,service,salary from Employee where empid=+empid); ResultSet rs=s.executeQuery(query); rs.next(); if(!more) { rs.getString(enames); rs.getString(date_of_join); rs.getInt(service); rs.getInt(salary); } rs.close(); return ename,date_of_join,service,salary ; } public String Increament(int salary) throws RemoteException; { int totsalary;

if(salary>=10000) { DA=salary/30+500; HRA=salary+300; } else { DA=salary/30+400; HRA=salary/100+200; } } Employee Client.java import java.rmi.*; public class EmployeeClient { public static void main(String args[ ]) throws Exception { remote r=Naming.lookup(rmi://home-62138ac34c/reserveobj); EmployeeInter ei=(EmployeeInter) r; ei.getDetails( ); ei.Increament(Golconda,5); } } EmployeeServer.java import java.rmi.*; public class Employeeserver { public static void main(String args[ ])throws Exception { EmployeeImpl eip=new EmployeeImpl( ); Naming.rebind(employeeobj,eip); } }

*.Develop a remote object for railway reservation application having remote method to find status of reservation which when supplied by PNR number.Implement the above using RMI ReserveInter.java import javax.ejb.*; import java.rmi.*; public interface ReserveInter extends Remote { String getAvailList () throws Exception; String Reservation(String trainname,int wanted) throws RemoteException; } ReserveImpl.java import java.sql.*; import java.rmi.*; import java.rmi.server.*; public class ReserveImpl extends UnicastRemoteObject implements ReserveInter { public String getAvailList () throws RemoteException { try { Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); Connection con=DriverManager.getConnection(get DBURL()); Statement s=con.createStatement(select trainnames,timings,chargeamt,availableseats from TrainList where source=+source+destiny=+destiny); ResultSet rs=s.executeQuery(query); rs.next(); if(!more) { rs.getString(trainnames); rs.getString(timings); rs.getInt(chargeamt); rs.getInt(availableseats); } rs.close(); return trainnames,timings,chargeamt,availableseats; } public String Reservation(String trainname,int wanted) throws RemoteException;

{ String bankname; int creditcardno; int creditbal; int chargeamt; System.out.println(enter the trainname in which u want to travel); System.in(trainname); System.out.println(enter no of seats u want); System.in(wanted); if(availabeseats>=wanted) { System.out.println(seats available); //issue ticket System.out.println(enter the name of the bank in which u have credit card); System.in(bankname); System.out.println(enter ur creditcard no); System.in(creditcardno); for(i=1;1<=100;i++) { Statement s=con.createStatement(select creditbal from BankDB where bankname=+bankname+creditcardno=+creditcardno); if(i==creditcardno&&creditbal>=chargeamt ) { System.out.println(ur a/c has been verified); System.out.println(seat no+seat no+has been allotted to u with a charge of +chargeamt); System.in(chargeamt); System.out.println(ur seat has been confirmed); availableseats=availableseats-wanted; else System.out.println(seat may not be confirmed); } Reserve Client.java import java.rmi.*; public class ReserveClient { public static void main(String args[ ]) throws Exception { Remote r=Naming.lookup(rmi://home-62138ac34c/reserveobj); ReserveInter ri=(ReserveInter) r;

10

ri.getAvailList( ); ri.Reservation(Golconda,5); } } ReserveServer.java import java.rmi.*; public class ReserveServer { public static void main(String args[ ])throws Exception { ReserveImpl rip=new ReserveImpl( ); Naming.rebind(reserveobj,rip); } }

11

EJB
*.Consider an E-commerce application of Shopping Cart for purchasing things over the web.Implement the above application using EJB by considering necessary issues involved Online shopRemote.java package OnlineShopping; import javax.ejb.*; import java.rmi.*; public interface OnlineShopRemote extends EJBObject { Product[ ] getProductList () throws RemoteException; String Purchase(int itemid, int quantity) throws RemoteException; } OnlineShopHome.java package OnlineShopping; import java.ejb.*; import java.rmi.*; public interface OnlineShopHome extends EJBHome { public void create() throws CreateException,RemoteException; } OnlineShopClass.java package OnlineShopping; import javax.ejb.*; import java.uti.*; import java.rmi.*; public class OnlineShopClass implements SessionBean { SessionContext s=NULL; public void ejbC reate() { } public void ejbActivate() { } public void ejbPassivate() { } 12

public void ejbRemove() { } public void setSessionContext(SessionContext ctx) { s=ctx; } public Product[] getProductList () throws RemoteException { try { Connection con=DriverManager.getConnection(get DBURL()); String query=select * from ItemList; Statement s=con.createStatement(); resultSet rs=s.executeQuery(query); rs.next(); if(!more) { rs.getString(itemname); rs.getInt (iprice); rs.getInt(itemid); rs.getInt(quantity); } rs.close(); return itemid,itemname,quantity,iprice; } public String Purchase(int itemid,int quantity) throws RemoteException { int Price=0; int n; System.out.println(enter the no of items u want to buy); System.in(n); System.out.println(enter the itemid which u want to buy); System.in(itemid); System.out.println(enter quantity); System.in(quantity); if(n>1) { for(i=1;i=n;i++) { Price=Price+iprice; }

13

} //issue items System.out.println(enter the name of the bank in which u have credit card); System.in(bankname); System.out.println(enter ur creditcard no); System.in(creditcardno); Statement s=con.createStatement(select creditbal from BankDB where bankname=+bankname+creditcardno=+creditcardno); if(i==creditcardno&&creditbal>=Price ) { System.out.println(ur a/c has been verified); System.out.println(items with ids+itemid +has been allotted to u with a charge of +chargeamt); System.in(chargeamt); System.out.println(transaction completed); } OnlineShop Client.java import java.naming.*; import java.ejb.*; import java.util.*; import java.rmi.*; import OnlineShopping; public class OnlineShopClient { public static void main(String args[ ]) throws Exception { try{ properties p=new Properties( ); p.put(Context.PROVIDER_URL,t3://192.168.100.71:7001); p.put(Context.INTIAL_CONTEXT_FACTORY,weblogic.jndi.T3IntialContextFactory ); p.put(Context.PROVIDER_URL,t3://192.100.70:7001/console); intialContext ic=new IntialContext(p);Home os=(OnlineShopHome)ic.lookup(OnlineShopHome); OnlineShopRemote or=(OnlineShopHome)ic.lookup(OnlineShopRemote); os.getProductList(); os.Purchase(101,10); os.Purchase(102,20); } Catch(Exception e){

14

e.printStackTrace(); } } } ejb_jar.xml <!DOCTYPE ejb_jar PUBLIC _//SUN MicroSystems,Inc .//DTD Enterprise JavaBean 2.0//EN http://java.sun.com/j2ee/dtds/ejb_jar_2_0.dtd> <ejb_jar> <enterprise_beans> <session> <ejb_name>OnlineShoppingApplication</ejb_name> <home>OnlineShopping.OnlineShopHome</home> <remote>OnlineShopping.OnlineShopRemote</remote> <ejb_class>OnlineShopClass</ejb_class> </session> </enterprise_beans> weblogic_ejb_jar.xml <!DOCTYPE weblogic_ejb_jar PUBLIC _//BEA Systems,Inc.//DTD weblogic8.1.0 EJB//EN http://www.bea.com/servers/w1s810/dtd/weblogic_ejb_jar.dtd> <weblogic_ejb_jar> <weblogic_enterprise_bean> <ejb_name>OnlineShoppingApplication</ejb_name> <jndi_name>OnlineShopHome</jndi_name> </weblogic_enterprise_bean> </weblogic_ejb_jar>

15

*.Consider an Inventory application which supports three basic use cases: (i) Adding Items to the Stock (ii)Receive items (iii)Issue Items The application starts with adding item information to the stock.Then it allows enter goods receiving and issuing information.All those updated information are stored in the inbuilt database Implement the above application using EJB by considering necessary issues involved package Inventoryctrl; import javax.ejb.*; import java.rmi.remoteException ; InventoryHome.java public interface InventoryHome extends ejbHome { InventoryItem[ ] create() throws createException, RemoteException; } InventoryRemote.java public interface InventoryRemote extends ejbObject { public InventoryItem[ ] get Inventory() throws RemoteException; public void addItem(int custid,String itemcode,int quantity) throws RemoteException; public void removeItem(int custid,String itemcode,int quantity) throws RemoteException,ItemNotFoundException; public InventoryItem[ ] get BagContents(int custid) throws RemoteException; public void finalizeOrder(int custid) throws RemoteException; } InventoryClass.java package Inventoryctrl; import javax.ejb.*; public class InventoryBean implements SessionBean public InventoryItem[ ] get Inventory() { String query=select count(*) from inventory; Connection c=DriverManager.getC onnection(get DBURL()); Statement s=c.createStatement(); ResultSet rs=s.executeQuery(query); rs.next();

16

int count=rs.getInt(1); System.out.println(count is+conunt); rs.close(); public void addItem(int custid,String itemcode,int quantity) throws RemoteException; { try{ Connection c=DriverManager.getC onnection(get DBURL()); Int quantityOrdered=getItemCount(int custid,itemcode,c); If(quantityOrdered>0) { String query=update orders set quantity= (quantityOrdered+quantity)+where custid+and itemcode=+itemcode+ ; Statement s=c.createStatement(); s.executeUpdate(query); s.close: } else { String query=insert into orders(custid,itemcode,quantity) values(?,?,?); Prepared Statement ps=con. Prepared Statement(query); ps.setInt(1,custid); ps.setString(2,itemcode); ps.setInt(3,quantity) ; ps.executeUpdate() ; ps.close() ; } c.close() ; }catch(SQL Exception e){ log(SQL exception in addItem:+e); } } public int getItemCount(int custid,String itemcode,int quantity) throws SQLException; { String query=select quantity from orders where custid=+custid+and itemcode=+itemcode+ ; Statement s=c.createStatement(); ResultSet rs=s.executeQuery(query); boolean more=rs.next(); int quantity; if(!more) { quantity=0;

17

} else { Quantity=rs.getInt(1); } rs.close(); return quantity; } public void removeItem(int custid,String itemcode,int quantity) throws RemoteException,ItemNotFoundException; { try{ Connection c=DriverManager.getC onnection(get DBURL()); int numordered=getItemCount((int custid,String itemcode,int quantity) throws SQLException{ if(numorderd==0) { c.close(); throw ItemNotFoundException(); } log(numordered=+ numordered) { if(numordered<=quantity) { String query=delete from orders where custid=+custid+and itemcode=+itemcode; Statement s=c.createStatement(); int count=s. executeUpdate(query); s.close(); } else { Statement s=c.createStatement(); String query=update orders set quantity= (quantity-itemsToRemove)+where custid+and itemcode=+itemcode+ ; int count=s. executeUpdate(query); s.close(); } public void finalizeOrder(int custid) throws RemoteException; { Connection c=DriverManager.getC onnection(get DBURL()); Sring query=insert int orders (int custid,String itemcode,int quantity) select custid, itemcode, quantity from orders where custid=+custid; Statement s=c.createStatement()

18

s. executeUpdate(query); s.close(); Sring query=delete from orders where custid=+custid; Statement s=c.createStatement() s. executeUpdate(query); s.close(); } } public void ejbActivate () { System.out.println (student bean activated); } public void ejbpassivate () { System.out.println (student bean passivated); } public void setsessioncontext (sessioncontext sc) { ctx =sc; System.out.println (student bean selected); } Public void ejbCreate() throws CreateException { System.out.println (student bean created); } public void ejbRemove () { System.out.println (student bean removed); } } InventoryClient.java import. Inventoryctrl .*; import java.rmi.*; import javax.ejb.*; import java.util.*; import javax.rmi.*; import javax.naming.*; public class Inventoryclient { public static void main(String args[])

19

{ Properties p=new properties (); p.put (context INITIAL_CONTEXT_FACTORY,weblogic.jndi.WLInitialcontext factory); p.put (context PROVIDER URL,t3://localhost:1001); try { InitialContext ic=new InitialContext (p); Object obj=ic.lookup (Inventoryctrl); } catch (Exception e) { e.printstacktrace (system. out); } } } ejb_jar.xml <!DOCTYPE ejb_jar PUBLIC _//SUN MicroSystems,Inc .//DTD Enterprise JavaBean 2.0//EN http://java.sun.com/j2ee/dtds/ejb_jar_2_0.dtd> <ejb_jar> <enterprise_beans> <session> <ejb_name>InventoryApplication</ejb_name> <home>Inventoryctrl.InventoryHome</home> <remote>Inventoryctrl.InventoryRemote</remote> <ejb_class> InventoryClass</ejb_class> </session> </enterprise_beans> weblogic_ejb_jar.xml <!DOCTYPE weblogic_ejb_jar PUBLIC _//BEA Systems,Inc.//DTD weblogic8.1.0 EJB//EN http://www.bea.com/servers/w1s810/dtd/weblogic_ejb_jar.dtd> <weblogic_ejb_jar> <weblogic_enterprise_bean> <ejb_name>InventoryApplication</ejb_name> <jndi_name>InventoryHome</jndi_name> </weblogic_enterprise_bean> </weblogic_ejb_jar>

20

*.Develop a student application for processing admissions by considering various parameters.Implement the above application by using EJB // studentRemote.java package student; import java.rmi.RemoteException; import javax.ejb.EJBobject; public interface studentRemote extends EJBobject { void studetails(int no) throws RemoteException; } //studentHome.java package student; import java.rmi.RemoteException; import javax.ejb.EJBobject; public interface studentHome extends EJBHome { public student create () throws RemoteException, CreateException; } // studentBean.java package student; import java.rmi.RemoteException; import java.ejb.sessionBean; import java.ejb.sessioncontext; import java.ejb.CreateException; public class studentBean implements sessionBean { private Sessioncontext ctx; } public void ejbActivate () { System.out.println (student bean activated); } public void ejbpassivate () { System.out.println (student bean passivated); } public void setsessioncontext (sessioncontext sc)

21

{ ctx =sc; System.out.println (student bean selected); } public void ejbCreate() throws CreateException { System.out.println (student bean created); } public void ejbRemove () { System.out.println (student bean removed); } void studentails(int no) throws RemoteException { System.out.println (student details); System.out.println (Name: xxx); System.out.println (Branch: mca); System.out.println (college: Rishi); } } // studentclient.java import student.*; import java.rmi.*; import javax.ejb.*; import java.util.*; import javax.rmi.*; import javax.naming.*; public class studentclient { public static void main(String args[]) { Properties p=new properties (); p.put (context INITIAL_CONTEXT_FACTORY,weblogic.jndi.WLInitialcontext factory); p.put (context PROVIDER URL,t3://localhost:1001); try { InitialContext ic=new InitialContext (p); Object obj=ic.tookup (studentBean); Class cls=student.studentHome.class; Object o=portableRemoteObject.narrow (obj, cls);

22

Student.studentHome h= (student studentHome)o; r=h.create (); r.studetails (); r.remove (); } catch (Exception e) { e.printstacktrace (system. out); } } } ejb_jar.xml <!DOCTYPE ejb_jar PUBLIC _//SUN MicroSystems,Inc .//DTD Enterprise JavaBean 2.0//EN http://java.sun.com/j2ee/dtds/ejb_jar_2_0.dtd> <ejb_jar> <enterprise_beans> <session> <ejb_name>StudentApplication</ejb_name> <home>Student.StudentHome</home> <remote>Student.StudentRemote</remote> <ejb_class>StudentClass</ejb_class> </session> </enterprise_beans> weblogic_ejb_jar.xml <!DOCTYPE weblogic_ejb_jar PUBLIC _//BEA Systems,Inc.//DTD weblogic8.1.0 EJB//EN http://www.bea.com/servers/w1s810/dtd/weblogic_ejb_jar.dtd> <weblogic_ejb_jar> <weblogic_enterprise_bean> <ejb_name>StudentApplication</ejb_name> <jndi_name>StudentHome</jndi_name> </weblogic_enterprise_bean> </weblogic_ejb_jar>

23

*.Develop a Library application for issuing and returning books by considering Book(BookID,Title,author,edition,year,price) information.Implement the above application using EJB //a stateful session bean for Library operation Remote Interface.java package library; import javax.,ejb.*; import java.rmi.* ; import java.util.* ; public interface RemoteInterface extends EJBObject { public Vector details(int bookid) throws RemoteException } HomeInterface.java package library; import javax.,ejb.*; import java.rmi.* ; public interface HomeInterface extends EJBHome { public RemoteInterface create(int bookid) throws CreateException,RemoteException ; } LibBean.java package library; import java.sql.* ; import javax.,ejb.*; import java.rmi.* ; import java.util.* ; public class libBean implements SessionBean { private sessionContext ctx ; Vector v ; Connection con ; private int bookid ; public void setSessionContext(SessionContext ctx) { Try { Ctx=s ; Con=makeConnection() ;

24

} Catch(Exception e) { e.printStackTrace(); } } public void ejbCreate(int bookid) { This.bookid=bookid; } public void ejbActivate() public void ejbPassivate() public void ejbRemove () private Connection makeConnection() throws Exception { Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); Con=DriverManager.getConnection(Jdbc:Odbc:lib); return con; } public Vector details(int bookid) throws RemoteException { preparedStatement ps; resultSet rs; try{ ps=con.PreparedStatement(select * from library where bookid=?); ps.setInt(1,bookid); ps= ps.executeQuery(); if(rs.next()) { v.add(rs.getString(bookid)); v.add(rs.getString(bookname)); v.add(rs.getString(authorname)); v.add(rs.getString(duedate)); } } catch(Exception e) { } return v; }

25

} Client.java import library.* ; import javax.naming.* ; import java.rmi.* ; import java.util.* ; public class Client{ public static void main(String args[]) { try{ library.HomeInterface hm; library.RemoteInterface rm; Properties p=new Properties(); p.put(context.PROVIDE_URL,t3://localhost:7001); p.put(context.INTIAL_CONTEXT_FACTORY,weblogic.jndi.T3IntialContextFactory) ; Context con=new IntialContext(p); Hm=(library.HomeInterface)con.lookup(library.HomeInterface); Re=(library.RemoteInterface)hm.create(36); Vector v=re.details(36); for(int i=0;i<v.size();i++) System.out.println(v.get(i)); } Catch(Exception e) { e.printStackTrace(); } } } ejb-jar.xml <!DOCTYPE ejb_jar PUBLIC _//SUN MicroSystems,Inc .//DTD Enterprise JavaBean 2.0//EN http://java.sun.com/j2ee/dtds/ejb_jar_2_0.dtd> <ejb_jar> <enterprise_beans> <session> <ejb_name>LibraryApplication</ejb_name> <home>Library.HomeInterface</home> <remote>Library.RemoteInterface</remote> <ejb_class>libbean </ejb_class> </session> </enterprise_beans>

26

wegblogic_jar.xml <?xml version=1.0 encoding=UTF_8?> <!DOCTYPE weblogic_ejb_jar PUBLIC _//BEA Systems,Inc.//DTD weblogic 8.1.0 EJB//ENhttp://www.bea.com/services/wis810/dtd/weblogic_ejb_jar.dtd> <weblogic_ejb_jar> <weblogic_enterprise_bean> <ejb_name>libBean</ejb_name> <jndi_name>library.HomeInterface</jndi_name> </weblogic_enterprise_bean> </weblogic_ejb_jar>

27

*.Develop a statefull session bean for online shopping system Online shopRemote.java package OnlineShopping; import javax.ejb.*; import java.rmi.*; public interface OnlineShopRemote extends EJBObject { Product[ ] getProductList () throws RemoteException; String Purchase(int itemid, int quantity) throws RemoteException; } OnlineShopHome.java package OnlineShopping; import java.ejb.*; import java.rmi.*; public interface OnlineShopHome extends EJBHome { public void create() throws CreateException,RemoteException; } OnlineShopClass.java package OnlineShopping; import javax.ejb.*; import java.uti.*; import java.rmi.*; public class OnlineShopClass implements SessionBean { SessionContext s=NULL; public void ejbC reate() { } public void ejbActivate() { } public void ejbPassivate() { } public void ejbRemove() { } public void setSessionContext(SessionContext ctx) {

28

s=ctx; } public Product[] getProductList () throws RemoteException { try { Connection con=DriverManager.getConnection(get DBURL()); String query=select * from ItemList; Statement s=con.createStatement(); resultSet rs=s.executeQuery(query); rs.next(); if(!more) { rs.getString(itemname); rs.getInt (iprice); rs.getInt(itemid); rs.getInt(quantity); } rs.close(); return itemid,itemname,quantity,iprice; } public String Purchase(int itemid,int quantity) throws RemoteException { int Price=0; int n; System.out.println(enter the no of items u want to buy); System.in(n); System.out.println(enter the itemid which u want to buy); System.in(itemid); System.out.println(enter quantity); System.in(quantity); if(n>1) { for(i=1;i=n;i++) { Price=Price+iprice; } } //issue items System.out.println(enter the name of the bank in which u have credit card); System.in(bankname); System.out.println(enter ur creditcard no);

29

System.in(creditcardno); Statement s=con.createStatement(select creditbal from BankDB where bankname=+bankname+creditcardno=+creditcardno); if(i==creditcardno&&creditbal>=Price ) { System.out.println(ur a/c has been verified); System.out.println(items with ids+itemid +has been allotted to u with a charge of +chargeamt); System.in(chargeamt); System.out.println(transaction completed); } OnlineShop Client.java import java.naming.*; import java.ejb.*; import java.util.*; import java.rmi.*; import OnlineShopping; public class OnlineShopClient { public static void main(String args[ ]) throws Exception { try{ properties p=new Properties( ); p.put(Context.PROVIDER_URL,t3://192.168.100.71:7001); p.put(Context.INTIAL_CONTEXT_FACTORY,weblogic.jndi.T3IntialContextFactory ); p.put(Context.PROVIDER_URL,t3://192.100.70:7001/console); intialContext ic=new IntialContext(p);Home os=(OnlineShopHome)ic.lookup(OnlineShopHome); OnlineShopRemote or=(OnlineShopHome)ic.lookup(OnlineShopRemote); os.getProductList(); os.Purchase(101,10); os.Purchase(102,20); } Catch(Exception e) { e.printStackTrace(); } }

30

ejb_jar.xml <!DOCTYPE ejb_jar PUBLIC _//SUN MicroSystems,Inc .//DTD Enterprise JavaBean 2.0//EN http://java.sun.com/j2ee/dtds/ejb_jar_2_0.dtd> <ejb_jar> <enterprise_beans> <session> <ejb_name>OnlineShoppingApplication</ejb_name> <home>OnlineShopping.OnlineShopHome</home> <remote>OnlineShopping.OnlineShopRemote</remote> <ejb_class>OnlineShopClass</ejb_class> </session> </enterprise_beans> weblogic_ejb_jar.xml <!DOCTYPE weblogic_ejb_jar PUBLIC _//BEA Systems,Inc.//DTD weblogic8.1.0 EJB//EN http://www.bea.com/servers/w1s810/dtd/weblogic_ejb_jar.dtd> <weblogic_ejb_jar> <weblogic_enterprise_bean> <ejb_name>OnlineShoppingApplication</ejb_name> <jndi_name>OnlineShopHome</jndi_name> </weblogic_enterprise_bean> </weblogic_ejb_jar>

31

*.Deveolp a railway application having remote method to find status of reservation which when supplied by PNR number.Implement the above using EJB RailwayRemote.java package RailwayResrv; import javax.ejb.*; import java.rmi.*; public interface RailwayRemote extends EJBObject { String getAvailList () throws RemoteException; String Reservation(String trainname,int wanted) throws RemoteException; } RailwayHome.java package RailwayResrv; import java.ejb.*; import java.rmi.*; public interface RailwayHome extends EJBHome { public void create() throws CreateException,RemoteException; } RailwayClass.java package RailwayResrv; import javax.ejb.*; import java.uti.*; import java.rmi.*; public class RailwayClass implements SessionBean { SessionContext s=NULL; public void ejbC reate() { } public void ejbActivate() { } public void ejbPassivate() { } public void ejbRemove() { }

32

public void setSessionContext(SessionContext ctx) { s=ctx; } public String getAvailList () throws RemoteException { try { Connection con=DriverManager.getConnection(get DBURL()); String query=select trainnames,timings,chargeamt,availableseats from TrainList where source=+source+destiny=+destiny; Statement s=con.createStatement(); ResultSet rs=s.executeQuery(query); rs.next(); if(!more) { rs.getString(trainnames); rs.getString(timings); rs.getInt(chargeamt); rs.getInt(availableseats); } rs.close(); return trainnames,timings,chargeamt,availableseats; } public String Reservation(String trainname,int wanted) throws RemoteException; { System.out.println(enter the trainname in which u want to travel); System.in(trainname); System.out.println(enter no of seats u want); System.in(wanted); if(availabeseats>=wanted) { System.out.println(seats available); //issue ticket System.out.println(enter the name of the bank in which u have credit card); System.in(bankname); System.out.println(enter ur creditcard no); System.in(creditcardno); for(i=1;1<=100;i++) {

33

Statement s=con.createStatement(select creditbal from BankDB where bankname=+bankname+creditcardno=+creditcardno); if(i==creditcardno&&creditbal>=chargeamt ) { System.out.println(ur a/c has been verified); System.out.println(seat no+seat no+has been allotted to u with a charge of +chargeamt); System.in(chargeamt); System.out.println(ur seat has been confirmed); availableseats=availableseats-wanted; else System.out.println(seat may not be confirmed); } Railway Client.java import java.naming.*; import java.ejb.*; import java.util.*; import java.rmi.*; import dt_time.*; public class RailwayClient { public static void main(String args[ ]) throws Exception { try{ properties p=new Properties( ); p.put(Context.PROVIDER_URL,t3://192.168.100.71:7001); p.put(Context.INTIAL_CONTEXT_FACTORY,weblogic.jndi.T3IntialContextFactory ); p.put(Context.PROVIDER_URL,t3://192.100.70:7001/console); intialContext ic=new IntialContext(p); RalwayHome rh=(RailwayHome)ic.lookup(RailwayHome); RalwayRemote rr=(RailwayHome)ic.lookup(RailwayRemote); rh.getAvailList(); rh.Reservation(VAADI,4); rh.Reservation(GOLCONDA,2); } Catch(Exception e) { e.printStackTrace(); } } }

34

ejb_jar.xml <!DOCTYPE ejb_jar PUBLIC _//SUN MicroSystems,Inc .//DTD Enterprise JavaBean 2.0//EN http://java.sun.com/j2ee/dtds/ejb_jar_2_0.dtd> <ejb_jar> <enterprise_beans> <session> <ejb_name>RailwayApplication</ejb_name> <home>RailwayResrv.RailwayHome</home> <remote>RailwayResrv.RailwayRemote</remote> <ejb_class>RailwayClass</ejb_class> </session> </enterprise_beans> weblogic_ejb_jar.xml <!DOCTYPE weblogic_ejb_jar PUBLIC _//BEA Systems,Inc.//DTD weblogic8.1.0 EJB//EN http://www.bea.com/servers/w1s810/dtd/weblogic_ejb_jar.dtd> <weblogic_ejb_jar> <weblogic_enterprise_bean> <ejb_name>RailwayClassn</ejb_name> <jndi_name>RailwayHome</jndi_name> </weblogic_enterprise_bean> </weblogic_ejb_jar>

35

*.Write a stateless session bean that provides current date and time Date-Time Remote.java package dt_time; import javax.ejb.*; import java.rmi.*; public interface MyRemote extends EJBObject { String getDate() throws RemoteException; String getTime() throws RemoteException; } Date-TimeHome.java package dt_time; import java.ejb.*; import java.rmi.*; public interface MyHome extends EJBHome { MyRemote create() throws CreateException,RemoteException; } DateTime.java package dt_time; import javax.ejb.*; import java.uti.*; import java.rmi.*; public class Date_Time implements SessionBean { Calendar cal=Calendar.getInstance(); SessionContext s=NULL; public void ejbC reate() { } public void ejbActivate() { } public void ejbPassivate() { } public void ejbRemove() { } public void setSessionContext(SessionContext ctx) {

36

s=ctx; } public Sting getDate() throws RemoteException { String str=String.valueOf(cal.get(Calendar.DATE)); String str+=String.valueOf(cal.get(Calendar.MONTH)); String str+=String.valueOf(cal.get(Calendar.YEAR)); String str=String.valueOf(cal.get(Calendar.SECOND)); } } Date Time Client.java import java.naming.*; import java.ejb.*; import java.util.*; import java.rmi.*; import dt_time.*; public class DateTimeClient { public static void main(String args[ ]) throws Exception { try{ Properties p=new Properties( ); p.put(Context.PROVIDER_URL,t3://192.168.100.71:7001); p.put(Context.INTIAL_CONTEXT_FACTORY,weblogic.jndi.T3IntialContextFactory ); IntialContext ic=new IntialContext(p); Dt_time.Date-TimeHome home=(dt_time.MyHome)ic.lookup(lateef_jndi); Dt_time.Date-TimeRemote remote=(dt_time.MyRemote)home.create(); String s=remote.getDate(); System.out.println(current date is=+s); String s=remote.getTime(); System.out.println(current time is=+s); } Catch(Exception e) { e.printStackTrace(); } } }

37

ejb-jar.xml <!DOCTYPE ejb_jar PUBLIC _//SUN MicroSystems,Inc .//DTD Enterprise JavaBean 2.0//EN http://java.sun.com/j2ee/dtds/ejb_jar_2_0.dtd> <ejb_jar> <enterprise_beans> <session> <ejb_name>Date and Time Application</ejb_name> <home>dt_Time.Date_TimeHome</home> <remote>dt_Time.Date_TimeRemote</remote> <ejb_class>Date TimeClass</ejb_class> </session> </enterprise_beans> weblogic_ejb_jar.xml <!DOCTYPE weblogic_ejb_jar PUBLIC _//BEA Systems,Inc.//DTD weblogic8.1.0 EJB//EN http://www.bea.com/servers/w1s810/dtd/weblogic_ejb_jar.dtd> <weblogic_ejb_jar> <weblogic_enterprise_bean> <ejb_name>Date and Time Application</ejb_name> <jndi_name>Date_Time Home</jndi_name> </weblogic_enterprise_bean> </weblogic_ejb_jar>

38

CORBA
*.Consider an Employee database and develop a Payroll application by considering BasicPay,DA,HRA,CCA,Deductions which generates pay slip using CORBA //Employee.IDL module Employee { interface Employee { void printDetails(lang in); }; }; //Implementation Class import Employee.*; import org.omg.CORBA.*; import java.util.properties; public class EmployeeImp extends EmployeeImpBase { public void printDetails(lang num) { DriverManager.registerDriver(new oracle.jdbc driver.oracleDriver()); Connection con=DriverManager.getConnection("jdbc:oracle@localhost:2222", "scott","tiger"); statement stmt=con.createstatement(); ResultSet rs=stmt.executeQuery("Select from emp where eno=num"); while(rs.next()) { System.out.println(rs.getInt(1)); System.out.println(rs.getString(2)); System.out.println(rs.getFloat(3)); } con.close(); } //EmployeeServer.java import employee.*; import org.omg.CosNaming.*; import org.omg.NamingContentPackage.*; import CORBA.*; import java.util.properties; 39

public class EmployeeServer { public static void main(String args[]) { try { ORB orb=ORB.init(args,""); EmployeeImp ei=new EmployeeImp(); org.omg.CORBA.object object=orb.resolve_initial_references("Name service"); NamingContent ncref=NamingContentHelper.narrow(objRef); Employee href=EmployeeHelper.narrow(EmployeeImp); NameComponent nc=new NameComponent("Employee",""); ncref.rebind(path href); System.out.println("Server is waiting..."); orb.run(); } catch(Exeception e) { System.out.println("Error"); e.printStackTrace(System.out); } } } //EmployeeClient.java import Employee.*; import org.omg.CosNaming.NamingContentPackage; import org.omg.CosNaming.*; import org.omg.CORBA.*; public class EmployeeClient { static Employee EmpImpl; public void static main(String args[]) { try { ORB orb=ORB.init(args,null); org.omg.CORBA.object object=orb.resolve_initial_references("NamingService"); NamingContent ncref=NamingContentHelper.narrow(objRef); NameComponent nc=new NameComponent("Hello",""); NameComponent path[]={nc}; Employee EmpImpl=EmployeeHelper.narrow(ncref); System.out.println(EmpImpl.printDetails()); }

40

catch(Exeception e) { System.out.println("error"+e); } } }

41

*.Develop a bank application to find the balance amount from the saving account by considering necessary issues.Implement the above application by using CORBA IDL // bank.idl Module bank { Interface Bank { Float displaBalance (float Val) }; } // BankImpl.java import bank .*; import org.omg.CORBA.*; import java.sql.*; Public class BankImpl extends BankImplBase { Public float display Balance (float Val) { try { Class.forName (sun.jdbc.odbc.JdbcOdbcdriver); Connection con=DriverManager.getConnection (Jdbc: Odbc: MyDb,Scott,and tige@orastub); Statement stmt=con.CreateStatement (); Resultset rs=stmt.executeQuery (select balance from bank where no= val); if (rs.next ()) { if (rs.getFloat()) return (rs.getFloat(1)); } rs.close(); stmt.close(); con.close (); } catch (Exception e) { System.out.println (error); } return (0); }

42

//BankServer.java import bank.*; import org.omg.cosNaming.*; import org.omg.cosNaming.NamingContext.package.*; import org.omg.CORBA.*; Public class Bankserver { Public static void main (String args []) { try { ORB orb=ORB.init (args,null); BankImpl bi=new BankImpl (); org.omg.CORBA.object objref=orb.resolve.initial.references(Name service); Naming Context ncref= NamingContextHelper.narrow(objref); Bank href=BankHelper.narrow(bi); NameComponent nc=new NameComponent(Bank, ); NameComponent path[]={nc}; ncref.Rebind(path,href); System.out.println(server is ready&waiting); Orb.run(); } catch(Exception e) { e.print-stackTrace(System.out); } System.out.println(server is existing); } }

// BankClient.java import bank.*; import org.omg.cosNaming.*; import org.omg.cosNaming.NamingContext.package.*; import org.omg.CORBA.*; public class Bankclient { Public static void main(String args[]) { try {

43

ORB orb=ORB.init(args,null); BankImpl bi=new BankImpl(); org.omg.CORBA.object objref=orb.resolve.initial.references(Name service); NamingContext ncref= NamingContextHelper.narrow(objref); Bank href=BankHelper.narrow(bi); NameComponent nc=new NameComponent(Bank, ); NameComponent path[]={nc}; Bank bankImpl=BankHelper.narrow(ncref,resolve Cpath); System.out.println(Balance +bankImpl.display Balance(args[0])); } catch(Exception e) { e.print-stackTrace(System.out); } } }

44

COM

45

*.Implement a COM component to perform the fallowing operations on bank account: (i) Deposit (ii)Withdraw /* VB */ Dim bal As Long Dim val(6) As Long Dim i,x As Integer public Function deposit(ByVal acno As Long,ByVal amt As Long) As Long i=0 For i=0 To 5 If(i=acno) Then val(i)=val(i)+amt End If Next Deposit=Val(acno) End Function Public Function withdraw(ByVal acno As Long,ByVal amt As Long) As Long bal=0 For i=0 To 5 If(i=acno) Then val(i)=val(i)-amt End If Next withdraw=val(acno) End Function Public Function showBal(ByVal acno As Long) As Long bal=0 For i=0 To 5 If(i=acno) Then bal=val(i) End If Next Showbal=bal End Function Public Function initialize() x=500 For i=0 To 5 Val(i)=1000+x Next End Function /* C# */ Using system;

46

Using bankproj; Namespace CConsoleApplication8 { ///<summary> //.description for class1 ///</summary> Class class1 { ///<summary> //.entry point for application ///</summary> [STA Thread] Static void Main(string[] args) { // bank b=new bank(); b.intialize(); Console.Writeline(intial bal+b.showBal(1)); Console.writre(After depositing 1500\t); Console.Writeline(b.deposit(1,1500)); Console.write(After withdrawing 1000\t); Console.Writeline(b.withdraw(1,1000)); Console.write(current balance \t); Console.Writeline(b.showBal(1)); Console.ReadLine(); //TODO :Add code to start application here // } } }

47

.NET
*.Develop a component for converting currency from rupees to dollar and dollar to rupees using .NET //Windows Application for currenc conversion Using system; Using system.Drawing; Using system.Collection; Using system.ComponentModel; Using system.Windows.Forms; Using system.Data; private void radioButton1_CheckedChanged(object sender,System.EventArgs e) { double k=convert.ToDouble(textBox1.Text); k=(double) k*0.5; string v= +k; textBox2.Text=v; } private void radioButton4_CheckedChanged(object sender,System.EventArgs e) { double k=convert.ToDouble(textBox1.Text); k=(double) k*0.125; string v= +k; textBox2.Text=v; } private void radioButton2_CheckedChanged(object sender,System.EventArgs e) { double k=convert.ToDouble(textBox1.Text); k=(double) k*2; string v= +k; textBox2.Text=v; } private void radioButton3_CheckedChanged(object sender,System.EventArgs e) { double k=convert.ToDouble(textBox1.Text); k=(double) k*0.5; string v= +k; textBox2.Text=v; } } }

48

*.Develop a component for browsing CD catalogue using COM/.NET Import System.IO Public Class Form1 Inherits Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim str() As String Dim i As Integer Str = Directory.GetLogicalDrives() For i=0 To str.Length-1 LostBpx1.Items.Add(str(i)) Next End Sub Private Sub ListBox1_SelectedIndexChanged (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChange Dim d As New DirectoryInfo(ListBox1.SelectedItem) Dim dinfo() As DirectoryInfo Dim i As Integer dinfo=d.GetDirectories ListBox2.Items.Clear() For i=0 To dinfo.Length-1 ListBox2.Items.Add(dinfo(i).Name) Next End Sub Private Sub ListBox2_SelectedIndexChanged (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChange Dim d As New DirectoryInfo(ListBox1.SelectedItem&ListBox1.SelectedItem) Dim dinfo() As FileInfo Dim i As Integer ListBox3.Items.Clear() dinfo=d.GetDirectories For i=0 To dinfo.Length-1 ListBox3.Items.Add(dinfo(i).Name) Next End Sub End class

49

*.Develop a calculator component for arthimetic operation using .NET public partial class Form1 : Form { // Load variables private int sum; // Uses Double for large numbers: um = total private string TextEntry = null; // String for reading text box entry private char op; // op = operation public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) {

50

} private void button1_Click(object sender, EventArgs e) // Name of button { textBox1.AppendText("1"); // Show digit in text box TextEntry = textBox1.Text; // Prep TextEntry with textBox1 string } private void button2_Click(object sender, EventArgs e) { textBox1.AppendText("2"); TextEntry = textBox1.Text; } private void button3_Click(object sender, EventArgs e) { textBox1.AppendText("3"); TextEntry = textBox1.Text; } private void button4_Click(object sender, EventArgs e) { textBox1.AppendText("4"); TextEntry = textBox1.Text; } private void button5_Click(object sender, EventArgs e) {

51

textBox1.AppendText("5"); TextEntry = textBox1.Text; } private void button6_Click(object sender, EventArgs e) { textBox1.AppendText("6"); TextEntry = textBox1.Text; } private void button7_Click(object sender, EventArgs e) { textBox1.AppendText("7"); TextEntry = textBox1.Text; } private void button8_Click(object sender, EventArgs e) { textBox1.AppendText("8"); TextEntry = textBox1.Text; } private void button9_Click(object sender, EventArgs e) { textBox1.AppendText("9"); TextEntry = textBox1.Text; }

52

private void button14_Click(object sender, EventArgs e) { textBox1.AppendText("0"); TextEntry = textBox1.Text; } private void button10_Click(object sender, EventArgs e) { // Input value from the user sum = int.Parse(TextEntry); op = '+'; // addition for switch in total textBox1.Clear(); // to set for next number } private void button11_Click(object sender, EventArgs e) { // Input value from the user sum += int.Parse(TextEntry); op = '-'; // minus textBox1.Clear(); } private void button12_Click(object sender, EventArgs e) { // Input value from the user sum = int.Parse(TextEntry);

53

op = '*'; // multipalcation for switch in total textBox1.Clear(); } private void button13_Click(object sender, EventArgs e) { // Input value from the user sum = int.Parse(TextEntry); op = '/'; // division textBox1.Clear(); } private void button15_Click(object sender, EventArgs e) { textBox1.Clear(); switch (op) { case '+': sum = int.Parse(TextEntry); // sum + last digit entry textBox1.Text = sum.ToString(); // display sum break; case '-': sum -= int.Parse(TextEntry); textBox1.Text = sum.ToString(); break;

54

case '*': sum *= int.Parse(TextEntry); textBox1.Text = sum.ToString(); break; case '/': sum /= int.Parse(TextEntry); textBox1.Text = sum.ToString(); break; } } private void button16_Click(object sender, EventArgs e) { textBox1.Clear(); sum = 0; } }

*.Write a program for currency conversion Using System; Using System.Drawing; Using System.Collection; Using System.ComponentModel; Using System.Windows.Forms; Using System.Data; private void radioButton1_CheckedChanged(Object sender,System.EventArgs e) {

55

//conversion from foreign heat to centigrade double k=Convert.ToDouble(textBox1.Text); k=(double) 5/9*(k-32); String v= +k; textBox2.Text=v; } private void radioButton4_CheckedChanged(Object sender,System.EventArgs e) { //conversion from centigrade to foreign heat double k=Convert.ToDouble(textBox1.Text); k=(double) (k*9)/5+32; String v= +k; textBox2.Text=v; }

Active-X Controls using VB


19.VC++ Program To Develop An Active-X Control: 1. Create a new project and Workspace with the type set to MFC ACTIVE-X CONTROL WIZARD as shown below:

56

2.Selection of number of Controls:

57

3.Select the control options:-

58

4. Select Tools menu from menu bar and select ACTIVE-X CONTROL TEST CONTAINER option. 5. Select Edit menu from menu bar and Select Insert new control option from Edit. 6.Select the projectname control from the Dialog box.

59

60

20.VC++ Program To Collect Student Information And Store In Database Step1: Register an ODBC DataBase

Step-2 Create a new project & workspace set to MFC Appwizard(exe) as shown below

61

Step-3: Select Database view without file support optioin and select data source. This wil display the dialogue shown here:

62

Step-4: Once Snapshot has been choosen click ok button and specify the table. Again click ok. Step-5: Last we will get dialogue displaying the class and filenames.Click ok. Step-6: Now Insert the appropriate controls in the work space and run the project. The output will look like:

63

64

You might also like