网页实现数据库的增删改查_通过网页改后台数据-程序员宅基地

技术标签: 开发  

最近在做项目web后台数据增删查改的时候,看到一篇较为详细的经典文章,

所以转载了下来,

文章出处在:https://blog.csdn.net/qq_32539825/article/details/70657340

如果作者认为侵权的告诉我,我立马删。


首先jsp 和Servlet语句基本一样,这里的实现需要用到上篇http://blog.csdn.net/qq_32539825/article/details/70494788里的部分内容

包括UserCoon.java UserDao.java User.java

在这里使用jsp语句也实现了分页内容。

这里写图片描述

1 login.jsp

<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'login.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <form action="servlet/Login" method="post">  
用户名:<input type="text" name="username" value=""><br>  
密 码:<input type="password" name="password" value=""><br>  
用户类型:  
<select name="type">
<option value="管理员">管理员</option>
<option value="普通用户">普通用户</option>
</select><br>  
<input type="submit" value="提交">  
<input type="reset" value="取消">  
</form>  
  </body>
</html>
  1. dologin.jsp
    首先用javabean得到login中的内容,并调用Insert.java插入到数据库
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.UserDao"  %>


<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'dologin.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
  <%request.setCharacterEncoding("utf-8"); %>
<jsp:useBean id="user" class="nuc.test.user.User">
<jsp:setProperty name="user" property="*"/>
</jsp:useBean>
<%
   UserDao usera=new UserDao();
   usera.Insert(user);
 %>

  <body>
</html>

3 queryBena.java
查询数据库中的内容
这里写图片描述

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.UserDao"  %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'queryBean.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <%
        UserDao user=new UserDao();
        User usera=new User();
        ResultSet rst=user.Query();
     %>

     <table border=1>
     <tr><td>用户名</td><td>密码</td><td>用户类型</td><td colspan="2" align="center">数据操作</td></tr>
     <%while(rst.next()) {%>
     <tr><td><%=rst.getString("username") %></td><td><%=rst.getString("password") %></td><td><%=rst.getString("type") %></td><td><a href="deleteBean.jsp?id=<%=rst.getString("id")%>">删除操作</a></td><td><a href="updateBean.jsp?id=<%=rst.getString("id")%>">更新操作</a></td></tr>
     <%} %>
     </table>

</body>
</html>

4 deleteBean.jsp
点击查询页面上的删除键 便会超链接到deleteBean.jsp并带着值

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.UserDao"  %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'deleteBean.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
   <%  
   UserDao user=new UserDao();
    User usera=new User();
    usera.setId(request.getParameter("id"));
    int rest=user.Delete(usera);

    %>
  </body>
</html>

5 updateBean.jsp
首先将需要更新的一条内容输出到网页上,修改后 点提交 调用doUpdateBean.jsp更新

这里写图片描述

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.UserDao"  %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'updateBean.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
  <%
  request.setCharacterEncoding("utf-8");
  UserDao user=new UserDao();
  User usera=new User();
  usera.setId(request.getParameter("id"));
  ResultSet rs=user.Select(usera);
  if(rs.next()){
   %>
   <form action="doUpdateBean.jsp?id=<%=rs.getString("id")%>" method="post">
    用户名:<input type="text" value="<%=rs.getString("username") %>" name="username"><br>
    密 码:<input type="text" value="<%=rs.getString("password") %>" name="password"><br>
    用户类型:<select name="type">
   <option value="管理员">管理员</option>
   <option value="普通用户">普通用户</option>
   </select><br>
   <input type="submit" value="提交">
   <input type="reset" value="取消">
   </form>


%} %>

  </body>
</html>

6 doUpdateBean.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.UserDao"  %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'doUpdateBean.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <%request.setCharacterEncoding("utf-8"); %>
    <jsp:useBean id="use" class="nuc.test.user.User">
    <jsp:setProperty name="use" property="*"/>
    </jsp:useBean>
    <%
    UserDao user=new UserDao();
    //User usera=new User();
    use.setId(request.getParameter("id"));

    int rs=0;
    rs=user.Update(use);
     %>
     <jsp:forward page="queryBean.jsp"/>
  </body>
