TY_BCS_A_JAVA_SLIP7



SLIP 7:
Create the table student with the fields roll number ,name, percentage using postgresql. Write a menu driven program (command line interface) to perform the following oprations on student table.
a. insert
b. modify
c. delete
d. search
e. view all
f. exit
/* Slip 7_2 : */

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

class Slip7_2
{
            static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

            Connection con;
            PreparedStatement ps;
            Statement st;
            ResultSet rs;
            Slip7_2() throws Exception
            {
                        Class.forName("com.mysql.jdbc.Driver");
                        con=DriverManager.getConnection("jdbc:mysql:/Employee","root","nrc");
                        System.out.println("connected to DB");
            }

            void insert()throws Exception
            {
                        System.out.print("Enter Roll no :");
                        int rno=Integer.parseInt(br.readLine());

                        System.out.print("Enter Name :");
                        String name=br.readLine();
                        a
                                    System.out.print("Enter Percentage :");
                        float per=Float.parseFloat(br.readLine());

                        String sql="insert into student values(?,?,?)";
                        ps=con.prepareStatement(sql);

                        ps.setInt(1,rno);
                        ps.setString(2,name);
                        ps.setFloat(3,per);

                        int n=ps.executeUpdate();
                        if(n>0)
                                    System.out.println("Record Inserted...");
            }

            void modify()throws Exception
            {
                        System.out.print("Enter Roll no to be updated:");
                        int rno=Integer.parseInt(br.readLine());

                        System.out.print("Enter new Percentage :");
                        float per=Float.parseFloat(br.readLine());

                        String sql="update student set per=? where rno=?";
                        ps=con.prepareStatement(sql);

                        ps.setFloat(1,per);
                        ps.setInt(2,rno);

                        int n=ps.executeUpdate();
                        if(n>0)
                                    System.out.println("Record Updated...");
            }

            void delete()throws Exception
            {
                        System.out.println("Enter Roll No to be deleted :");
                        int rno=Integer.parseInt(br.readLine());
                        String sql="delete from student where rno=?";
                        ps=con.prepareStatement(sql);
                        ps.setInt(1,rno);
                        int n=ps.executeUpdate();
                        if(n>0)
                                    System.out.println("Record Deleted...");
            }

            void search()throws Exception
            {

                        System.out.println("Enter Roll No :");
                        int rno=Integer.parseInt(br.readLine());
                        String searchsql="select * from student where rno=?";
                        ps=con.prepareStatement(searchsql);
                        ps.setInt(1,rno);

                        rs=ps.executeQuery();
                        if(rs.next())
                        {
                                    System.out.println("Roll No :"+rs.getInt(1)
                                                            +"\nName :"+rs.getString(2)
                                                            +"\nPercentage :"+rs.getFloat(3));
                        }
                        else
                                    System.out.println("Roll no not found...");
            }

            void viewAll()throws Exception
            {
                        String sql="select * from student";
                        st=con.createStatement();
                        rs=st.executeQuery(sql);
                        System.out.println("Roll No\t Name \t Percentage");
                        while(rs.next())
                        {
                                    System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getFloat(3));
                        }
            }


            public static void main(String a[]) throws Exception
            {
                        Slip7_2 ob = new Slip7_2();
                        int ch;
                        do
                        {
                                    System.out.println(" 1.Insert\n 2.Modify \n 3.Delete \n 4.Search \n 5.view All \n 0.Exit");
                                    System.out.println("Enter your choice : ");
                                    ch = Integer.parseInt(br.readLine());

                                    switch(ch)
                                    {
                                                case 1 :ob.insert();
                                                            break;
                                                case 2 : ob.modify();
                                                             break;
                                                case 3 : ob.delete();
                                                             break;
                                                case 4 : ob.search();
                                                             break;
                                                case 5 : ob.viewAll();
                                                             break;
                                                case 0 : break;
                                    }
                        }while(ch!=0);

            }
}

No comments:

Post a Comment