JAVA CODE FOR RMI BOOKSHOP DETAIL | URDINESH

Software Programming, Tutorials, Interview Preparations,Stock Market,BSE/NSE, General informations

Tuesday, May 20, 2014

JAVA CODE FOR RMI BOOKSHOP DETAIL

CREATE A  TABLE IN DB AS FOLLOWS:
================================
id,author,Bname,Price,stock => Columns

Then Please insert some sample values in this table.Then Use the following code
PROGRAM: (SERVER)

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.sql.*;
import java.io.*;
class buff implements Serializable
{
            public String a,bn;
public int price,id;
            public int  stock;
}
interface BDetail extends Remote
{
            public buff get(int id) throws RemoteException;
public void purch(int id) throws RemoteException;
}
public class Detail extends UnicastRemoteObject implements BDetail
{
            Connection con;
Statement st;
ResultSet rs;
public Detail() throws RemoteException
            {
 try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:reservation");
st = con.createStatement();
}
catch(Exception e)
{
                        }
            }
public buff get(int id) throws RemoteException
            {
    buff o=new buff();
    int i=0;
                            try
                           {      
                                    rs=st.executeQuery("Select * from detail where id="+id);
                                    if(rs.next())
                                    {
                                                o.id=rs.getInt(1);
                                                o.a=rs.getString(2);
                                                o.bn=rs.getString(3);
                        o.price=rs.getInt(4);
                                                o.stock=rs.getInt(5);
                                    }
            else
                                    {
                        o.a="-----";
                                                o.bn="----";
           
                                    }
                              }
                              catch(Exception e)
                             {
                                    System.out.println(e);
                              }
      return o;    
              }
public void purch(int id) throws RemoteException
{
 try
{
                        rs=st.executeQuery("Select stock from detail where id="+id);
                        rs.next();
int stk=rs.getInt("stock");
if(stk==0)
{
            throw new Exception("No book in stock ID::"+id);
}
else
{
                                    stk--;
st.executeUpdate("update detail"+" "+"set "+"stock="+stk+" WHERE id="+id);
}
               }
               catch(Exception e)
               {
            System.out.println(e);
   }
        }
        public static void main(String[] x)throws Exception
       {
            Detail o=new Detail();
            Naming.rebind("book",o);
        }
  }

PROGRAM: (CLIENT)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.rmi.*;
import java.rmi.registry.*;

interface BDetail extends Remote
  {
     public buff get(int id) throws RemoteException;
     public void purch(int id) throws RemoteException;
}

class BookShop extends JFrame implements ActionListener
 {
   JLabel ttl;
   JButton b1,b2,b3;
   JTextField t1,tt1,tt2,tt3,tt4,tt5;
   JLabel l1,id,auth,title,stock,price;
   JPanel p,p1,p2,p3;
   Container c;
   BDetail m1;
   BookShop()
   {
           super("BOOK SHOP");
           try
          {
   m1=(BDetail)Naming.lookup("rmi://Localhost/book");
   c=this.getContentPane();
   p=new JPanel();
   p1=new JPanel();
   p2=new JPanel();
   p3=new JPanel();
   p2.setLayout(new GridLayout(6,5));

   ttl=new JLabel("BOOK SHOP");
   ttl.setFont(new Font("Ariel", Font.BOLD, 20));

   p.add(ttl);
   add(p,"North");

   l1=new JLabel("ID::");
   t1=new JTextField(3);
   b1=new JButton("Submit");
   b2=new JButton("Clear");
   p1.add(l1);
   p1.add(t1);
   p1.add(b1);
   p1.add(b2);
  c.add(p1,"East");
  c.add(new JPanel(),"West");
  id=new JLabel("ID");
  title=new JLabel("TITlE");
  stock=new JLabel("STOCK");
  auth=new JLabel("AUTHOR");
  price=new JLabel("PRICE");
  tt1=new JTextField(3);
  tt1.setEditable(false);

  tt2=new JTextField(21);
 tt2.setEditable(false);

 tt3=new JTextField(21);
 tt3.setEditable(false);

 tt4=new JTextField(21);
tt4.setEditable(false);

tt5=new JTextField(21);
tt5.setEditable(false);

b3=new JButton("Purchase");

p2.add(id);
p2.add(tt1);

p2.add(auth);
p2.add(tt2);

p2.add(title);
p2.add(tt3);

p2.add(stock);
p2.add(tt4);

p2.add(price);
p2.add(tt5);

c.add(p2,"Center");

p3.add(b3);
c.add(p3,"South");

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);


addWindowListener(new WindowAdapter( )
 {
        public void windowClosing(WindowEvent e)
        {
                        System.exit(0);
         }
  }                         
  }
catch(Exception ff){ }
  }
public void actionPerformed(ActionEvent e)
{
  try
   {
if(e.getActionCommand()=="Submit")
{
    buff o=m1.get(Integer.parseInt(t1.getText()));
    tt1.setText(""+o.id);
    tt2.setText(o.a);
    tt3.setText(o.bn);
    tt4.setText(""+o.stock);
    tt5.setText(""+o.price);

}

if(e.getActionCommand()=="Clear")
{
   t1.setText("");
}
  if(e.getActionCommand()=="Purchase")
{
   int n=Integer.parseInt(t1.getText());
   m1.purch(n);

   buff o=m1.get(n);
 
   tt1.setText(""+o.id);
   tt2.setText(o.a);
   tt3.setText(o.bn);
   tt4.setText(""+o.stock);
   tt5.setText(""+o.price);
 }
    }
 catch(Exception d)
{
   System.out.println(d);
  }
     }
public static void main(String[] a)
{
  BookShop o=new BookShop();
  o.setVisible(true);
  o.setSize(900,100);
  o.pack();
   }

}

No comments:

Post a Comment

Thanks for your valuable comments

Followers