</html>

7 接下来的下个是用来显示分页的 并且设置每页显示5条信息

Bar.jsp
用来编写分页信息

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.*"  %>
<%@page import="java.util.*"  %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'Bar.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
         <%
   int pages;
   int currpage=1;
   if(request.getParameter("page")!=null){
       currpage=Integer.parseInt(request.getParameter("page"));

   }
   Find find=new Find();
   int count=find.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='showPage.jsp?page="+i+"'>"+i+"</a>");
      }
     sb.append("  ");
   }
   out.print(sb);

   request.setAttribute("bar",sb.toString());

    %>

</body>
</html>

ShowPage.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.*"  %>
<%@page import="java.util.*"  %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'showPage.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
     <table align="center" width="800" border="1">
    <tr>
        <td align="center" colspan="5">
        <h2>所有用户信息</h2>
        </td>
    </tr>
    <tr align="center">
    <td>用户名</td><td>密码</td><td>用户类型</td><td colspan="2">操作方法</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><a href="servlet/Update?id=<%=usera.getId()%>">修改</a></td>
       <td><a href="servlet/Delete?id=<%=usera.getId()%>">删除</a></td>
    </tr>
    <%} %>
    <tr>
    <td align="center" colspan="3">
           <jsp:include page="Bar.jsp"/>
    </td>
    </tr>
    <tr align="center"><td colspan="5"><a href="first.jsp">添加用户</a></td></tr>
    </table>

  </body>
</html>

这里写图片描述


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_39936434/article/details/80159282

智能推荐

乐高打印机robotc_reset_motor_encoder(a)-程序员宅基地

文章浏览阅读1.1k次,点赞3次,收藏12次。乐高打印机robotc_reset_motor_encoder(a)

C语言中的日志输出编程_c语言 错误处理 日志记录-程序员宅基地

文章浏览阅读146次。在上面的示例中,我们首先定义了几个日志级别的常量,包括调试级别(DEBUG)、信息级别(INFO)、警告级别(WARNING)和错误级别(ERROR)。日志是在软件开发和调试过程中非常常见的工具,它可以帮助开发人员追踪程序的执行过程、发现潜在的问题和记录重要的信息。在C语言中,我们可以使用一些技术来实现日志输出功能,以便更好地理解和调试我们的代码。通过使用上述的日志输出编程技术,我们可以在C语言代码中方便地添加日志输出,从而更好地理解和调试代码。运算符,可以在没有额外参数的情况下省略可变参数的逗号。_c语言 错误处理 日志记录

达梦数据库的表空间_达梦数据库表存在那里-程序员宅基地

文章浏览阅读1.2k次。1. 达梦数据库默认表空间1)SYSTEM(系统表空间,用于存放数据字典信息)2)ROLL(回滚表空间,用于存放的回滚数据,支持MVCC(事务多版本))3)TEMP(临时表空间,用于存放是临时数据)4)MAIN(main表空间,如果用户创建数据对象,不指定存储位置,默认存放到main)5)HMAIN(Hmain表空间,用于存放的是huge table的信息)查找数据库中的表空间SQL..._达梦数据库表存在那里

pbootcms模板自动清理runtime缓存-程序员宅基地

文章浏览阅读335次,点赞5次,收藏6次。/ 下一次清理时间。// 初始化清理时间。完成后每天第一个访问你网站就会触发自动清理脚本,如果上次清理时间是一天前(时间可自行设置),就会执行自动清理。打开/apps/home/controller/ExtLabelController.php文件。// 测试扩展单个标签。然后再模板通用文件里面加入。// 自动会话清理脚本。

有感而发:程序员到底要不要阅读框架源码?_学编程 需要看开源代码吗知乎-程序员宅基地

文章浏览阅读1.4k次,点赞12次,收藏20次。最近正在写【高并发专题】的文章,其中,在【高并发专题】中,有不少是分析源码的文章,很多读者留言说阅读源码比较枯燥!问我程序员会使用框架了,会进行CRUD了,是否真的有必要阅读框架源码?!_学编程 需要看开源代码吗知乎

Java基础之int和Integer有什么区别-程序员宅基地

文章浏览阅读10w+次,点赞291次,收藏869次。1 int与Integer的基本使用对比(1)Integer是int的包装类;int是基本数据类型; (2)Integer变量必须实例化后才能使用;int变量不需要; (3)Integer实际是对象的引用,指向此new的Integer对象;int是直接存储数据值 ; (4)Integer的默认值是null;int的默认值是0。2 int与Integer的深入对比(1)由于In..._int和integer

随便推点

联想服务器bios设置u盘启动不了系统,U盘重装系统的时候按F12不能启动,会出现联想拯救者是什么原因?bios设置U盘启动了...-程序员宅基地

文章浏览阅读2.9k次。U盘重装系统的时候按F12不能启动,会出现联想拯救者是什么原因?bios设置U盘启动了以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!U盘重装系统的时候按F12不能启动,会出现联想拯救者是什么原因?bios设置U盘启动了, 按F12不能启动,会出现联想拯救者是什么原因bios设置了的话,就不用再俺F12了 啊。直接就可以..._拯救者u盘不能启动

arcpy定义坐标系_arcpy 定义坐标系-程序员宅基地

文章浏览阅读2.7k次,点赞2次,收藏4次。参考:http://pro.arcgis.com/zh-cn/pro-app/arcpy/classes/spatialreference.htmhttp://desktop.arcgis.com/zh-cn/arcmap/10.3/tools/data-management-toolbox/define-projection.htm创建坐标系输出坐标系定义新的坐标系..._arcpy 定义坐标系

华为数通HCIA学习资料&学习总结_华为官方hcia资料-程序员宅基地

文章浏览阅读3.9k次,点赞3次,收藏15次。数通HCIA资料以及个人的一点总结,可以参考一下文档链接:https://mubu.com/doc/5yFQv99YFG 密码:spus_华为官方hcia资料

推荐开源项目:GCR Cleaner - 管理你的Google Cloud Registry高效又省心-程序员宅基地

文章浏览阅读302次,点赞3次,收藏10次。推荐开源项目:GCR Cleaner - 管理你的Google Cloud Registry高效又省心项目地址:https://gitcode.com/GoogleCloudPlatform/gcr-cleaner项目简介GCR Cleaner 是一个由 Google Cloud Platform 团队开发的工具,用于自动化清理 Google Cloud Registry (GCR) 中无用...

【机器视觉运动控制一体机小课堂】三分钟实现中值滤波去除噪点_labview中值滤波-程序员宅基地

文章浏览阅读1.7k次。背景在实际的机器视觉项目应用当中图像质量效果是视觉处理方案能否准确和稳定运行的关键因素。在遇到存在噪点的图像时,最常用的图像预处理方法就是进行中值滤波。中值滤波去除图像上存在孤立的噪声点有很好的应用效果,它能提高图像的平滑度。 它在去除噪点时,能够保护图像的边缘,使之不被模糊,保留大部分边缘信息。_labview中值滤波

emwin使用自定义字库显示字母及汉字特殊字符_emwin自带字库吗-程序员宅基地

文章浏览阅读6.1k次。基于Segger的emwin进行液晶屏上的图形开发,减低了很多工作量,也非常的好移植,ST系列的单片机上可以免授权使用。 emwin自带了很多字体,有各种大小的数字的字体,可以在GUI.h里面看到定义了的字体。emwin工程里自带了匹配该版本的图片和字体的生成工具。字体的生成工具FontCvtST。 为了节省内存空间,我们只需要关注自己要用到的字符。_emwin自带字库吗

推荐文章

热门文章

相关标签