81,092
社区成员
发帖
与我相关
我的任务
分享
<!-- 信息的表格展示-->
<div>
<table id="ScoreTable" class="table table-bordered" align="center">
<tr>
<th>序号</th>
<th>学生学号</th>
<th>学生姓名</th>
<th>课程号</th>
<th>课程名称</th>
<th>成绩分数</th>
</tr>
<% request.setCharacterEncoding("UTF-8"); %>
<%
List<Score> scores = null;
String stuId = request.getParameter("stuId");
if (stuId == null||stuId.equals("")) {
ScoreDAO scoreDAOImp=new ScoreDAOImp();
scores=scoreDAOImp.query();
}
else {
ScoreDAO scoreDAO = new ScoreDAOImp();
scores = scoreDAO.queryByStu(stuId);
}
for (Score item : scores) {
%>
<tr>
<td><%=item.getId()%>
</td>
<td><%=item.getStuID()%>
</td>
<td><%=item.getStuName()%>
</td>
<td><%=item.getCourID()%>
</td>
<td><%=item.getCourName()%>
</td>
<td><input type="text" id="newScore" name="newScore" value="<%=item.getScore()%>"></td>
</tr>
<%
}
%>
</table>
public List<Score> queryByStu(String id) {
List<Score> scores = new ArrayList<Score>();
Connection conn = null;
PreparedStatement statement = null;
ResultSet rs = null;
try {
conn = JDBCUtil.getConnection();
//3.用来执行sql语句
String sql = "select t_score.CourseId,t_course.CourseName,t_score.Score FROM t_score INNER JOIN t_student on t_score.StudentId=? INNER JOIN t_course on t_score.CourseId=t_course.Id";//一般不要写*,会降低性能
statement = conn.prepareStatement(sql);//对sql语句进行预编译,按住strl不放鼠标移上去可以看到定义,查看源代码
statement.setString(1,id);
//4.执行查询,获取resultset结果集
rs = statement.executeQuery();
//5.读取数据
while (rs.next()) {
Integer courId = rs.getInt("t_score.CourseId");
String courName = rs.getString("t_course.CourseName");
BigDecimal stuScore = rs.getBigDecimal("t_score.Score");
Score score = new Score();
score.setCourID(courId);
score.setCourName(courName);
score.setScore(stuScore);
scores.add(score);
}
} catch (Exception ex) {
ex.printStackTrace();//打印堆栈异常
} finally {
//6.关闭连接
JDBCUtil.release(rs, statement, conn);
}
return scores;
}
public List<Score> query() {
List<Score> scores = new ArrayList<Score>();
Connection conn = null;
PreparedStatement statement = null;
ResultSet rs = null;
try {
conn = JDBCUtil.getConnection();
//3.用来执行sql语句
String sql = "select t_score.Id,t_score.StudentId,t_student.Sname,t_score.CourseId,t_course.CourseName,t_score.Score FROM t_score INNER JOIN t_student on t_score.StudentId=t_student.Id INNER JOIN t_course on t_score.CourseId=t_course.Id";//一般不要写*,会降低性能
statement = conn.prepareStatement(sql);//对sql语句进行预编译,按住strl不放鼠标移上去可以看到定义,查看源代码
//4.执行查询,获取resultset结果集
rs = statement.executeQuery();
//5.读取数据
while (rs.next()) {
Integer id = rs.getInt("t_score.Id");
String stuId = rs.getString("t_score.StudentId");
String stuName = rs.getString("t_student.Sname");
Integer courId = rs.getInt("t_score.CourseId");
String courName = rs.getString("t_course.CourseName");
BigDecimal stuScore = rs.getBigDecimal("t_score.Score");
Score score = new Score();
score.setId(id);
score.setStuID(stuId);
score.setStuName(stuName);
score.setCourID(courId);
score.setCourName(courName);
score.setScore(stuScore);
scores.add(score);
}
} catch (Exception ex) {
ex.printStackTrace();//打印堆栈异常
} finally {
//6.关闭连接
JDBCUtil.release(rs, statement, conn);
}
return scores;
}