首页 > 解决方案 > java.sql.SQLException:未找到列“等级”

问题描述

我有 3 个表 user(id,name), course(id,name) 和 users_courses(user_id,course_id,grade)

我在 DbCourse.java 中使用了这个查询(适用于 phpmyadmin)

    private final String GET_STUDENT_COURSES = "select * from users_courses where user_id = ? ";

以及我想获取带有课程名称和成绩的哈希图的方法

public HashMap<String, Double> getHashMapStudentCourses(User user) {
    HashMap<String, Double> studentCourses = new HashMap<>();
    try {
        ps = con.prepareStatement(GET_STUDENT_COURSES);
        ps.setInt(1, user.getId());
        rs = ps.executeQuery();
        while(rs.next()){
            String courseName =(this.get_course(rs.getInt("course_id"))).getCourse_name();
            studentCourses.put(courseName,rs.getDouble("grade"));
        }
    } catch (SQLException ex) {
        Logger.getLogger(DbCourse.class.getName()).log(Level.SEVERE, null, ex);
    }
    return studentCourses;
}

这是 get_course 方法

 public Course get_course(int id) {
    try {
        ps = con.prepareStatement(GET_COURSE);
        ps.setInt(1, id);
        rs = ps.executeQuery();
        if (rs.first()) {
            return new Course(rs.getInt("id"), rs.getString("name"));
        }
    } catch (SQLException ex) {
        Logger.getLogger(DbCourse.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

当我使用它时出现此异常 java.sql.SQLException: Column 'grade' not found。请帮助我不知道错误在哪里

伙计们,在我评论了 while 循环中的 2 行之后,我得到了 stackoverflow 异常,这是什么原因

标签: javasqldatabaseprepared-statementresultset

解决方案


推荐阅读