Write a java program to read n integers into LinkedList collection Do the following operations i) Display only negative integers ii) Delete the last element

import java.io.*;
import java.util.*;
class Slip29
{
    public static void main(String ar[]) throws IOException
    {
        int n;
        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
        System.out.print("\t\t Enter the size of Linked List ");
        n= Integer.parseInt(br.readLine());
        LinkedList<Integer> ll = new LinkedList<Integer>();
        for(int i=0;i<n;i++)
        {
           System.out.println("Enter the element ");
           ll.add(Integer.parseInt(br.readLine()));
        }
        Iterator<Integer> it = ll.iterator();
        System.out.println(" Contents of Linked List using iterator ");
        while(it.hasNext())
           System.out.println(" \t " + it.next());
        it = ll.iterator();
        System.out.println(" List of negative numbers ");
        while(it.hasNext())
        {
            int no=it.next();
            if(no<0)
               System.out.println("\t" + no);
        }
        ll.removeLast();
        it=ll.iterator();
           System.out.println(" Contents of linked list after removing last element... ");
        while(it.hasNext())
          System.out.println("\t" +it.next());
    }
}

Write a java program which reads a series of first names & stores them in Linked List The program should not allow duplicate names and it should also allow the user to search first name.

import java.io.*;
import java.util.*;
class Slip30
{
    public static void main(String ar[]) throws IOException
    {
        int n;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter size of linkedList...");
        n = Integer.parseInt(br.readLine());
        LinkedList <String> ll = new LinkedList <String>();
        for(int i=0;i<n;i++)
        {
            System.out.println("Enter the element...");
            String str = br.readLine();
            if(ll.contains(str)==true)
            {
                System.out.println("The element is already existing...");
                i--;
            }
            else
            ll.add(str);
        }
        Iterator <String> it = ll.iterator();
        System.out.println("Contents of Linked List using iterator...");
        while(it.hasNext())
          System.out.println("\t" + it.next());
        System.out.println("Enter the element to search : ");
        String search = br.readLine();
        int pos = ll.indexOf(search);
        if(pos== -1)
           System.out.println(" The element not found" );
        else
           System.out.println(" The element found at location :  " +pos);
    }
}

Write a JAVA program to accept names of n students and insert into LinkedLlist i) Display the contents of list using Iterator. ii) Display the content in reverse order. (using ListIterator)

//roll no:-5211
//slp no:-28
import java.util.*;
import java.io.*;
class slip28
{
public static void main(String args[])
{
String str;
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
LinkedList a=new LinkedList();
System.out.println("Enter a no of String you want:");
int n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++)
{
 System.out.println("Enter string:");
str=br.readLine();
a.add(str);
}
 System.out.println("Display LinkedList:");
Iterator iter=a.iterator();
while(iter.hasNext())
{
System.out.println(iter.next());
}
Collections.reverse(a);
System.out.println("Reversing LinkedList containtns:"+a);
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Error");
}
}
}

/* *****output******
F:\5211>javac slip28.java
Note: slip28.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

F:\5211>java slip28
Enter a no of String you want:
2
Enter string:

Rupesh
Enter string:
Rohan
Display LinkedList:
Rupesh

Rohan
Reversing LinkedList containtns:[Rohan,Rupesh]
*/

Write a JAVA program to read n strings & insert into ArrayList collection. Display the elements of collection in reverse order.

import java.util.*;
import java.io.*;
class Slip27
{
    public static void main(String ar[])throws IOException
    {
        int n;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("\t Enter size of arraylist ");
        n=Integer.parseInt(br.readLine());
        ArrayList <String> a = new ArrayList<String>(n);
        for(int i=0;i<n;i++)
        {
            System.out.println("Enter element");
            a.add(br.readLine());
        }
        System.out.println(" The contents of arraylist using iterator..");
        Iterator <String> it = a.iterator();
        while(it.hasNext())
           System.out.println(" \t  " + it.next());
        ListIterator <String> lit = a.listIterator();
           while(lit.hasNext())
         lit.next();
           System.out.println("The contents of arraylist in reverse order... ");
          while(lit.hasPrevious())
           System.out.println("  \t  " + lit.previous());
    }
    }

Write a JAVA program using servlet to count the no of times a servlet has been invoked.


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


    public class slip24 extends implements
    {

    static int numreq=0;
    proctected void doGet(ServletRequest reg,ServletResponse res)

    throws ServletException,IOException
    {
    res.setContentType("text/html");
    PrintWriter out=res.getWriter();
    out.println("Servlet invoke :");
    numreq++;
    out.println("numreq");
    out.close();

    }
}

Write a JAVA program to accept mobile handset details through html page and servlet will insert the details in handset table(Assume handset table is already present)

import java.io.*;
import java.sql.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Slip22 extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        String modelno=request.getParameter("modelno");
        String modelname=request.getParameter("modelname");
        Connection con;
        Statement state;
        ResultSet rs;
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection("jdbc:odbc:mobile");
            String sql="insert into mobile values(?,?)";
            PreparedStatement p=con.prepareStatement(sql);
            p.setString(1,modelno);
            p.setString(2,modelname);
            p.executeUpdate();
            out.println("Record Inserted");
            p.close();
            con.close();
        }catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

Write a JAVA program Design a screen with two buttons start thread and stop thread. Clicking on start ,it should start printing “Thread running” until stop button is pressed.

import java.awt.*;
import java.awt.event.*;

public class s19 extends Frame implements

ActionListener,Runnable
 {
   Button start,stop;
                 TextField tf;
      int x=0,y=0;
     String msg="";
   Thread t1=new Thread(this);
      public s19()
       {
             setLayout(new FlowLayout());
             start=new Button("start");
             stop=new Button("stop");
            add(start);
            add(stop);

         start.addActionListener(this);
         stop.addActionListener(this);
                  addWindowListener(new

WindowAdapter()
                     {
                        public void

windowClosing(WindowEvent e)
                         {
                           System.exit(0);
                            }
                      });
                     
                     setSize(200,200);
                     setVisible(true);
              }
 
  public void actionPerformed(ActionEvent ae)
     {
             Button btn=(Button)ae.getSource();
              if(btn==start)
              {
                  t1.start();
                 }
       

      if(btn==stop)
              {
                  t1.stop();
                 }
}


public void run()
 {
   try
   {
      while(true)
       {
          repaint();
          Thread.sleep(350);
         }
  }


catch(Exception e)
{
  }
 }

public void paint(Graphics g)
  {
   msg=msg+"thread running";
  g.drawString(msg,10,y+=10);
    }

  public static void main(String args[])
    {
     new s19();
      }
   }

Write a JAVA program using servlet for e-mail registration with user-id, password, name, address, fields & display the details on next page.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Slip21 extends HttpServlet
  {
     public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
    
      {

       String uid = req.getParameter("uid");
       String Password = req.getParameter("Password");
       String name = req.getParameter("Name");
       String addr = req.getParameter("Address");
       res.setContentType("text/html");
       PrintWriter pw = res.getWriter();
       pw.println("\nUser id : "+uid);
       pw.println("\nPassword : "+Password);
       pw.println("\nName : "+name);
       pw.println("\nAddress : " +addr);
       pw.close();
     }
}

Write a Java program to display “Hello Java” 50 times using multithreading

class mythread implements Runnable
{
    Thread t;
    public mythread(String title)
    {
    t=new Thread(this,title);
    t.start();
    }
    public void run()
    {
    for(int i=0;i<50;i++)
{
System.out.println((i+1)+"ThreadName:"+Thread.currentThread().getName());
try
{
Thread.sleep(100);
}
catch(Exception e)
{
}
}}}
public class slip18
{
public static void main(String a [])
{
System.out.println("ThreadName:"+Thread.currentThread().getName());
mythread mt=new mythread("Hello Java");
}
}   

/*
OUTPUT
---------------------------------------------------------------
D:\ROHAN\Semester\sem 6>javac slip18.java

D:\ROHAN\Semester\sem 6>java  slip18
ThreadName:main
1ThreadName:Hello Java
2ThreadName:Hello Java
3ThreadName:Hello Java
4ThreadName:Hello Java
5ThreadName:Hello Java
6ThreadName:Hello Java
7ThreadName:Hello Java
8ThreadName:Hello Java
9ThreadName:Hello Java
10ThreadName:Hello Java
11ThreadName:Hello Java
12ThreadName:Hello Java
13ThreadName:Hello Java
14ThreadName:Hello Java
15ThreadName:Hello Java
16ThreadName:Hello Java
17ThreadName:Hello Java
18ThreadName:Hello Java
19ThreadName:Hello Java
20ThreadName:Hello Java
21ThreadName:Hello Java
22ThreadName:Hello Java
23ThreadName:Hello Java
24ThreadName:Hello Java
25ThreadName:Hello Java
26ThreadName:Hello Java
27ThreadName:Hello Java
28ThreadName:Hello Java
29ThreadName:Hello Java
30ThreadName:Hello Java
31ThreadName:Hello Java
32ThreadName:Hello Java
33ThreadName:Hello Java
34ThreadName:Hello Java
35ThreadName:Hello Java
36ThreadName:Hello Java
37ThreadName:Hello Java
38ThreadName:Hello Java
39ThreadName:Hello Java
40ThreadName:Hello Java
41ThreadName:Hello Java
42ThreadName:Hello Java
43ThreadName:Hello Java
44ThreadName:Hello Java
45ThreadName:Hello Java
46ThreadName:Hello Java
47ThreadName:Hello Java
48ThreadName:Hello Java
49ThreadName:Hello Java
50ThreadName:Hello Java
---------------------------------------------------------------
*/

