javaweb简单的增删改查之“通过姓名查找,查询所有信息”

Posted 亮哥哥哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javaweb简单的增删改查之“通过姓名查找,查询所有信息”相关的知识,希望对你有一定的参考价值。

1.方法
/*
* 查看全部信息 */ public List<Member> selectByName(String name) { // TODO Auto-generated method stub List<Member> list=new ArrayList<Member>(); PreparedStatement pre=null; ResultSet rs=null; try { String sql="select*from blog where name=?"; pre=con.prepareStatement(sql); pre.setString(1, name); rs=pre.executeQuery(); while(rs.next()) { Member user=new Member(); user.setName(rs.getString("name")); user.setAge(rs.getString("age")); list.add(user); } }catch(SQLException e) { e.printStackTrace(); }finally { Databass.Databass.close(rs, pre, con); } return list; } public List<Member> selectall(){ List<Member> list=new ArrayList<Member>(); try { String sql="select * from blog"; PreparedStatement pre=con.prepareStatement(sql); ResultSet rs=pre.executeQuery(); while(rs.next()) { Member user=new Member(); user.setName(rs.getString("name")); user.setAge(rs.getString("age")); list.add(user); } rs.close(); pre.close(); con.close(); }catch(SQLException e) { e.printStackTrace(); } return list; }
通过姓名查找servlet
/**
* Servlet implementation class ServletSearchByname */ @WebServlet("/ServletSearchByname") public class ServletSearchByname extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServletSearchByname() { 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 //设置请求字体 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String name=request.getParameter("name"); Method ww=new Method(); List<Member> list=ww.selectByName(name); request.setAttribute("list", list); request.getRequestDispatcher("/searchByName1.jsp").forward(request, response); } }
查询所有信息servlet
package
Servlet; import java.io.IOException; 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 Member.Member; import Method.Method; /** * Servlet implementation class ServletShow */ @WebServlet("/ServletShow") public class ServletShow extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServletShow() { 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 { //设置请求字体 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); Method sd=new Method(); List<Member> list=sd.selectByName(request.getParameter("name")); request.setAttribute("list", list); request.getRequestDispatcher("/show.jsp").forward(request, response); } }
输入姓名进行查找
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="Method.*,Member.*,Servlet.*,java.util.List" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>通过姓名进行查找</title> </head> <body> <form action="ServletSearchByname" method="post"> 请输入要查询的姓名: <a><input type="text" name="name"></a> <a><input type="submit" value="查询"></a> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page import="Member.*,Method.*,java.util.List,Servlet.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>通过姓名查找</title>
</head>
<body>

<table>
<tr>
            <td>姓名</td>
            <td>年龄</td>
            </tr>
<%
String name=request.getParameter("name");
Method ww=new Method();
List<Member> list=ww.selectByName(name);
request.setAttribute("list", list);
%>
<c:forEach var="u" items="${list}">
                  <tr>
                    
                    <td>${u.name}</td>
                    <td>${u.age }</td>
                    
                </tr>
                </c:forEach>
</table>



</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@page import="Member.*,Method.*,java.util.List,Servlet.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="" >
            <tr>
            <td>姓名:</td>
            <td>年龄:</td>
            </tr>
            
            <%
                    
                    Method sd=new Method();
                    List<Member> list=sd.selectall();
                    request.setAttribute("list", list); %>
            
                  <c:forEach var="u" items="${list}">
                  <tr>
                    
                    <td>${u.name}</td>
                    <td>${u.age }</td>
                    
                </tr>
                  
                 
                  </c:forEach>
            </table>
            <a href="main.jsp">返回主页</a>
            


</body>
</html>

以上是关于javaweb简单的增删改查之“通过姓名查找,查询所有信息”的主要内容,如果未能解决你的问题,请参考以下文章

软件测试人员必掌握的增删改查之简单查询

基于maven+ssm的增删改查之批量删除

基于maven+ssm的增删改查之修改员工信息

基于maven+ssm的增删改查之maven环境的搭建

基于maven+ssm的增删改查之添加员工实现

基于maven+ssm的增删改查之测试相关ssm环境是否成功