我正在尝试将用户输入的值(学生ID)与mysql数据库中的student_id值进行比较。我想让用户输入一个学生ID并返回一个字符串,上面写着“Is(student_name)是正确的学生吗?”来自数据库的student_name。这就是我到目前为止,signUp()是switch语句的一部分。我认为问题来自user_entered_student_id变量。我试图将其作为用户输入的id,但是当我尝试运行该程序时,它表示该变量从未使用过。任何想法都会有所帮助!
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();
}
}
答案 0 :(得分:2)
此声明不使用user_entered_student_id
变量:
ResultSet rs = myStmt.executeQuery(
"SELECT student_name FROM ClassSelector.students " +
"WHERE student_id = user_entered_student_id");
应该是:
ResultSet rs = myStmt.executeQuery(
"SELECT student_name FROM ClassSelector.students " +
"WHERE student_id = " + user_entered_student_id);