Write a JAVA program which will create two child threads by implementing Runnable interface; one thread will print even nos from 1 to 50 and other display vowels

import java.io.*;
class x1 extends Thread

{
 public void run()
 {
   for(int i=1;i<=50;i++)
{
 if(i%2==0)
System.out.println("even no" +i);
}
}
}

class x2 implements Runnable
{
 public void run()
{
   try

{
BufferedReader br=new BufferedReader(new

InputStreamReader(System.in));
System.out.println("Enter the string");
String data=br.readLine();
int n1=data.length();
char p[]=data.toCharArray();
System.out.println("vowel is");
for(int i=0;i<n1;i++)
{
if(p[i]=='a'|| p[i]=='e'|| p[i]=='i'|| p[i]=='o'|| 

p[i]=='u')
 System.out.println(p[i]);
}
}
catch(Exception e)
{
}

}
}

class s17
{
public static void main(String args[])
{
try
{
 x1 runnable=new x1();
Thread tx1=new Thread(runnable);
tx1.start();
tx1.sleep(50);

 x2 runnable1=new x2();
Thread  tx2=new Thread(runnable);
//x2.data=argv[i];
tx2.start();
}
catch(Exception e)
{
}

}
}

output:-


D:\5242>java s17
even no2
even no4
even no6
even no8
even no10
even no12
even no14
even no16
even no18
even no20
even no22
even no24
even no26
even no28
even no30
even no32
even no34
even no36
even no38
even no40
even no42
even no44
even no46
even no48
even no50
even no2
even no4
even no6
even no8
even no10
even no12
even no14
even no16
even no18
even no20
even no22
even no24
even no26
even no28
even no30
even no32
even no34
even no36
even no38
even no40
even no42
even no44
even no46
even no48
even no50

Write a JAVA program which will generate following threads I. To display 10 terms of Fibonacci series. II. To display 1 to 20 in reverse order

import java.io.*;
class fib extends Thread

{
 public void run()
 {
   try

{

int a=0,b=1,c=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Limit");
int n=Integer.parseInt(br.readLine());
System.out.println("Fibonacci series:");
while(n>0)
{
  System.out.println(c);
a=b;
b=c;
c=a+b;
n=n-1;
}
}
catch(Exception e)
{
}
}
}
class rev extends Thread
{
 public void run()
{
try
{
System.out.println("Reverse is");
for(int i=20;i>=1;i--)
System.out.println(i);
}

catch(Exception e)
{
 }
}
}
class s16
{
public static void main(String args[])
{
 try
{
  fib t1=new fib();
t1.start();
t1.sleep(5000);
rev t2=new rev();
t2.start();
}
catch(Exception e)
{
}
}
}


output:-


D:\5242>javac s16.java

D:\5242>java s16
Enter the Limit
2
Fibonacci series:
0
1
Reverse is
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1

Write a JAVA program to accept the rno of student as a command line argument and display the record of student (rno, sname, per) on the screen.

import java.sql.*;
import java.io.*;

