Advertisement
Guest User

Untitled

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