Write a java program to accept the details of Patient(pno,pname) from user and insert it into the database, display it and delete the record of entered pno from the table.
import java.sql.*; import java.io.*; import javax.sql.*; class slip8 { public static void main(String args[]) { Connection con; Statement state; ResultSet rs; int ch; boolean flag=true; String decision; int pno; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:my1dsn.dsn"); 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.Delete the existing record"); System.out.println("3.Display all records"); 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 Patient no:"); pno=Integer.parseInt(br.readLine()); System.out.println("Enter patient name:"); String pname=br.readLine(); String sql="insert into patient values(?,?)"; PreparedStatement p=con.prepareStatement(sql); p.setInt(1,pno); p.setString(2,pname); p.executeUpdate(); System.out.println("Record added"); break; case 2: state=con.createStatement(); while(flag) { System.out.println("Enter patient no for delete:"); pno=Integer.parseInt(br.readLine()); sql="delete from patient where pno="+pno; System.out.println(sql); int rows=state.executeUpdate(sql); System.out.println(rows+"Rows successfully deleted"); System.out.println("Do you want to delete more record(y/n):"); decision=br.readLine().toLowerCase(); if(decision.charAt(0)=='n') flag=false; } break; case 3: state=con.createStatement(); sql="Select * from patient"; rs=state.executeQuery(sql); while(rs.next()) { System.out.println("\n"); System.out.println("\t"+rs.getInt(1)); System.out.println("\t"+rs.getString(2)); } break; case 4: System.exit(0); default: System.out.println("Invalid choice"); break; }} while(ch!=4); } catch(Exception e) { System.out.println(e); } } }
Write a java program to accept the details of Patient(pno,pname) from user and insert it into the database, display it and delete the record of entered pno from the table.
Reviewed by
on
April 27, 2015
Rating: