Guest User

Untitled

a guest
Dec 3rd, 2017
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. import java.util.*;
  2. import java.sql.*;
  3. import static java.lang.System.out;
  4.  
  5. class StudentMenu {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.         Connection conn = null;
  9.         ResultSet rs = null;
  10.        
  11.         try {
  12.             Class.forName("org.postgresql.Driver");
  13.             conn = DriverManager.getConnection("jdbc:postgresql://localhost/jdbcarjun", "postgres", "");
  14.            
  15.             if(conn == null) {
  16.                 out.println("Failed to establish conncection");
  17.             } else {
  18.                 int choice;
  19.                 boolean userCont = true;
  20.                 out.println("Operations on Student Table");
  21.                 do {
  22.                     out.println("Menu:\n1. Insert\n2. Modify\n3. Delete\n4. Search\n5. View All\n6. Exit");
  23.                     choice = sc.nextInt();
  24.                     if(choice == 1) {
  25.                         int newRoll, newPerc;
  26.                         String newName;
  27.                         out.println("Enter the roll number: ");
  28.                         newRoll = sc.nextInt();
  29.                         out.println("Enter the name: ");
  30.                         newName = sc.next();
  31.                         out.println("Enter percentage: ");
  32.                         newPerc = sc.nextInt();
  33.                         if(newPerc < 0 || newPerc > 100) {
  34.                             out.println("Not a valid value");                          
  35.                         } else {
  36.                             PreparedStatement ps = conn.prepareStatement("INSERT INTO students VALUES(?,?,?)");
  37.                             ps.setInt(1, newRoll);
  38.                             ps.setString(2, newName);
  39.                             ps.setInt(3, newPerc);
  40.                             try {
  41.                                 if(ps.executeUpdate() == 1)
  42.                                     out.println("Database insertion successful!");
  43.                             } catch (Exception e) {
  44.                                 out.println(e);
  45.                             }
  46.                             ps.close();
  47.                         }
  48.                     } else if(choice == 2) {
  49.                         out.println("WIP");
  50.                     } else if(choice == 3) {
  51.                         out.println("WIP");
  52.                     } else if(choice == 4) {
  53.                         out.println("Search by:\n1: Roll No\n2: Name\n3: Percentage Less Than\n4: Percentage More Than\n5: Percentage Equals");
  54.                         int usrSearchChoice = sc.nextInt();
  55.                         PreparedStatement ps = null;
  56.                         if(usrSearchChoice == 1) {
  57.                             ps = conn.prepareStatement("SELECT * FROM students WHERE rollno=?");
  58.                             out.println("Enter roll number to search for: ");
  59.                             ps.setInt(1, sc.nextInt());
  60.                             rs = ps.executeQuery();
  61.                             while(rs.next()) {
  62.                                 out.println("Name: " + rs.getString(2) + "\t" + "Percentage: " + rs.getString(3));
  63.                             }
  64.                         } else if(usrSearchChoice == 2) {
  65.                         } else if(usrSearchChoice == 3) {
  66.                         } else if(usrSearchChoice == 4) {
  67.                         } else if(usrSearchChoice == 5) {
  68.                         } else {
  69.                             out.println("Invalid choice");
  70.                         }
  71.                     } else if(choice == 5) {
  72.                         Statement stmt = conn.createStatement();
  73.                         ResultSet rset = stmt.executeQuery("SELECT * FROM students");  
  74.                         out.println("-----------");
  75.                         while(rset.next()) {
  76.                             out.println("Roll No: " + rset.getInt(1));
  77.                             out.println("Name: " + rset.getString(2));
  78.                             out.println("Percentage: " + rset.getInt(3));
  79.                         }
  80.                         out.println("------------");
  81.                     } else if(choice == 6) {
  82.                         userCont = false;
  83.                     } else {
  84.                         out.println("Invalid choice. Try again.");
  85.                     }
  86.                 } while(userCont);
  87.                
  88.             }
  89.         } catch(Exception e) {
  90.             out.println(e);
  91.         }
  92.     }
  93. }
Add Comment
Please, Sign In to add comment