老师你好,我有几个问题想请教一下老师
1.我的showCourse.jsp显示页面不正常,为什么呢?我的jsp页面代码如下
<table border = "1px solid black"> <tr> <td>编号</td> <td>课程名称</td> <td>所属方向</td> <td>课程描述</td> <td>创建时间</td> </tr> <c:forEach items = "${list}" var = "l"> <tr> <td>${l.id}</td> <td>${l.name}<td> <td>${l.category}<td> <td>${l.desp}<td> <td>${l.createTime}<td> </tr> </c:forEach> </table>
2.每次添加完只显示存储在list中的数据,而不是数据库中的数据,如何修改?相关页面代码如下:
//addServlet public class AddCourseServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); String category = request.getParameter("category"); String desp = request.getParameter("desp"); Course course = new Course(null,name,category,desp,new Date(System.currentTimeMillis())); CourseService cs = new CourseServiceImpl(); int i= cs.add(course); List list = new ArrayList(); list.add(course); request.setAttribute("list", list); if(i > 0) { request.getRequestDispatcher("showCourse.jsp").forward(request, response); } else { System.out.println("添加失败"); request.getRequestDispatcher("fail.jsp").forward(request, response); } } } //queryServlet protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection conn = null; PreparedStatement pstmt = null; ResultSet res = null; boolean flag = false; List<Course> list = new ArrayList(); try { conn = JDBCUtils.getConnection(); String sql = "select * from course"; pstmt = conn.prepareStatement(sql); res = pstmt.executeQuery(); while(res.next()) { flag = true; String id = res.getString("id"); String name = res.getString("name"); String category = res.getString("category"); String desp = res.getString("desp"); Date createTime = res.getDate("createTime"); list.add(new Course(id,name,category,desp,createTime)); } if(flag) { request.getServletContext().setAttribute("list", list); request.getRequestDispatcher("showCourse.jsp").forward(request, response); } else{ request.getRequestDispatcher("fail.jsp").forward(request, response); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { JDBCUtils.release(res, conn, pstmt); } }
麻烦老师给我看看代码,找出我的问题所在,提出修改建议,感谢!
25
收起
正在回答 回答被采纳积分+1
2回答
3. Java 数据库开发与实战应用
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星