Advertisement
Guest User

Untitled

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