class Search
{
public static void main(String args[])
{
Connection conn=null;
ResultSet rs=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:nik");
Statement stmt=conn.createStatement();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("enter roll no u want to search");
int rno=Integer.parseInt(br.readLine());
rs=stmt.executeQuery("select * from stud where Rno="+rno);
System.out.println("success");
while(rs.next())
{
System.out.println("RollNo="+rs.getInt(1)+""+"name="+rs.getString(2)+" "+"per="+rs.getInt(3));
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}

Output:-
----------------------------------------------------------

D:\5242>javac Search.java

D:\5242>java Search
enter roll no u want to search
1
success
RollNo=1 name=rohan per=90


Write a JAVA program to accept the details of Employee (Eno , Ename, Sal) from the user, store it into the database and display that details on the screen .

import java.sql.*;
import java.io.*;

    class Slip13
    {
    public ststic void main(String args[])
    {
       if(args.length!=3)
       {
        System.out.println("Wrong number Argument"0;
        System.exit(1);
       }
      
       int Eno,Sal;
       String Ename.query;
       Connection conn=null;
       Statement stmt=null;
       resultSet rs=null;

    try
       {
        Eno=Integer.parseInt(args[0]);
       }
       catch(numberFormatException e)
       {
        Eno=5;
        System.out.println("Error");
       }

        Ename=args[1]);
    try
       {
        Sal=Integer.parseInt(args[2]);
       }
       catch(numberFormatException e)
       {
        Sal=45;
        System.out.println("Error");
       }
    try
       {
        class.foeNAme("Sun.jdbc.odbc.JdbcOdbcDriver");
        Con=Drivermanager.getConnection9"jdbc:odbc:t       );
        stmt-Conn.createStatement();

        query=("insert into empl values('"+Eno+"','"+Ename+"','"+Sal+"'););
        System.out.println("Query");
        conn.commit();
        System.out.println("record has been inserted");
       }
catch(Exception e)
{
System.out.println("Exception occour=+e);
}
try

Write a JAVA program to accept the number from the user and do the following - Calculate Factorial of a given Number. - To check whether given number is prime or not. (Use Thread)

class NewThread implements Runnable
{
     int n=5,f=1;
    Thread t;
    NewThread()
    {
       t=new Thread(this,"factorial");
     System.out.println("Main thread "+t);
       t.start();
    }
        public void run()
       {
          try
           {
              while(n>0)
             {
               f=f*n;
              n=n-1;
             }
               System.out.println(f);                    
                   Thread.sleep(100);
              
       
             }
              catch(InterruptedException e)
               {
                  System.out.println("Exception"); 
               }
                System.out.println("Exiting Main thread");
       }
   
}

class Thread5
{
    public static void main(String args[])
    {
       new NewThread();
    new NewThread1();
                System.out.println("Exiting main thread");
       }
}



class NewThread1 implements Runnable
{
Thread t1;
NewThread1()
{
    t1=new Thread(this,"Prime number");
     System.out.println("t1 is created"+t1);
    t1.start();
}
public void run()
{
int i=2,no=11 ,flag=1;
      
         try
           {
int n2= no/2;
    if(n2==0)
     System.out.println("no.is not prime");
    else
     System.out.println("no.is prime");
                   Thread.sleep(1000);
          }
            catch(InterruptedException e)
               {
                  System.out.println("Exception"); 
               }
}
}

Write a JAVA program to display the record of Student (rno, sname, per) on the screen by selecting rno from the Choice component.

import java.io.*;
import java.sql.*;
import javax.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Slip10 extends Frame implements

ItemListener
{
    Connection con;
    ResultSet rs;
    Statement state;
    Choice choice1;
    TextField t1,t2,t3;
    Label l1,l2,l3;
       
    public Slip10()
    {
        try
        {
           

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
           

con=DriverManager.getConnection("jdbc:odbc:myd

sn");
           

state=con.createStatement();
            String sql="select *

from student";
           

rs=state.executeQuery(sql);
       
            setLayout(new

FlowLayout());
            setSize(300,300);
            t1=new

TextField(10);
            t2=new

TextField(10);
            t3=new

TextField(10);
            l1=new Label("Roll

No: ");
            l2=new

Label("Name:");
            l3=new

Label("Percentage:");
            choice1=new

Choice();
            while(rs.next())
            {
               

choice1.add(rs.getString(1));
            }
        }catch(Exception e)
        {
           

System.out.println(e);
        }

        add(choice1);
        add(l1);
        add(t1);
        add(l2);
        add(t2);
        add(l3);
        add(t3);
        choice1.addItemListener(this);
       
        addWindowListener(new

WindowAdapter()
        {
            public void

windowClosing(WindowEvent we)
            {
               

setVisible(false);
               

System.exit(0);
                dispose();
            }
        });
    }
   
    public void itemStateChanged(ItemEvent

ie)
    {
        try
        {
           

state=con.createStatement();
            int

no=Integer.parseInt(choice1.getSelectedItem());
           

rs=state.executeQuery("select * from student

where Code=" +no);
           
            while(rs.next())
            {
               

t1.setText(rs.getString(1));
               

t2.setText(rs.getString(2));
               

t3.setText(rs.getString(3));
            }
        }catch(Exception e)
        {
           

System.out.println(e);
        }
    }

    public static void main(String args[])
    {
        Slip10 obj=new Slip10();
        obj.setVisible(true);
    }
}

Write a JAVA program to accept the details of teacher (Tno,TName,Sal) from the user, store it into the table and update the salary of employee to entered salary amount and eno is entered employee eno.

import java.sql.*;
import javax.sql.*;
import java.io.*;

class Slip11
{
    public static void main(String args[])
    {
        Connection con;
        ResultSet rs;
        Statement state;
        try
        {
           

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
           

System.out.println("Driver Loaded");
           

con=DriverManager.getConnection("jdbc:odbc:myd

sn");
           

System.out.println("Statement object created");
            BufferedReader

br=new BufferedReader(new

InputStreamReader(System.in));
           

System.out.println("Enter Teacher Number: ");
            int

no=Integer.parseInt(br.readLine());
           

System.out.println("Enter Name: ");
            String

name=br.readLine();
           

System.out.println("Enter Salary: ");
            int

sal=Integer.parseInt(br.readLine());

            String sql="insert

into teacher values(?,?,?)";
            PreparedStatement

p=con.prepareStatement(sql);
            p.setInt(1,no);
            p.setString(2,name);
            p.setInt(3,sal);
            p.executeUpdate();
           

System.out.println("Record Added");

           

System.out.println("Enter Teacher Number for the

record you wish to Update: ");
           

no=Integer.parseInt(br.readLine());
           

System.out.println("Enter new Name: ");
            name=br.readLine();
           

System.out.println("Enter new Salary: ");
           

sal=Integer.parseInt(br.readLine());
            sql="update teacher

set Name=?, Salary=? where Code=?";
           

p=con.prepareStatement(sql);
            p.setString(1,name);
            p.setInt(2,sal);
            p.setInt(3,no);
            p.executeUpdate();
           

System.out.println("Record Updated");   
           
            p.close();
            con.close();

        }catch(Exception e)
        {
           

System.out.println(e);
        }
    }
}

Write a JAVA program to accept the details of Doctor (DNo, DName, Address, Salary) from the user. Insert it into the table and display it on the screen. (use Command Line arguments).

import java.sql.*;
class Slip9
{
   public static void main(String args[])
   {
        Connection con;
        PreparedStatement pstmt;
        ResultSet rs;
        try
        {
            if(args.length==0)
            {
               System.out.println("Pass the argument");
               System.exit(0);
            }
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            System.out.println("Driver loaded");
            con=DriverManager.getConnection("jdbc:odbc:Bookdsn");
            System.out.println("Connection Established");
            if(args[0].equals("|")|| args[0].equals("i"))
            {
                 pstmt=con.prepareStatement("Insert into Doctor values(?,?,?,?)");
                 pstmt.setInt(1,Integer.parseInt(args[1]));
                 pstmt.setString(2,args[2]);
                 pstmt.setString(3,args[3]);
                 pstmt.setFloat(4,Float.parseFloat(args[4]));
                 pstmt.executeUpdate();
                 System.out.println("Inserted record successfully");
            }
            pstmt=con.prepareStatement("Select * from Doctor");
            rs=pstmt.executeQuery();
            while(rs.next());
            {
                System.out.println("" +rs.getInt(1)+""+rs.getString(2)+""+rs.getString(3)+""+rs.getFloat(4));
            }
            rs.close();
            con.close();
       }
       catch(ClassNotFoundException e)
       {
           e.printStackTrace();
       }
       catch(SQLException e)
       {
           e.printStackTrace();
       }
       catch(Exception e)
       {
           e.printStackTrace();
       }
    }
}

Write a JAVA program to accept the details of Patient (pno , pname) from the user and insert it into the database , display it and delete the record of entered pno from the table.

import java.sql.*;
import javax.sql.*;
import java.io.*;

class Slip8
{
    public static void main(String args[])
    {
        Connection con;
        ResultSet rs;
        Statement state;
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            System.out.println("Driver Loaded");
            con=DriverManager.getConnection("jdbc:odbc:mydsn");
            System.out.println("Statement object created");
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter patient Number: ");
            int no=Integer.parseInt(br.readLine());
            System.out.println("Enter Name: ");
            String name=br.readLine();

            String sql="insert into patient values(?,?)";
            PreparedStatement p=con.prepareStatement(sql);
            p.setInt(1,no);
            p.setString(2,name);
            p.executeUpdate();
            System.out.println("Record Added");

            state=con.createStatement();
            rs=state.executeQuery("select * from patient where pno=" +no);
            while(rs.next())
            {
                System.out.println("\n");
                System.out.print("\t" +rs.getInt(1));
                System.out.print("\t" +rs.getString(2));
            }

            System.out.println("\n\n Enter the PNO of the record you wish to delete");
            int no1=Integer.parseInt(br.readLine());
            p=con.prepareStatement("delete * from patient where pno=" +no1);
            p.executeUpdate();
            System.out.println("Record Deleted");

            p.close();
            con.close();
        }catch(Exception e)
        {
            System.out.println(e);
        }
    }
}   

Write a JAVA program to accept the empno from the user and update the salary of employee and display the updated Record on the screen. Employee having fields empno , ename and salary.

import java.io.*;
import java.sql.*;

class slip7
{
    public static void main(String args[])throws SQLException,IOException,ClassNotFoundException
    {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        Connection con=DriverManager.getConnection("jdbc:odbc:mdsn");
        Statement stmt=con.createStatement();
        /*System.out.println("Enter Employee name ");
                    String name=br.readLine();
                    System.out.println("Enter Employee number  ");
                        int no=Integer.parseInt(br.readLine());
                        System.out.println("Enter Employee salary ");
                        int sal=Integer.parseInt(br.readLine());
                        String sql="insert into emp values('"+name+"',"+no+","+sal+")";
                        System.out.println("sql  "+sql);
                        int count=stmt.executeUpdate(sql);
                        if(count>0)
                        System.out.println("Added");
                        else
                        System.out.println("Not Added");*/
        System.out.println("*********************Update record*********************");
        System.out.println("Enter empno");
                        int eno=Integer.parseInt(br.readLine());
                        System.out.println("Enter new salary");
                        int esal=Integer.parseInt(br.readLine());
                        //String abv=;
                        //System.out.println(" "+eno+"  "+esal+"  "+abv);
                       
                        PreparedStatement p=null;
                        p=con.prepareStatement("update h12 set  esal=? where empno=?");
                        p.setInt(1,esal);
                        p.setInt(2,eno);
                        int count=p.executeUpdate();
                        if(count>0)
                        System.out.println("Updated");
                        else
                        System.out.println("Not updated");
        }
    }

Write a JAVA program to accept the details of Doctor (Dno , DName, Salary) from the user and insert it into the database. (Use PreparedStatement class and AWT).

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.sql.*;

        class one extends JFrame implements ActionListener
          {

                    JFrame f;
                     JLabel l1,l2,l3;
                     JTextField t1,t2,t3;
                     JButton b1,b2;
                     one()
                        {
                           f=new  JFrame("slp5");
                           l1=new JLabel("dno");
                           l2=new JLabel("dname");
                           l3=new JLabel("salary");   
                           t1=new JTextField(15);
                           t2=new JTextField(15);
                           t3=new JTextField(15);
                           b1=new JButton("Insert ");
                            b2=new JButton("clear ");
                             f.setLayout(new FlowLayout());
                             f.setVisible(true);
                             f.setSize(300,300);
                             b1.addActionListener(this);
                             b2.addActionListener(this);
                             f.add(l1);
                             f.add(t1);
                             f.add(l2);
                             f.add(t2);
                             f.add(l3);
                             f.add(t3);
                             f.add(b1);
                             f.add(b2);
                          }
                 
                        public void actionPerformed(ActionEvent e)
                          {
                              if(e.getSource()==b1)
                                 {
                                        try
                                        {     
                                       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                        Connection con=DriverManager.getConnection("jdbc:odbc:emp");
                                         Statement stmt=con.createStatement();

                                         String name=t2.getText();
                                         int eno=Integer.parseInt(t1.getText());
                                         int sal=Integer.parseInt(t3.getText());
                                         String sql="insert into emp values(";
                                         sql=sql+dno+",'"+name+"',"+sal+")";
                                          System.out.println(sql);
                                          int i=stmt.executeUpdate(sql);
                                           if(i>0)
                                             {
                                                System.out.println("record added ");
                                              }
                                              con.close();
                                             }//try close
                                                catch(Exception a)
                                                {
                                                   }
                                              }
                                         
                                         if(e.getSource()==b2)
                                             {
                                                t1.setText("");
                                               t2.setText("");
                                               t3.setText("");
                                              }
                                }//action performed close
             }//one close
  
             class slp1
              {
                  public static void main(String args[])
                 {
                      one o=new one();
                  }
             }

Write a menu driven program in JAVA for the implementation of Scrollable ResultSet. The menus are- - Move to the next Record. - Move to the first Record. - Move to the Previous Record - Move to the last Record.

import java.io.*;
import java.sql.*;

       public class slip6
     {
         public static void main(String args[])
       {
            try
            {
               BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
               Connection con=null;
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                con=DriverManager.getConnection("jdbc:odbc:imp");
                Statement sta=con.createStatement();
                sta=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
             ResultSet rs=sta.executeQuery("select * from stud");
               
                for(;;)
               {
                    System.out.println("---------MAIN MENU---------");
                      System.out.println("1:Next");
                      System.out.println("2:First");
                      System.out.println("3:Previous");
                       System.out.println("4:Last");
                      System.out.println("5:Exit");
                       System.out.println("ENTER your choice :");
                       int choice=Integer.parseInt(br.readLine());
               
               switch(choice)
                {
                      case 1:
                                  System.out.println("Display next record");
                                  rs.next();
                                   System.out.println("Roll No:"+rs.getInt(1));
                                  System.out.println("Name:"+rs.getString(2));
                                  System.out.println("PERCENTAGE:"+rs.getFloat(3));
                             break;
                       case 2:
                                  System.out.println("Display first record");
                                  rs.first();
                                   System.out.println("Roll No:"+rs.getInt(1));
                                  System.out.println("Name:"+rs.getString(2));
                                  System.out.println("PERCENTAGE:"+rs.getFloat(3));
                             break;
                       case 3:
                                  System.out.println("Display previous record");
                                  rs.previous();
                                   System.out.println("Roll No:"+rs.getInt(1));
                                  System.out.println("Name:"+rs.getString(2));
                                  System.out.println("PERCENTAGE:"+rs.getFloat(3));
                             break;
                          case 4:
                                  System.out.println("Display last record");
                                  rs.last();
                                   System.out.println("Roll No:"+rs.getInt(1));
                                  System.out.println("Name:"+rs.getString(2));
                                  System.out.println("PERCENTAGE:"+rs.getFloat(3));
                             break;
                           case 5:
                                           System.exit(0);
                                     break;
                     }
                      }
                  }
                     catch(Exception e)
                      {
                          System.out.println(e);
                       }
              }
             }


/* ***********Output *************
---------MAIN MENU---------
1:Next
2:First
3:Previous
4:Last
5:Exit
ENTER your choice :
3
Display previous record
Roll No:5213
Name:rohan
PERCENTAGE:99.0
---------MAIN MENU---------
1:Next
2:First
3:Previous
4:Last
5:Exit
ENTER your choice :
*/

Write a JAVA program to accept the details of student (Rno , SName , Per) from the user and insert it into the table. (use PreparedStatement Class).

import java.sql.*;
import java.io.*;

class slip4
{
public static void main(String args[])
{
 if(args.length!=3)
{
 System.out.println("wrong no of argument");
 System.exit(1);
}
int Rno,per;
String sname,query;
Connection conn=null;
ResultSet rs=null;
try
{
Rno=Integer.parseInt(args[0]);
}
catch(NumberFormatException e)
{
Rno=5;
System.out.println("Error");
}
 sname=args[1];
 try
{
 per=Integer.parseInt(args[2]);
}
catch(NumberFormatException e)
{
per=44;
System.out.println("Error");
}
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:my");
PreparedStatement ps=conn.prepareStatement("insert into stud values(?,?,?)");
ps.setInt(1,Rno);
ps.setString(2,sname);
ps.setInt(3,per);
ps.executeUpdate();
System.out.println("record has been inserted");

rs.close();
conn.close();
conn.commit();
}
catch(Exception e)
{
System.out.println("exception occure="+e);
}
}
}

Write a java program to accept the details of Employee (Eno,EName,Sal) from the user and insert it into the Database.(use Awt).

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.sql.*;

        class one extends JFrame implements ActionListener
          {

                    JFrame f;
                     JLabel l1,l2,l3;
                     JTextField t1,t2,t3;
                     JButton b1,b2;
                     one()
                        {
                           f=new  JFrame("slp1");
                           l1=new JLabel("eno");
                           l2=new JLabel("ename");
                           l3=new JLabel("salary");   
                           t1=new JTextField(15);
                           t2=new JTextField(15);
                           t3=new JTextField(15);
                           b1=new JButton("Insert ");
                            b2=new JButton("clear ");
                             f.setLayout(new FlowLayout());
                             f.setVisible(true);
                             f.setSize(300,300);
                             b1.addActionListener(this);
                             b2.addActionListener(this);
                             f.add(l1);
                             f.add(t1);
                             f.add(l2);
                             f.add(t2);
                             f.add(l3);
                             f.add(t3);
                             f.add(b1);
                             f.add(b2);
                          }
                 
                        public void actionPerformed(ActionEvent e)
                          {
                              if(e.getSource()==b1)
                                 {
                                        try
                                        {     
                                       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                        Connection con=DriverManager.getConnection("jdbc:odbc:emp");
                                         Statement stmt=con.createStatement();

                                         String name=t2.getText();
                                         int eno=Integer.parseInt(t1.getText());
                                         int sal=Integer.parseInt(t3.getText());
                                         String sql="insert into emp values(";
                                         sql=sql+eno+",'"+name+"',"+sal+")";
                                          System.out.println(sql);
                                          int i=stmt.executeUpdate(sql);
                                           if(i>0)
                                             {
                                                System.out.println("record added ");
                                              }
                                              con.close();
                                             }//try close
                                                catch(Exception a)
                                                {
                                                   }
                                              }
                                         
                                         if(e.getSource()==b2)
                                             {
                                                t1.setText("");
                                               t2.setText("");
                                               t3.setText("");
                                              }
                                }//action performed close
             }//one close
  
             class slip1
              {
                  public static void main(String args[])
                 {
                      one o=new one();
                  }
             }

Write a Menu driven program in Java for the following. - Insert Record into the Tablle. - Update The Existing Record. - Display all the Records from the Tabe.

import java.sql.*;
import java.io.*;
import javax.sql.*;

class Slip2
{
    public static void main(String args[])
    {
        Connection con;
        Statement state;
        ResultSet rs;
        int ch;
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            System.out.println("Driver Loaded");
            con=DriverManager.getConnection("jdbc:odbc:zzz");
            System.out.println("Statement object created");

            do
            {
                System.out.println("\n");
                System.out.println("Menu:");
                System.out.println("1.Insert Record into the Table");
                System.out.println("2.Update The Existing Record.");
                System.out.println("3.Display all the Records from the Table");
                System.out.println("4.Exit");
                System.out.println("Enter your choice: ");

                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                ch=Integer.parseInt(br.readLine());

                switch(ch)
                {
                    case 1:
                    System.out.println("Enter Employee Number: ");
                    int no=Integer.parseInt(br.readLine());
                    System.out.println("Enter Employee Name: ");
                    String name=br.readLine();
                    System.out.println("Enter Employee Salary: ");
                    int sal=Integer.parseInt(br.readLine());
                    String sql="insert into employee values(?,?,?)";
                    PreparedStatement p=con.prepareStatement(sql);
                    p.setInt(1,no);
                    p.setString(2,name);
                    p.setInt(3,sal);
                    p.executeUpdate();
                    System.out.println("Record Added");
                    //p.close();
                    //con.close();
                    break;

                    case 2:
                    System.out.println("Enter Employee Number for the record you wish to Update: ");
                    no=Integer.parseInt(br.readLine());
                    System.out.println("Enter new Name: ");
                    name=br.readLine();
                    System.out.println("Enter new Salary: ");
                    sal=Integer.parseInt(br.readLine());
                    sql="update employee set Name=?, Salary=? where Code=?";
                    p=con.prepareStatement(sql);
                    p.setString(1,name);
                    p.setInt(2,sal);
                    p.setInt(3,no);
                    p.executeUpdate();
                    System.out.println("Record Updated");
                    //p.close();
                    //con.close();
                    break;

                    case 3:
                    state=con.createStatement();
                    sql="select * from employee";
                    rs=state.executeQuery(sql);
                    while(rs.next())
                    {
                        System.out.println("\n");
                        System.out.print("\t" +rs.getInt(1));
                        System.out.print("\t" +rs.getString(2));
                        System.out.print("\t" +rs.getInt(3));
                    }
                    break;

                    case 4:
                    System.exit(0);

                    default:
                    System.out.println("Invalid Choice");
                    break;
                }
            }while(ch!=4);
        }catch(Exception e)
        {
            System.out.println(e);
        }
    }
}