当尝试在数据库结果中获取数据时,它为零行,但是当尝试在 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;
}
所以我需要帮助来解决这个问题,它在我的项目中非常重要,没有这些数据就无法继续
最佳答案
您正在执行 rs.getInt("acdemicYearCourseID")
,但 acdemicYearCourseID
列不在您的 SELECT
列列表中。
另外尝试将 getInt("...")
、getString("...")
更改为 getInt(1),
getString(2)
关于java - 在JSP中通过PreparedStatement获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42805743/