JAVA控制台多表查询时怎么写
publicvoidStudentDAOQueryGrades(StringStuID,StringCourseID)//学生按照课程号查询成绩信息的数据库操作{try{...
public void StudentDAOQueryGrades(String StuID,String CourseID)//学生按照课程号查询成绩信息的数据库操作
{
try {
Connection conn =DbConn.getConnection();
String sql="SELECT course.CourseID,course.CourseName,course.CourseHour,"+
" course.CourseScore,stugrades.Grade FROM course JOIN stugrades"+
" ON course.CourseID=stugrades.CourseID WHERE "+
" StuID='"+StuID+"' AND stugrades.CourseID='"+CourseID+"'";
Statement stmttest=conn.createStatement();
//------------声明SQL语句-----------end------------//
//----4--------得到结果集-----------start------------//
ResultSet rstst=stmttest.executeQuery(sql);
//------------得到结果集-----------end------------//
//------5------得到结果-----------start------------//
System.out.println("课程号 | 课程名 | 课时 | 学分 | 成绩 ");
System.out.println("----------------------------------------");
while(rstst.next()) //请问这个地方怎么改?它好像只能是单表查询时用这个错误
//错误提示找不到CourseID
{
String kechenghao=rstst.getString("CoureID");
String kechengming=rstst.getString("CourseName");
String keshi=rstst.getString("CourseHour");
String xuefen=rstst.getString("CourseScore");
String chengji=rstst.getString("Grade");
System.out.println(kechenghao+" | "+kechengming+" | "+keshi+" | "+xuefen+" | "+chengji );
System.out.println("-----------------------------------------");
}
//------------得到结果-----------end------------//
}
catch (Exception e) {
e.printStackTrace();
System.out.println(" "+e.getMessage());
}
} 展开
{
try {
Connection conn =DbConn.getConnection();
String sql="SELECT course.CourseID,course.CourseName,course.CourseHour,"+
" course.CourseScore,stugrades.Grade FROM course JOIN stugrades"+
" ON course.CourseID=stugrades.CourseID WHERE "+
" StuID='"+StuID+"' AND stugrades.CourseID='"+CourseID+"'";
Statement stmttest=conn.createStatement();
//------------声明SQL语句-----------end------------//
//----4--------得到结果集-----------start------------//
ResultSet rstst=stmttest.executeQuery(sql);
//------------得到结果集-----------end------------//
//------5------得到结果-----------start------------//
System.out.println("课程号 | 课程名 | 课时 | 学分 | 成绩 ");
System.out.println("----------------------------------------");
while(rstst.next()) //请问这个地方怎么改?它好像只能是单表查询时用这个错误
//错误提示找不到CourseID
{
String kechenghao=rstst.getString("CoureID");
String kechengming=rstst.getString("CourseName");
String keshi=rstst.getString("CourseHour");
String xuefen=rstst.getString("CourseScore");
String chengji=rstst.getString("Grade");
System.out.println(kechenghao+" | "+kechengming+" | "+keshi+" | "+xuefen+" | "+chengji );
System.out.println("-----------------------------------------");
}
//------------得到结果-----------end------------//
}
catch (Exception e) {
e.printStackTrace();
System.out.println(" "+e.getMessage());
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询