I have a Java application that uses a few switch statements to get user information about students and classes. I have three MySQL tables:
students
student_id | student_name | hometown
classes
class_id | classname | description
student_x_classes
student_id | student_name | class_id | classname
The code I have is below. Its works fine, but I must admit, is pretty ugly. I've been trying to simplify it and break down the big ugly methods to make it more readable but keep struggling. I'm very new to Java and MySQL, so your patience is appreciated!
package classselector;
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;
}
}
}
protected static int menu() {
try {
int choice;
Scanner sc = new Scanner(System.in);
System.out.println("\n Class Selection Menu");
System.out.println("**********************************");
System.out.println("0: Exit Menu");
System.out.println("1: Create New Student");
System.out.println("2: Sign Up For a Class");
System.out.println("3: List Classes for All Students");
System.out.println("**********************************");
System.out.println("Enter a choice: ");
choice = sc.nextInt();
return choice;
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid choice!");
} catch (Exception e) {
System.out.println("Something went wrong...");
}
return 0;
}
static void createStudent() {
System.out.println("\nCreate Student\n");
try {
Scanner input = new Scanner(System.in);
System.out.println("Enter a Student ID: ");
String student_id = input.nextLine();
System.out.println("Enter Student Name: ");
String student_name = input.nextLine();
System.out.println("Enter Student Hometown: ");
String hometown = input.nextLine();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ClassSelector?autoReconnect=true&useSSL=false", "root", "");
String sql = "INSERT INTO students" + "(student_id, student_name, hometown)" + "VALUES (?, ?, ?)";
PreparedStatement myStmt = con.prepareStatement(sql);
myStmt.setString(1, student_id);
myStmt.setString(2, student_name);
myStmt.setString(3, hometown);
myStmt.executeUpdate();
System.out.println("New Student Added");
} catch (SQLIntegrityConstraintViolationException ex) {
System.out.println("This entry has duplicate student ID or Student Name, please try again");
} catch (SQLException SQL) {
SQL.printStackTrace();
} catch (Exception exc) {
exc.printStackTrace();
}
}
static void signUp() {
System.out.println("\nSign Up For a Class\n");
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ClassSelector?autoReconnect=true&useSSL=false", "root", "");
Statement myStmt = con.createStatement();
Scanner input = new Scanner(System.in);
System.out.println("Enter Student ID: ");
String user_entered_student_id = input.nextLine();
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 = myStmt.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 = myStmt.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();
}
else{
return;
}
}
}
} catch (java.sql.SQLException SQL) {
SQL.printStackTrace();
} catch (Exception EXC) {
EXC.printStackTrace();
}
}
static void listClasses() {
System.out.println("\nStudent Enrollment\n");
try {
Scanner input = new Scanner(System.in);
System.out.println("Enter Student ID to See What Classes they are enrolled in: ");
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();
ResultSet rs;
boolean found = false;
rs = myStmt.executeQuery("SELECT student_id, student_name, class_id, classname FROM ClassSelector.student_x_class WHERE student_id = " + user_entered_student_id);
while (rs.next()) {
String studentInClass = (rs.getString("student_id") + "\t" + rs.getString("student_name") + " " + rs.getString("class_id") + " " + rs.getString("classname"));
if (user_entered_student_id.equals(rs.getString("student_id"))) {
System.out.println(studentInClass);
found = true;
}
}
if (!found) {
System.out.println("This Student does not Exist!");
}
} catch (java.sql.SQLException SQL) {
SQL.printStackTrace();
} catch (Exception EXC) {
EXC.printStackTrace();
}
}
}