URDINESH: JAVA CODE
Showing posts with label JAVA CODE. Show all posts
Showing posts with label JAVA CODE. Show all posts

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

}

JAVA CODE FOR FILTER STREAM-STUDENT MARKLIST



PROGRAM:

import java.io.*;
class Studentmarklist1
{
            public static void main(String a[]) throws IOException
            {
                        DataInputStream d=new DataInputStream(System.in);
System.out.println("\nEnter the Name,Rollno and Mark1,Mark2,Mark3,Mark4,Mark5::");
                        String name=d.readLine();
                        int no=Integer.parseInt(d.readLine());
                        int m1=Integer.parseInt(d.readLine());
                        int m2=Integer.parseInt(d.readLine());
                        int m3=Integer.parseInt(d.readLine());
                        int m4=Integer.parseInt(d.readLine());
                        int m5=Integer.parseInt(d.readLine());
                        int tot=(m1+m2+m3+m4+m5);
                        float  avg=tot/5;
                        System.out.println("\nName:="+name);
                        System.out.println("Rollno:="+no);
                        System.out.println("Mark1:="+m1);
                        System.out.println("Mark2:="+m2);
                        System.out.println("Mark3:="+m3);
                        System.out.println("Mark4:="+m4);
                        System.out.println("Mark5:="+m5);
                        System.out.println("Total:="+tot);
                        System.out.println("Average:="+avg);
if((m1&m2&m3&m4&m5)>40)
                        {
                                    System.out.println("Result=Pass");
                        }
                        else
                        {
                                    System.out.println("Result=Fail");
                        }
                        if(avg>=90.0)
                        {
                                    System.out.println("Grade=Outstanding!");
                        }
                        if(avg>=70.5&avg<=90.0)
                        {
          



System.out.println("Grade=First Class");
                        }
                        if(avg>=60.0&avg<=70.5)
                        {
            System.out.println("Grade=Second Class");
                        }
                        if(avg>50.0&avg<60.0)
                        {
            System.out.println("Grade=Third Class");
                        }
                        if(avg<50.0)
                        {
           System.out.println("Grade=No Class");
                        }
            FileOutputStream fos=new FileOutputStream("Stud.txt");
DataOutputStream dos=new DataOutputStream(fos);
dos.writeUTF(name);
dos.writeInt(no);
dos.writeInt(m1);
dos.writeInt(m2);
dos.writeInt(m3);
dos.writeInt(m4);
dos.writeInt(m5);
dos.writeInt(tot);
dos.writeFloat(avg);
            }
 }

OUTPUT:

Enter the Name,Rollno and Mark1,Mark2,Mark3,Mark4,Mark5::
MARTIN
101
98
92
91
90
87

Name:=MARTIN
Rollno:=101

Mark1:=98
Mark2:=92
Mark3:=91
Mark4:=90
Mark5:=87
Total:=458
Average:=91.6
Result=Pass

Grade=Outstanding!

JAVA CODE FOR STREAM CUSTOMIZATION-COUNT READER



PROGRAM:

import java.io.*;
public class Streamcustomization extends Reader
{
            Reader in=null;
int  read=0,charCount=0,wordCount=0,lineCount=0;
            boolean whiteSpace=true;
            public void CountReader(Reader r)
            {
                        in=r;
            }
            public int read(char[]array,int off,int len)throws IOException
            {
                        if(array==null)
                        throw new IOException("null array");
                        read=in.read(array,off,len);
                        charCount+=read;
                        char c;
                        for(int i=0;i<read;i++)
                        {
                                    c=array[i];
                                    if(c=='\n')
                                    lineCount++;
                                    if(Character.isWhitespace(c))
                                                whiteSpace=true;
                                    else if(Character.isLetterOrDigit(c) && whiteSpace)
                                    {
                        wordCount++;
                        whiteSpace=false;
                                    }
                        }
                        return read;
            }
            public void close() throws IOException
            {
                        in.close();
            }
            public int getCharCount()
            {
                        return charCount;
            }
           

public int getWordCount()
            {
return wordCount;
            }
            public int getLineCount()
            {
                        return lineCount;
            }
            public static void main(String a[]) throws Exception
            {
                        CountReader cr=new CountReader(new FileReader("lab.java"));
                        char c[]=new char[4096];
                        int read=0;
                        while((read=cr.read(c,0,c.length))!=-1)
                        System.out.println(new String(c,0,read));
                        cr.close();
System.out.println("\n\n Read chars:"+cr.getCharCount()+"\n words:"+cr.getWordCount()+"\n lines:"+cr.getLineCount());
            }
 }



OUTPUT:

class lab
{
public static void main(String args[])
{
System.out.println("welcome to MCA lab");
}
}


 Read chars:18
 words:4

 lines:1

JAVA CODE FOR TCP CHAT APPLICATION


PROGRAM: (SERVER)
import java.io.*;
import java.net.*;
class ChatS
{
public static void main(String args[])throws IOException
{
ServerSocket s=new ServerSocket(3300);
Socket s1=s.accept();
for(;;)
{
DataInputStream d1=new DataInputStream(s1.getInputStream());
String s2=d1.readUTF();
System.out.println("client:"+s2);
DataInputStream b=new DataInputStream(System.in);
String s3=b.readLine();
DataOutputStream d3=new DataOutputStream(s1.getOutputStream());
System.out.print(“server: “);
d3.writeUTF(s3);
}  
}  
}
PROGRAM: (CLIENT)
import java.io.*;
import java.net.*;
class ChatC
{
public static void main(String args[])throws IOException
{
Socket s1=new Socket("127.0.0.1",3300);
for(;;)
{
DataInputStream dis=new DataInputStream(System.in);
String str=dis.readLine();
DataOutputStream ds=new DataOutputStream(s1.getOutputStream());
System.out.print(“client: “);
ds.writeUTF(str);
DataInputStream d1=new DataInputStream(s1.getInputStream());
String Str1=d1.readLine();
System.out.println("server:"+Str1);    
}   
}  
}






OUTPUT: (CLIENT)

client: Hai
From server:How are you
client: fine


OUTPUT: (SERVER)


From client:Hai
server: How are you

From client: fine

JAVA CODE FOR THREE-TIER APPLICATION: ADDRESS SEARCH

CREATE A  TABLE IN DB AS FOLLOWS:
================================
PID => NUMBER
NAME =>  TEXT
ADDRESS =>  TEXT
CITY  => TEXT
PHONE NO =>  NUMBER

Then Please insert some sample values in this table.Then Use the following code


FRONT PAGE PROGRAM USING HTML:

<html >
<head>
<title>WELCOME TO ADDRESS SEARCH</title>
</head>
<body bgcolor="cyan">
<h1> Welcome to address search</h1>
<form  name="form1" method="get" action="http://localhost:8080/servlet/address">
<input type="text" name="teln" />
<input type="submit"  value="Submit" />
</form>  
</body>
</html>                      



PROGRAM:

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Example extends HttpServlet
  {
  public void doGet(HttpServletRequest req,HttpServletResponse res)throws     ServletException,IOException
{
             res.setContentType("text/html");
            PrintWriter out=res.getWriter();
String n=req.getParameter("teln");
int nn=Integer.parseInt(n);
try
            {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection c=DriverManager.getConnection("jdbc:odbc:xxx");
    Statement s=c.createStatement();
    String  str="select * from address where telno="+nn;
    ResultSet r=s.executeQuery(str);
   while(r.next())
{
int id1=r.getInt(1);
String name1=r.getString(2);
String add1=r.getString(3);
String city1=r.getString(4);
int no1=r.getInt(5);

//output page
out.println(“<html ><head><title>WELCOME TO ADDRESS SEARCH</title></head>”);
out.println(“<body><h1> WELCOME TO ADDRESS SEARCH<BR><BR><BR><BR>”);
out.println(“<table WIDTH="100%" CELLPADDING="2"                       CELLSPACING="2" bgcolor=CYAN align="center" BORDER="3">”);
out.println(“<tr><FONT SIZE=32><TH> ID </TH><TH>   NAME</TH> <TH> ADDRESS</TH><TH> CITY </TH><TH> PHONE NO</TH> </TR>”);
      out.println(“<tr><td>”+id1+”<td>”+name1+”<td>”+add1+”<td>”+city1+”<td>”+no1+”</tr>”);
out.println(“</table></body></html>”);
}
       }

catch(Exception e)
{
out.println(e);
}
      }
}


Followers