0

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();
            }
        }
1
  • 1
    Use PreparedStatement and pass your parameter. Commented Feb 26, 2016 at 23:08

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

@Rassisland as PM 77-1 already pointed out in the comment below your question - you should use PreparedStatement to create query with parameters - it's safer and easier to avoid such mistakes.
I updated the code and added an extra bit that isn't working either. I'm trying to get the confirm "Y" to run the code, but after I enter a Y, it just resets and doesn't print the last two lines, any ideads?
@Rassisland, Strings comparision should be done via String#equals method. In your case it should be confirm.equals("Y")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.