View Menu Application in ORM Architecture with DAO using Java JDBC
Program 1
package dao; import model.Student; import java.sql.*; import myconnection.MyCon; import java.util.*; public class StudentDAO { Connection con=null; PreparedStatement ps=null; String sql; ResultSet rs=null; public boolean insertStudent(Student S) throws Exception { con=MyCon.getConnection(); sql="insert into student values(?,?,?,?,?)"; ps=con.prepareStatement(sql); ps.setInt(1, S.getRno()); ps.setString(2,S.getName()); ps.setInt(3,S.getPhy()); ps.setInt(4, S.getChem()); ps.setInt(5, S.getMath()); if(ps.executeUpdate()>0) return true; return false; } public Student searchStudent(int rno) throws Exception { con=MyCon.getConnection(); sql="select * from student where sno=?"; ps=con.prepareStatement(sql); ps.setInt(1, rno); rs=ps.executeQuery(); Student S=new Student(); if(rs.next()) { S.setRno(rs.getInt(1)); S.setName(rs.getString(2)); S.setPhy(rs.getInt(3)); S.setChem(rs.getInt(4)); S.setMath(rs.getInt(5)); } else S=null; return S; } public boolean deleteStudent(int rno) throws Exception { con=MyCon.getConnection(); sql="delete from student where sno=?"; ps=con.prepareStatement(sql); ps.setInt(1,rno); if(ps.executeUpdate()>0) return true; return false; } public List<Student> searchAll() throws Exception { sql="select * from student"; con=MyCon.getConnection(); ps=con.prepareStatement(sql); rs=ps.executeQuery(); List<Student>mylist=new ArrayList<Student>(); while(rs.next()) { Student S=new Student(); S.setRno(rs.getInt(1)); S.setName(rs.getString(2)); S.setPhy(rs.getInt(3)); S.setChem(rs.getInt(4)); S.setMath(rs.getInt(5)); mylist.add(S); S=null; } return mylist; } public boolean updateStudent(Student S) throws Exception { sql="update student set name=?,phy=?,chem=?,math=? where sno=?"; con=MyCon.getConnection(); ps=con.prepareStatement(sql); ps.setString(1, S.getName()); ps.setInt(2, S.getPhy()); ps.setInt(3, S.getMath()); ps.setInt(4, S.getChem()); ps.setInt(5, S.getRno()); if(ps.executeUpdate()>0) return true; return false; } }
Program 2
package view; import dao.StudentDAO; import java.io.Console; import java.util.*; import model.Student; public class TestMain { public static void main(String[] args) throws Exception { // Update Student // // Student S=new Student(); // S.setRno(103); // S.setName("Rahul"); // S.setPhy(88); // S.setChem(88); // S.setMath(88); // StudentDAO sd=new StudentDAO(); //// // if(sd.updateStudent(S)) // System.out.println("Record updated........"); // // Search All // int rollno; // Scanner scan=new Scanner(System.in); // System.out.println("Enter Roll no for Search"); // rollno=scan.nextInt(); // StudentDAO sd=new StudentDAO(); // Student S=null; // S=sd.searchStudent(rollno); // if(S==null) // System.out.println("Record not found......."); // else // { // System.out.println("Roll No: "+S.getRno()); // System.out.println("Name: "+S.getName()); // System.out.println("Physics: "+S.getPhy()); // System.out.println("Physics: "+S.getChem()); // System.out.println("Physics: "+S.getMath()); // String choice; // System.out.println("Are You sure want to delete"); // choice=scan.next(); // if(choice.toLowerCase().equals("yes")) // { // if(sd.deleteStudent(rollno)) // System.out.println("Record delete"); // else // System.out.println("Record not found....."); // // } // //delete code // Search Student Record // int rollno; // Scanner scan=new Scanner(System.in); // System.out.println("Enter Roll no for Search"); // rollno=scan.nextInt(); // StudentDAO sd=new StudentDAO(); // Student S=null; // S=sd.searchStudent(rollno); // if(S==null) // System.out.println("Record not found......."); // else // { // System.out.println("Roll No: "+S.getRno()); // System.out.println("Name: "+S.getName()); // System.out.println("Physics: "+S.getPhy()); // System.out.println("Physics: "+S.getChem()); // System.out.println("Physics: "+S.getMath()); // String choice; // System.out.println("Are You sure want to delete"); // choice=scan.next(); // if(choice.toLowerCase().equals("yes")) // { // if(sd.deleteStudent(rollno)) // System.out.println("Record delete"); // else // System.out.println("Record not found....."); // // } // } // } // Insert Student Code //Read data int rollno,p,c,m; String name; Scanner scan=new Scanner(System.in); System.out.println("Enter Roll no"); rollno=scan.nextInt(); System.out.println("Enter Name"); name=scan.next(); System.out.println("Enter Physics Marks:"); p=scan.nextInt(); System.out.println("Enter Chemistry Marks:"); c=scan.nextInt(); System.out.println("Enter Maths Marks:"); m=scan.nextInt(); // Model Class Student S=new Student(); S.setRno(rollno); S.setName(name); S.setPhy(p); S.setChem(m); S.setMath(m); StudentDAO sd=new StudentDAO(); if(sd.insertStudent(S)) System.out.println("Record inserted...."); } }
Program 3
package view; import dao.StudentDAO; import java.util.*; import model.Student; public class TestMainView { public static void main(String[] args) { int choice; do { System.out.println("------------Student Menu Application--------------"); System.out.println("1. Insert Record"); System.out.println("2. Search Record By Roll No"); System.out.println("3. Delete Record Ny Roll No"); System.out.println("4. Search All Record"); System.out.println("5. Update Record"); System.out.println("6.Exit"); System.out.println("--------------------------------------------------"); Scanner scan=new Scanner(System.in); System.out.println("Enter Your Choice"); choice=scan.nextInt(); switch(choice) { case 1:insertData();break; case 2:searchData();break; case 3:deleteData();break; case 4:searchAllData();break; case 5:updateData();break; } }while(choice !=6); } public static void searchData() { try { int rollno; Scanner scan=new Scanner(System.in); System.out.println("Enter Roll no for Search"); rollno=scan.nextInt(); StudentDAO sd=new StudentDAO(); Student S=null; S=sd.searchStudent(rollno); if(S==null) System.out.println("Record not found......."); else { System.out.println("Roll No: "+S.getRno()); System.out.println("Name: "+S.getName()); System.out.println("Physics: "+S.getPhy()); System.out.println("Physics: "+S.getChem()); System.out.println("Physics: "+S.getMath()); } } catch(Exception e) { System.out.println(e); } } public static void deleteData() { try { int rollno; Scanner scan=new Scanner(System.in); System.out.println("Enter Roll no for Search"); rollno=scan.nextInt(); StudentDAO sd=new StudentDAO(); Student S=null; S=sd.searchStudent(rollno); if(S==null) System.out.println("Record not found......."); else { System.out.println("Roll No: "+S.getRno()); System.out.println("Name: "+S.getName()); System.out.println("Physics: "+S.getPhy()); System.out.println("Physics: "+S.getChem()); System.out.println("Physics: "+S.getMath()); String choice; System.out.println("Are You sure want to delete"); choice=scan.next(); if(choice.toLowerCase().equals("yes")) { if(sd.deleteStudent(rollno)) System.out.println("Record delete"); else System.out.println("Record not found....."); } } } catch(Exception e) { System.out.println(e); } } public static void searchAllData() { try { List<Student>mylist1=new ArrayList<Student>(); StudentDAO sd=new StudentDAO(); mylist1=sd.searchAll(); for(Student S:mylist1) { System.out.print(" " +S.getRno()); System.out.print(" " +S.getName()); System.out.print(" " +S.getPhy()); System.out.print(" " +S.getChem()); System.out.println(" " +S.getMath()); } } catch(Exception e) { System.out.println(e); } } public static void updateData() { } public static void insertData() { try { int rollno,p,c,m; String name; Scanner scan=new Scanner(System.in); System.out.println("Enter Roll no"); rollno=scan.nextInt(); System.out.println("Enter Name"); name=scan.next(); System.out.println("Enter Physics Marks:"); p=scan.nextInt(); System.out.println("Enter Chemistry Marks:"); c=scan.nextInt(); System.out.println("Enter Maths Marks:"); m=scan.nextInt(); // Model Class Student S=new Student(); S.setRno(rollno); S.setName(name); S.setPhy(p); S.setChem(m); S.setMath(m); StudentDAO sd=new StudentDAO(); if(sd.insertStudent(S)) System.out.println("Record inserted...."); } catch(Exception e) { System.out.println(e); } } }