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