Rassisland
Rassisland

Reputation: 167

comparing user-entered data to SQL database data

I'm trying to compare a user-entered value(student id) to the student_id value in the mysql database. I want to have the user enter a student id and return a string saying "Is (student_name) the correct student?" with the student_name coming from the database. This is what I have so far, the signUp() is part of a switch statement. I think the problem is coming from the user_entered_student_id variable. I'm trying to have that be the user-entered id, but when I try to run the program it says that variable is never used. Any ideas would help!

    static void signUp() {
        System.out.println("\nSign Up For a Class\n");
        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", "", "");
            Statement myStmt = 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?");


 String confirm = input.nextLine();
                    if(confirm == "Y"){
                    System.out.println("Enter ID From Classes Available: ");
                    System.out.println("SELECT * FROM ClassSelector.classes");
                    }
                }
            } catch (Exception exc) {
                exc.printStackTrace();
            }
        }

Upvotes: 0

Views: 321

Answers (1)

Maciej Dobrowolski
Maciej Dobrowolski

Reputation: 12140

This statement does not use user_entered_student_id variable:

ResultSet rs = myStmt.executeQuery(
    "SELECT student_name FROM ClassSelector.students " +
    "WHERE student_id = user_entered_student_id");

It should rather be:

ResultSet rs = myStmt.executeQuery(
    "SELECT student_name FROM ClassSelector.students " +
    "WHERE student_id = " + user_entered_student_id);

Upvotes: 2

Related Questions