Write a Java program to accept the details of Teacher(tno, name, salary) from the user and store into the database table and update the salary of techer yo entered salary amount and tno.
Write a Java program to accept the details of Teacher(tno, name, salary) from the user and store into the database table and update the salary of techer yo entered salary amount and tno.
Program:
import java.io.*; import java.sql.*; class Teacher { public static void main(String args[]) throws SQLException, ClassNotFoundException, IOException { int tno, salary, choice, r; String name; Statement s; ResultSet rs; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:teacherDSN"); do { System.out.println("Menu"); System.out.println("\n 1. Add Teacher \n 2. Update Teacher Salary"); switch(choice) { case 1: PreparedStatement ps = con.prepareStatement("insert into Teacher values(?,?,?)"); System.out.println("Enter Teacher No:"); tno = Integer.parseInt(br.readLine()); System.out.println("Enter Teacher Name:"); name = br.readLine(); System.out.println("Enter Teacher Salary:"); salary = Integer.parseInt(br.readLine()); ps.setInt(1, tno); ps.setString(2, name); ps.setInt(3, salary); r = ps.executeUpdate(); if(r>0){ System.out.println("Record Saved."); } ps.close(); s = con.createStatement(); rs = s.executeQuery("select * from Teacher"); while(rs.next()){ System.out.println("Teacher = [No:"+rs.getInt(1)+", Name:"+rs.getString(2)+", Salary:"+rs.getInt(3)+"]\n"); } rs.close(); s.close(); break; case 2: System.out.println("Enter tno to Update Salary:"); tno = Integer.parseInt(br.readLine()); System.out.println("Enter Teacher Salary:"); salary = Integer.parseInt(br.readLine()); s = con.createStatement(); r = s.executeUpdate("update Teacher set salary="+salary+" where tno="+tno); if(r>0){ System.out.println("Record Updated Successfully"); } s.close(); s = con.createStatement(); rs = s.executeQuery("select * from Teacher"); while(rs.next()){ System.out.println("Teacher = [No:"+rs.getInt(1)+", Name:"+rs.getString(2)+", Salary:"+rs.getInt(3)+"]\n"); } rs.close(); s.close(); break; default: System.out.println("Invalid Choice."); } } while(choice<=2); con.close(); } }
Comments
Post a Comment