在PreparedStatement上获取JSP中的数据

时间:2017-03-15 09:30:39

标签: java mysql jsp

当尝试在数据库中获取数据结果时,它会变为零行,但是当尝试复制并过去在mysql上查询时,返回所需的特定行数。

与mysql服务器的连接

private Connection connection() {
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/olesdb", "root", "");
    } catch (Exception e) {
        //System.out.println("Connection error");
    }
    return con;
}

**

我的获取数据的功能

**

public List<AcademicYearCourses> getStudentCourse(int studentID, int academicYear,int semester) throws SQLException{
    List<AcademicYearCourses> list = new ArrayList<>();
    PreparedStatement sta = connection().prepareStatement("SELECT courseCode,courseName FROM courses co,studentprograms stpro,academicyearcourse acco WHERE stpro.studentID=? AND acco.academicYearID=? AND acco.semesterID=? AND stpro.programID= acco.programID AND stpro.studyYear=acco.studyYear AND acco.courseID=co.courseID");
    sta.setInt(1, studentID);
    sta.setInt(2, academicYear);
    sta.setInt(3, semester);
    ResultSet res = sta.executeQuery();
    while(res.next()){
        AcademicYearCourses acco = new AcademicYearCourses();
        acco.setAcdemicYearCourseID(rs.getInt("acdemicYearCourseID"));
        acco.setCourseName(rs.getString("courseName"));
        acco.setCourseCode(rs.getString("courseCode"));
        list.add(acco);
    }
    return list;
}

所以我需要帮助来解决这个问题,这在我的项目中非常重要,并且在没有这些数据的情况下可以继续

1 个答案:

答案 0 :(得分:0)

您正在rs.getInt("acdemicYearCourseID"),但acdemicYearCourseID列列表中不包含SELECT列。

同时尝试将getInt("...")getString("...")更改为getInt(1), getString(2)