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

1.方法
/*
* 查看全部信息 */ public List<Member> selectByNameString 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.prepareStatementsql); pre.setString1, name); rs=pre.executeQuery); whilers.next)) { Member user=new Member); user.setNamers.getString"name")); user.setAgers.getString"age")); list.adduser); } }catchSQLException e) { e.printStackTrace); }finally { Databass.Databass.closers, pre, con); } return list; } public List<Member> selectall){ List<Member> list=new ArrayList<Member>); try { String sql="select * from blog"; PreparedStatement pre=con.prepareStatementsql); ResultSet rs=pre.executeQuery); whilers.next)) { Member user=new Member); user.setNamers.getString"name")); user.setAgers.getString"age")); list.adduser); } rs.close); pre.close); con.close); }catchSQLException 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#doGetHttpServletRequest request, HttpServletResponse response) */ protected void doGetHttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter).append"Served at: ").appendrequest.getContextPath)); } /** * @see HttpServlet#doPostHttpServletRequest request, HttpServletResponse response) */ protected void doPostHttpServletRequest 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.selectByNamename); request.setAttribute"list", list); request.getRequestDispatcher"/searchByName1.jsp").forwardrequest, 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#doGetHttpServletRequest request, HttpServletResponse response) */ protected void doGetHttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter).append"Served at: ").appendrequest.getContextPath)); } /** * @see HttpServlet#doPostHttpServletRequest request, HttpServletResponse response) */ protected void doPostHttpServletRequest 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.selectByNamerequest.getParameter"name")); request.setAttribute"list", list); request.getRequestDispatcher"/show.jsp").forwardrequest, 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.selectByNamename);
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>

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注