首页 > 其他分享 >2023.3.8每日总结

2023.3.8每日总结

时间:2023-03-08 21:15:10浏览次数:59  
标签:总结 HttpServlet name rs list 每日 request 2023.3 response

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>
 

标签:总结,HttpServlet,name,rs,list,每日,request,2023.3,response
From: https://www.cnblogs.com/fuchuchu/p/17196265.html

相关文章

  • 3月8号总结
    1.Error与Exception的区别Error是程序无法处理的错误,比如OutOfMemoryError、ThreadDeath等。这些异常发生时,Java虚拟机(JVM)一般会选择线程终止。此类异常是程序的致命异......
  • 2023.3.6
    >数理方程与特殊函数  “为了下面的表述简单起见,我现在是采用一个算符的形式,来写这些方程。而一旦采用算符的形式的话,下面写起来就比较简单,而且就把这三种类型全......
  • 今日总结
    今天继续联系了javaweb的增删改查<%@pagelanguage="java"contentType="text/html;charset=utf-8"pageEncoding="utf-8"%><!DOCTYPEhtmlPUBLIC"-//W3C//......
  • Git简单总结
    0x01Git理解分布式版本控制器:在个人电脑,云端都有着所有的代码。版本控制,可自由回滚,向前向后。git记录的是快照,不是整个代码的备份;每个快照之间通过指针指向来记录。g......
  • 2023.3.7Android开发
    今天学习了Android开发的图像显示imageview中的图片属性xml中的缩放类型,fitxy拉伸图片使其正好填满试图(图片可被拉伸变形)firststrat保持宽高比例,拉伸图片使其位于试图的......
  • 2023.3.8 闲话
    膜拜国际特级大师SMTwy膜拜国际特级大师SMTwy膜拜国际特级大师SMTwy膜拜国际特级大师SMTwy膜拜国际特级大师SMTwy推歌:ダーリン(Darling)-MARETU.约定素数......
  • 3.8每日总结13
    今天利用两个小时的时间去解决昨天Androidstudio连不上mysql的问题,虽然还是没有解决掉,但是从中学习了很多知识。第一就是导入.jar包后要进行系统的识别不然粘贴上去......
  • 排序算法总结
    1.冒泡排序原理:数组元素两两比较,交换位置,大元素往后放,经过一轮比较后,最大的元素就会出现在最大索引处(nums[].length-1-i)。Java代码:publicclassSort01{//......
  • 每日随笔2023/3/8
    今天学习了As中的显隐式Intent 三种构建方式:在Intent的构造函数中指定调用意图对象的setClass方法指定调用意图对象的setComponent方法指定(1)Intentintent=new......
  • ssh 远程连接方式总结
    ssh远程连接方式总结嵌入式视觉于2023-03-0619:42:13发布335收藏1分类专栏:软件安装文章标签:sshlinux服务器版权软件安装专栏收录该内容7篇文章2订阅订阅......