servlet之数据库的增,删,改,查

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/QQ736238785/article/details/70038277

查询:

修改:


删除:


添加:


代码:

frame.jsp(显示左右界面)

<frameset cols="40%,70%">
<frame src="framemenu.jsp" name="menu">
<frame src="framemain.jsp" name="main">
</frameset>
<body>
</body>
framemenu.jsp

<body>
<a href="../servlet/FindPage" target="main">用户管理</a><br>
<a href="../servlet/servletInsert.jsp" target="main">添加用户</a><br>
<a href="../successful.jsp" target="main">新闻管理</a>    
</body>
FindPage.java(构造分页)

public class FindPage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FindPage() {
        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
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		int pages;//定义总页数
	       int currpage=1;//定义当前页码
	       if(request.getParameter("page")!=null){
	            currpage=Integer.parseInt(request.getParameter("page"));
	         }
	       Find fd=new Find();
	       int count=fd.SelectCount();
	       if(count%User.PAGESIZE==0){
	                 pages=count/User.PAGESIZE;
	          }else{
	                pages=count/User.PAGESIZE+1;
	          }
	         StringBuffer sb=new StringBuffer();
	       for(int i=1;i<=pages;i++){
	             if(i==currpage){
		               sb.append("["+i+"]");
	         }else{
		                sb.append("<a href='../servlet/servletPage.jsp?page="+i+"'>"+i+"</a>"); //构建分页条
	          }
	                 sb.append("  ");
	           }
	            request.setAttribute("bar", sb.toString());
	            request.getRequestDispatcher("servletPage.jsp").forward(request,response);
	}
}
servletPage.jsp

<body>
<table align="center" width="800" border="1">
<tr>
   <td align="center" colspan="5">
    <h2>所有用户信息</h2>
   </td>
</tr>
<tr align="center">
     <td><b>用户名</b></td>
     <td><b>密码</b></td>
     <td><b>用户类型</b></td>
     <td colspan="2"><b>数据操作</b></td>
</tr>
    <%
      int currpage=1;//定义当前页码
      if(request.getParameter("page")!=null){
         currpage=Integer.parseInt(request.getParameter("page"));
      }
      Find find=new Find();
      List<User> listall=new ArrayList<User>();
      listall=find.Selectcontent(currpage);
      Iterator<User> it=listall.iterator();
      while(it.hasNext()){
    	  User usera=it.next();
     %>
<tr align="center">
  <td><%=usera.getUsername()%></td>
  <td><%=usera.getPassword()%></td>
  <td><%=usera.getType()%></td>
  <td><button><a href="Update?id=<%=usera.getId()%>">修改</a></button></td>
  <td><button><a href="Delete?id=<%=usera.getId()%>">删除</a></button></td>
</tr>
 <%} %>
 <tr>
  <td align="center" colspan="5"> 
    <%=request.getAttribute("bar") %>
    
  </tr>
</table>
</body>
Delete.java

public class Delete extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Delete() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
	     response.setContentType("text/html;charset=utf-8");
		User user=new User();
		user.setId(request.getParameter("id"));
		UserDao update=new UserDao();
		int result=update.Delete(user);
		 PrintWriter out =response.getWriter();
		if(result!=0){
			out.println("删除成功");
		response.sendRedirect("FindPage");
		}
	}

}
Insert.java

public class Insert extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Insert() {
        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
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}
*/
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		User user=new User();
	     request.setCharacterEncoding("utf-8");
	     response.setContentType("text/html;charset=utf-8");
	     user.setUsername(request.getParameter("username"));
	     user.setPassword(request.getParameter("password"));
	     user.setType(request.getParameter("type"));
		UserDao add=new UserDao();
		int result=add.Insert(user);
		if(result!=0){
			 PrintWriter out =response.getWriter();
			 out.println("添加成功!");
		}
	}

}
Update.java

public class Update extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Update() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		doGet(request, response);
		request.setCharacterEncoding("utf-8");
	     response.setContentType("text/html;charset=utf-8");
		User user=new User();
		user.setId(request.getParameter("id"));
		UserDao update=new UserDao();
		ResultSet result=update.Select(user);
		try {
			if(result.next()){
			 PrintWriter out =response.getWriter();
			 out.println("<form action='doUpdate?id="+result.getString("id")+"' method='post'>");
			 out.println("用户名:<input type='text' value='"+result.getString("username")+"' name='username'>");
			 out.println("密 码:<input type='password' value='"+result.getString("password")+"' name='password'>");
			 out.println("用户类型:<select name='type'>");
			 out.println("<option value='管理员'>管理员</option>");
			 out.println("<option value='普通用户'>普通用户</option>");
			 out.println("</select>");
			 out.println("<input type='submit' value='修改'>");
			 out.println("<input type='reset' value='取消'>");
			 out.println("</form>");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
doUpdate.java

public class doUpdate extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public doUpdate() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		 request.setCharacterEncoding("utf-8");
	     response.setContentType("text/html;charset=utf-8");
		  UserDao user=new UserDao();//实例化
		   User  usera=new User();
		   User use=new User();
		   PrintWriter out =response.getWriter();
		   usera.setId(request.getParameter("id"));
		   usera.setUsername(request.getParameter("username"));
		   usera.setPassword(request.getParameter("password"));
		   usera.setType(request.getParameter("type"));
		   use.setId(usera.getId());
		   use.setUsername(usera.getUsername());
		   use.setPassword(usera.getPassword());
		   use.setType(usera.getType());
		   int rstu=user.Update(use);
		   response.sendRedirect("FindPage");
		 
	}
}
web.xml

<servlet>
    <servlet-name>Login</servlet-name>
    <servlet-class>nuc.test.servlet.Login</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>FindPage</servlet-name>
    <servlet-class>nuc.test.servlet.FindPage</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>Insert</servlet-name>
    <servlet-class>nuc.test.servlet.Insert</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>Update</servlet-name>
    <servlet-class>nuc.test.servlet.Update</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>Delete</servlet-name>
    <servlet-class>nuc.test.servlet.Delete</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>FindAll</servlet-name>
    <servlet-class>nuc.test.servlet.FindAll</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>doUpdate</servlet-name>
    <servlet-class>nuc.test.servlet.doUpdate</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/servlet/Login</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Update</servlet-name>
    <url-pattern>/servlet/Update</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Delete</servlet-name>
    <url-pattern>/servlet/Delete</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>FindAll</servlet-name>
    <url-pattern>/servlet/FindAll</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>doUpdate</servlet-name>
    <url-pattern>/servlet/doUpdate</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Insert</servlet-name>
    <url-pattern>/servlet/Insert</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>FindPage</servlet-name>
    <url-pattern>/servlet/FindPage</url-pattern>
  </servlet-mapping>










阅读更多
换一批

没有更多推荐了,返回首页