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

1 comment: