Write a Menu driven program in java for Insert Record into Table,Update the existing Record and Display all records from the table
Write a Menu driven program in java for the following:
1. Insert Record into Table
2. Update the existing Record
3. Display all records from the table
Program:
import java.sql.*; import java.io.*; public class MenuRec { public statuc void main(String args[])throws SQLException, ClassNotFoundException, IOException { int ch,rollNo,marks,k; String name; String sql; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); COnnection con = DriverManager.getConnection("jdbc:odbc:MyDSN"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Statement stmt; do { System.out.println("Menu:"); System.out.println("1. Insert \n 2.Update \n 3.Display:"); System.out.println("Enter Choice:"); ch = Integer.parseInt(br.readLine()); switch(ch) { case 1: stmt = con.createStatement(); System.out.println("Enter Roll No:"); rollNo = Integer.parseInt(br.readLine()); System.out.println("Enter Name:"); name = br.readLine(); System.out.println("Enter Marks:"); marks = Integer.parseInt(br.readLine()); sql = "insert into Result values ("+rollNo+",'"+name+"',"+marks+")"; k = stmt.executeUpdate(sql); if(k>0) { System.out.println("Record Inserted."); } stmt.close(); break; case 2: stmt = con.createStatement(); System.out.println("Enter Roll No:"); rollNo = Integer.parseInt(br.readLine()); System.out.println("Enter Name:"); name = br.readLine(); System.out.println("Enter Marks:"); marks = Integer.parseInt(br.readLine()); sql = "update Result set name='"+name+"',marks="+marks+" where roll="+rollNo; k = stmt.executeUpdate(sql); if(k>0) { System.out.println("Record Updated."); } stmt.close(); break; case 3: stmt = con.createStatement(); sql = "select roll,name, marks from Result"; ResultSet res = stmt.executeQuery(sql); while(res.next()){ System.out.println(res.getInt(1)+"\t"+res.getString(2)+"\t"+res.getInt(3)); } break; default: System.out.println("Invalid Choice."); } } while(ch<=3); } }
Comments
Post a Comment