Advertisement
Guest User

Untitled

a guest
Mar 6th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. package classselector;
  2.  
  3. import java.sql.*;
  4. import java.util.Scanner;;
  5.  
  6. public class ClassSelectorApp {
  7.  
  8. public static void main(String[] args) throws SQLException {
  9.  
  10. int menuItem = -1;
  11. while (menuItem != 0) {
  12. menuItem = menu();
  13. switch (menuItem) {
  14. case 1:
  15. createStudent();
  16. break;
  17. case 2:
  18. signUp();
  19. break;
  20. case 3:
  21. listClasses();
  22. break;
  23. case 0:
  24. break;
  25. default:
  26. System.out.println("Invalid Input");
  27. break;
  28.  
  29. }
  30. }
  31. }
  32.  
  33. protected static int menu() {
  34. try {
  35. int choice;
  36. Scanner sc = new Scanner(System.in);
  37. System.out.println("n Class Selection Menu");
  38. System.out.println("**********************************");
  39. System.out.println("0: Exit Menu");
  40. System.out.println("1: Create New Student");
  41. System.out.println("2: Sign Up For a Class");
  42. System.out.println("3: List Classes for All Students");
  43. System.out.println("**********************************");
  44. System.out.println("Enter a choice: ");
  45. choice = sc.nextInt();
  46. return choice;
  47.  
  48. } catch (java.util.InputMismatchException e) {
  49. System.out.println("Invalid choice!");
  50. } catch (Exception e) {
  51. System.out.println("Something went wrong...");
  52. }
  53. return 0;
  54. }
  55. static void createStudent() {
  56. System.out.println("nCreate Studentn");
  57. try {
  58. Scanner input = new Scanner(System.in);
  59. System.out.println("Enter a Student ID: ");
  60. String student_id = input.nextLine();
  61. System.out.println("Enter Student Name: ");
  62. String student_name = input.nextLine();
  63. System.out.println("Enter Student Hometown: ");
  64. String hometown = input.nextLine();
  65. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ClassSelector?autoReconnect=true&useSSL=false", "root", "");
  66. String sql = "INSERT INTO students" + "(student_id, student_name, hometown)" + "VALUES (?, ?, ?)";
  67. PreparedStatement myStmt = con.prepareStatement(sql);
  68. myStmt.setString(1, student_id);
  69. myStmt.setString(2, student_name);
  70. myStmt.setString(3, hometown);
  71. myStmt.executeUpdate();
  72. System.out.println("New Student Added");
  73.  
  74. } catch (SQLIntegrityConstraintViolationException ex) {
  75. System.out.println("This entry has duplicate student ID or Student Name, please try again");
  76. } catch (SQLException SQL) {
  77. SQL.printStackTrace();
  78. } catch (Exception exc) {
  79. exc.printStackTrace();
  80. }
  81. }
  82.  
  83. static void signUp() {
  84. System.out.println("nSign Up For a Classn");
  85. try {
  86. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ClassSelector?autoReconnect=true&useSSL=false", "root", "Volks91!");
  87. Statement myStmt = con.createStatement();
  88.  
  89. Scanner input = new Scanner(System.in);
  90. System.out.println("Enter Student ID: ");
  91. String user_entered_student_id = input.nextLine();
  92.  
  93.  
  94.  
  95. ResultSet rs;
  96. rs = myStmt.executeQuery("SELECT student_name FROM ClassSelector.students WHERE student_id = " + user_entered_student_id);
  97. while (rs.next()) {
  98. String userEnterId = rs.getString("student_name");
  99. System.out.println("Is " + userEnterId + " the correct student? (Y/N)");
  100. String confirm = input.nextLine();
  101.  
  102. if (confirm.equalsIgnoreCase("Y")) {
  103. ResultSet rs2 = myStmt.executeQuery("SELECT * FROM ClassSelector.classes");
  104. while (rs2.next()) {
  105. String avlClasses = rs2.getString("class_id") + "t" + rs2.getString("classname") + "t" + rs2.getString("description");
  106. System.out.println(avlClasses);
  107. }
  108. } else if (confirm.equalsIgnoreCase("N")) {
  109. System.out.println("Oops, let start over");
  110. return;
  111. }
  112.  
  113. System.out.println("Enter Class ID from Classes Listed Above to Join: ");
  114. String selectedClass = input.nextLine();
  115. ResultSet rs3 = myStmt.executeQuery("SELECT * FROM ClassSelector.classes WHERE class_id = " + selectedClass);
  116. while (rs3.next()) {
  117. String innerJoin = (userEnterId + " has been added to " + rs3.getString("classname") + " " + rs3.getString("class_id"));
  118. System.out.println(innerJoin);
  119. String student_classJoin = "INSERT IGNORE INTO student_x_class" + "(student_id,student_name, class_id, classname)" + "VALUES (?, ?, ?, ?)";
  120. PreparedStatement pStmt = con.prepareStatement(student_classJoin);
  121. pStmt.setString(1, user_entered_student_id);
  122. pStmt.setString(2, userEnterId);
  123. pStmt.setString(3, rs3.getString("class_id"));
  124. pStmt.setString(4, rs3.getString("classname"));
  125. pStmt.executeUpdate();
  126. System.out.println("Would you like to enroll " + userEnterId + " into another class? (Y/N)");
  127. String additionalClass = input.nextLine();
  128. if(additionalClass.equalsIgnoreCase("Y")){
  129. signUp();
  130. }
  131. else{
  132. return;
  133. }
  134. }
  135. }
  136. } catch (java.sql.SQLException SQL) {
  137. SQL.printStackTrace();
  138. } catch (Exception EXC) {
  139. EXC.printStackTrace();
  140. }
  141. }
  142.  
  143. static void listClasses() {
  144. System.out.println("nStudent Enrollmentn");
  145. try {
  146. Scanner input = new Scanner(System.in);
  147. System.out.println("Enter Student ID to See What Classes they are enrolled in: ");
  148. String user_entered_student_id = input.nextLine();
  149.  
  150. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ClassSelector?autoReconnect=true&useSSL=false", "root", "Volks91!");
  151. Statement myStmt = con.createStatement();
  152.  
  153. ResultSet rs;
  154. boolean found = false;
  155. rs = myStmt.executeQuery("SELECT student_id, student_name, class_id, classname FROM ClassSelector.student_x_class WHERE student_id = " + user_entered_student_id);
  156. while (rs.next()) {
  157. String studentInClass = (rs.getString("student_id") + "t" + rs.getString("student_name") + " " + rs.getString("class_id") + " " + rs.getString("classname"));
  158. if (user_entered_student_id.equals(rs.getString("student_id"))) {
  159. System.out.println(studentInClass);
  160. found = true;
  161. }
  162. }
  163. if (!found) {
  164. System.out.println("This Student does not Exist!");
  165. }
  166.  
  167. } catch (java.sql.SQLException SQL) {
  168. SQL.printStackTrace();
  169. } catch (Exception EXC) {
  170. EXC.printStackTrace();
  171. }
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement