Webページはデータベースの削除と検索を実現します

44691 ワード

最近、プロジェクトのwebバックグラウンドのデータの削除をして変更する時、1篇の比較的に詳しい経典の文章を見て、
だから転載して、
出典は次のとおりです.https://blog.csdn.net/qq_32539825/article/details/70657340
もし作者が権利侵害を私に教えてくれたら、私はすぐに削除します.
まずjspとサーブレット文は基本的に同じで,ここでの実装には前編を用いる必要がある.http://blog.csdn.net/qq_32539825/article/details/70494788の内容
UserCoonも含めてjava UserDao.java User.java
ここではjsp文を用いてページングも実現した.
这里写图片描述
1 login.jsp
<html>
  <head>
    <base href="">

    <title>My JSP 'login.jsp' starting pagetitle>

    <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">
    

  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>
  • dologin.jspはまずjavabeanでloginの内容を得てInsertを呼び出す.JAvaデータベース
  • への挿入
    "java" import="java.util.*" pageEncoding="ISO-8859-1"%>
    "java.sql.*"%>
    "nuc.test.user.User" %>
    "nuc.test.Dao.UserDao"  %>
    
    
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    
    <html>
      <head>
        <base href="">
    
        <title>My JSP 'dologin.jsp' starting pagetitle>
    
        <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">
        
    
      head>
    
      <body>
      request.setCharacterEncoding("utf-8"); %>
    <jsp:useBean id="user" class="nuc.test.user.User">
    <jsp:setProperty name="user" property="*"/>
    jsp:useBean>
    new UserDao();
       usera.Insert(user);
     %>
    
      <body>
    html>

    3 queryBena.JAvaクエリーデータベースの内容这里写图片描述
    "java" import="java.util.*" pageEncoding="utf-8"%>
    "java.sql.*"%>
    "nuc.test.user.User" %>
    "nuc.test.Dao.UserDao"  %>
    
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    
    <html>
      <head>
        <base href="">
    
        <title>My JSP 'queryBean.jsp' starting pagetitle>
    
        <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">
        
    
      head>
    
      <body>
        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>"username") %>td><td>"password") %>td><td>"type") %>td><td><a href="deleteBean.jsp?id=id")%>">    a>td><td><a href="updateBean.jsp?id=id")%>">    a>td>tr>
         
         table>
    
    body>
    html>

    4 deleteBean.jspクエリーページの削除キーをクリックするとdeleteBeanにハイパーリンクします.jspは値を持っています
    "java" import="java.util.*" pageEncoding="ISO-8859-1"%>
    "java.sql.*"%>
    "nuc.test.user.User" %>
    "nuc.test.Dao.UserDao"  %>
    
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    
    <html>
      <head>
        <base href="">
    
        <title>My JSP 'deleteBean.jsp' starting pagetitle>
    
        <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">
        
    
      head>
    
      <body>
       new UserDao();
        User usera=new User();
        usera.setId(request.getParameter("id"));
        int rest=user.Delete(usera);
    
        %>
      body>
    html>

    5 updateBean.jspはまず更新すべき1つの内容をウェブページに出力、修正後にdoUpdateBeanを呼び出す.jsp更新
    这里写图片描述
    "java" import="java.util.*" pageEncoding="utf-8"%>
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    "java.sql.*"%>
    "nuc.test.user.User" %>
    "nuc.test.Dao.UserDao"  %>
    
    <html>
      <head>
        <base href="">
    
        <title>My JSP 'updateBean.jsp' starting pagetitle>
    
        <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">
        
    
      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=id")%>" method="post">
           :<input type="text" value="username") %>" name="username"><br>
           :<input type="text" value="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
    "java" import="java.util.*" pageEncoding="ISO-8859-1"%>
    "java.sql.*"%>
    "nuc.test.user.User" %>
    "nuc.test.Dao.UserDao"  %>
    
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    
    <html>
      <head>
        <base href="">
    
        <title>My JSP 'doUpdateBean.jsp' starting pagetitle>
    
        <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">
        
    
      head>
    
      <body>
        request.setCharacterEncoding("utf-8"); %>
        <jsp:useBean id="use" class="nuc.test.user.User">
        <jsp:setProperty name="use" property="*"/>
        jsp:useBean>
        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はページング情報を記述するために使用される
    "java" import="java.util.*" pageEncoding="ISO-8859-1"%>
    "java.sql.*"%>
    "nuc.test.user.User" %>
    "nuc.test.Dao.*"  %>
    "java.util.*"  %>
    
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    
    <html>
      <head>
        <base href="">
    
        <title>My JSP 'Bar.jsp' starting pagetitle>
    
        <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">
        
    
      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(""+i+"");
          }
         sb.append("  ");
       }
       out.print(sb);
    
       request.setAttribute("bar",sb.toString());
    
        %>
    
    body>
    html>

    ShowPage.jsp
    "java" import="java.util.*" pageEncoding="utf-8"%>
    "java.sql.*"%>
    "nuc.test.user.User" %>
    "nuc.test.Dao.*"  %>
    "java.util.*"  %>
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    
    <html>
      <head>
        <base href="">
    
        <title>My JSP 'showPage.jsp' starting pagetitle>
    
        <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">
        
    
      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 listall=new ArrayList();
            listall=find.Selectcontent(currpage);
            Iterator it=listall.iterator();
            while(it.hasNext()){
                   User usera=it.next();
    
         %>
        <tr align="center">
           <td>td>
           <td>td>
           <td>td>
           <td><a href="servlet/Update?id=">  a>td>
           <td><a href="servlet/Delete?id=">  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>

    这里写图片描述