javaweb之浏览功能
今天我们来写浏览功能,浏览主要是通过sql语句将数据库里的数据查出来,并显示在页面上。
一.dao层
在上一篇文章的基础上dao层加入浏览方法。
public List<Course> list(){ String sql="select * from submitcourse"; List<Course> list=new ArrayList<>(); ResultSet rs=null; Course ns=new Course(); try { Connection conn=DBUtil.getConnection(); PreparedStatement pstmt=conn.prepareStatement(sql); // pstmt.setString(1,n.getTeachername()); rs=pstmt.executeQuery(); while(rs.next()) { ns=new Course(rs.getString(1),rs.getString(2),rs.getString(3)); list.add(ns); } //System.out.println(rs); System.out.println(rs); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; }
二.Servlet层
在servlet中实现交互,并修改servlet里面的格式。
package servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import Bean.Course; import dao.daoCourse; /** * Servlet implementation class course_servlet */ @WebServlet("/course_servlet") public class course_servlet extends HttpServlet { private static final long serialVersionUID = 1L; daoCourse gooddao=new daoCourse();//调用dao层的方法 /** * @see HttpServlet#HttpServlet() */ public course_servlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String method=request.getParameter("method"); if("add".equals(method)) { String teachername=request.getParameter("teachername"); String classname=request.getParameter("classname"); String place=request.getParameter("place"); Course n=new Course(classname,teachername,place); gooddao.add(n); request.setAttribute("message", "添加成功"); request.getRequestDispatcher("NewFile2.jsp").forward(request, response); } } else if("looklist".equals(method)) { List<Course> nt= gooddao.list(); request.setAttribute("nt", nt); System.out.println("王建民"); request.getRequestDispatcher("NewFile3.jsp").forward(request, response); } } }
三.写jsp
在此处我们使用jstl和EL来进行数据的获取
NewFile3.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<td>课程名称</td>
<td>教师姓名</td>
<td>教学地点</td>
<td>操作</td>
</tr>
<c:forEach items="${nt}" var="xm">
<tr>
<td>${xm.classname}</td>
<td>${xm.teachername}</td>
<td>${xm.place}</td>
<td><a href="course_servlet?method=delete&teachername=${xm.teachername}" onClick="return confirm('确认删除吗?');">删除</a>
<a href="course_servlet?method=queryByTeachername&teachername=${xm.teachername}">修改</a>
</tr>
</c:forEach>
</table>
</body>
</html>
NewFile2.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <form action="course_servlet?method=looklist" method="post" > <h1>添加成功</h1> <input type="submit" value="浏览"> </form> </body> </html>
四.运行结果:
这写的有点丑,可以做一个菜单。
点击浏览即可浏览出表里面的数据。
下一篇文章为大家写删除功能!!!!!!