Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.Scanner;;
- public class ClassSelectorApp {
- public static void main(String[] args) throws SQLException {
- int menuItem = -1;
- while (menuItem != 0) {
- menuItem = menu();
- switch (menuItem) {
- case 1:
- createStudent();
- break;
- case 2:
- signUp();
- break;
- case 3:
- listClasses();
- break;
- case 0:
- break;
- default:
- System.out.println("Invalid Input");
- break;
- }
- }
- }
- static void signUp() {
- System.out.println("nSign Up For a Classn");
- try {
- Scanner input = new Scanner(System.in);
- System.out.println("Enter Student ID: ");
- String user_entered_student_id = input.nextLine();
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ClassSelector?autoReconnect=true&useSSL=false", "root", "");
- Statement myStmt = con.createStatement();
- Statement myStmt2 = con.createStatement();
- Statement myStmt3 = con.createStatement();
- ResultSet rs;
- rs = myStmt.executeQuery("SELECT student_name FROM ClassSelector.students WHERE student_id = " + user_entered_student_id);
- while (rs.next()) {
- String userEnterId = rs.getString("student_name");
- System.out.println("Is " + userEnterId + " the correct student? (Y/N)");
- String confirm = input.nextLine();
- if (confirm.equalsIgnoreCase("Y")) {
- ResultSet rs2 = myStmt2.executeQuery("SELECT * FROM ClassSelector.classes");
- while (rs2.next()) {
- String avlClasses = rs2.getString("class_id") + "t" + rs2.getString("classname") + "t" + rs2.getString("description");
- System.out.println(avlClasses);
- }
- } else if (confirm.equalsIgnoreCase("N")) {
- System.out.println("Oops, let start over");
- return;
- }
- System.out.println("Enter Class ID from Classes Listed Above to Join: ");
- String selectedClass = input.nextLine();
- ResultSet rs3 = myStmt3.executeQuery("SELECT * FROM ClassSelector.classes WHERE class_id = " + selectedClass);
- while (rs3.next()) {
- String innerJoin = (userEnterId + " has been added to " + rs3.getString("classname") + " " + rs3.getString("class_id"));
- System.out.println(innerJoin);
- String student_classJoin = "INSERT IGNORE INTO student_x_class" + "(student_id,student_name, class_id, classname)" + "VALUES (?, ?, ?, ?)";
- PreparedStatement pStmt = con.prepareStatement(student_classJoin);
- pStmt.setString(1, user_entered_student_id);
- pStmt.setString(2, userEnterId);
- pStmt.setString(3, rs3.getString("class_id"));
- pStmt.setString(4, rs3.getString("classname"));
- pStmt.executeUpdate();
- System.out.println("Would you like to enroll " + userEnterId + " into another class? (Y/N)");
- String additionalClass = input.nextLine();
- if(additionalClass.equalsIgnoreCase("Y")){
- signUp();
- }
- }
- }
- } catch (java.sql.SQLException SQL) {
- SQL.printStackTrace();
- } catch (Exception EXC) {
- EXC.printStackTrace();
- }
- }
- public void signUp(){
- System.out.println("nSign Up For a Classn");
- try{
- Scanner input = new Scanner(System.in);
- System.out.println("Enter Student ID: ");
- String user_entered_student_id = input.nextLine();
- String userEnterId = student_name + (user_entered_student_id);
- System.out.println("Enter Class ID from Classes Listed Above to Join: ");
- String selectedClass = input.nextLine();
- student_x_class(selectedClass);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void student_name(Scanner input,String user_entered_student_id, Statement myStmt, Connection con) {
- try{
- myStmt = con.createStatement();
- String query = ("SELECT student_name FROM ClassSelector.students WHERE student_id = " + user_entered_student_id);
- ResultSet rs;
- rs = myStmt.executeQuery(query);
- while (rs.next()) {
- String userEnterId = rs.getString("student_name");
- System.out.println("Is " + userEnterId + " the correct student? (Y/N)");
- String confirm = input.nextLine();
- if (confirm.equalsIgnoreCase("Y")) {
- getClass();
- }
- else if (confirm.equalsIgnoreCase("N")) {
- System.out.println("Oops, let start over");
- }
- return;
- }
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void getClass(ResultSet rs, Statement myStmt){
- try{
- rs = myStmt.executeQuery("SELECT * FROM ClassSelector.classes");
- while (rs.next()) {
- String avlClasses = rs.getString("class_id") + "t" + rs.getString("classname") + "t" + rs.getString("description");
- System.out.println(avlClasses);
- }
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void student_x_class(Scanner input,String selectedClass, String userEnterId, String user_entered_student_id, ResultSet rs, Statement myStmt, Connection con){
- try{
- rs = myStmt.executeQuery("SELECT * FROM ClassSelector.classes WHERE class_id = " + selectedClass);
- while (rs.next()) {
- String innerJoin = (userEnterId + " has been added to " + rs.getString("classname") + " " + rs.getString("class_id"));
- System.out.println(innerJoin);
- String student_classJoin = "INSERT IGNORE INTO student_x_class" + "(student_id,student_name, class_id, classname)" + "VALUES (?, ?, ?, ?)";
- PreparedStatement pStmt = con.prepareStatement(student_classJoin);
- pStmt.setString(1, user_entered_student_id);
- pStmt.setString(2, userEnterId);
- pStmt.setString(3, rs.getString("class_id"));
- pStmt.setString(4, rs.getString("classname"));
- pStmt.executeUpdate();
- System.out.println("Would you like to enroll " + userEnterId + " into another class? (Y/N)");
- String additionalClass = input.nextLine();
- if(additionalClass.equalsIgnoreCase("Y")){
- signUp();
- }
- }
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement