收藏模块

收藏模块功能的实现

1.在个人登陆状态下,在热门关注里选择喜欢的问题,点击收藏便可收藏该问题

代码:

 <body>
  
        <% 
          String topic=new String(request.getParameter("topic").getBytes("ISO-8859-1"),"UTF-8");
          String author=new String(request.getParameter("author").getBytes("ISO-8859-1"),"UTF-8"); 
          String classify=new String(request.getParameter("classify").getBytes("ISO-8859-1"),"UTF-8");             
          String userName =(String)session.getAttribute("userName"); 
          if(userName==null) {
                String msg="请先登陆账户!";
                int type=JOptionPane.YES_NO_CANCEL_OPTION;
                String title="信息提示";
                JOptionPane.showMessageDialog(null, msg, title, type);
                response.sendRedirect("hotquestion.jsp");
            }else {      
          Connection con=null;
          Statement stmt=null;
          Class.forName("com.mysql.jdbc.Driver");
          String url="jdbc:mysql://localhost:3306/learning?useUnicode=true&characterEncoding=gbk";
          con=DriverManager.getConnection(url,"root","root");
          stmt=con.createStatement();
          String sql0="INSERT INTO `shoucang` VALUES ('"+classify+"', '"+author+"', '"+topic+"','"+userName+"');";
          stmt.executeUpdate(sql0);
          response.sendRedirect("hotquestion.jsp");            
          stmt.close();
          con.close();
          }
          %>
  </body>

  2.在我的收藏里可以查看个人收藏

收藏模块_第1张图片

 <body>
  
        <%
          String userName =(String)session.getAttribute("userName");
            if(userName==null) {
                String msg="请先登陆账户!";
                int type=JOptionPane.YES_NO_CANCEL_OPTION;
                String title="信息提示";
                JOptionPane.showMessageDialog(null, msg, title, type);
                response.sendRedirect("tishi.jsp");
            }else {
          Connection con=null;
          Statement stmt=null;
          ResultSet rs=null;
          String author;
          String topic;
          int i=0;
          Class.forName("com.mysql.jdbc.Driver");
          String url="jdbc:mysql://localhost:3306/learning?useUnicode=true&characterEncoding=gbk";
          con=DriverManager.getConnection(url,"root","root");
          stmt=con.createStatement();
          String query="select distinct * from `shoucang` where userName='"+userName+"'";
          rs=stmt.executeQuery(query);
          %>
    <table align="center" width="100%" border="1">
        <tr background="#22dcff" >
            <th>编号</th>
            <th>类型</th>
            <th>主题</th>
            <th>操作</th>
        </tr>
          <%
          while(rs.next()){  
          author=rs.getString("author");
          topic=rs.getString("topic"); 
          i++; 
           %>
          <tr height="40" >
              <td align="center"><%=i %></td>
              <td align="center"><%=rs.getString("classify") %></td>
              <td align="center"><a href="dealing/response.jsp?author=<%=author%>&topic=<%=topic%>"><%=topic%></a></td>
              <td align="center"><a href="dealing/deleteshouc.jsp?author=<%=author%>&topic=<%=topic%>">删除</a></td>
          </tr>
          <%
            }
            }
           %>
     </table>
  </body>

  3.个人收藏删除

收藏模块_第2张图片

代码:

      <body>
           <%   String topic=new String(request.getParameter("topic").getBytes("ISO-8859-1"),"UTF-8");
                String author=new String(request.getParameter("author").getBytes("ISO-8859-1"),"UTF-8");      
                Connection con=null;
                Statement stmt=null;
                Class.forName("com.mysql.jdbc.Driver");
                /*3306为端口号,student为数据库名,url后面加的?useUnicode=true&characterEncoding=gbk,是为了处理向数据库中添加数据时出现乱码的问题。*/
                String url="jdbc:mysql://localhost:3306/learning?useUnicode=true&characterEncoding=gbk";
                con=DriverManager.getConnection(url,"root","root");
                stmt=con.createStatement();
                String sql2="DELETE FROM `shoucang` WHERE author='"+author+"' AND topic='"+topic+"'";
                stmt.executeUpdate(sql2);
                String msg="删除成功!";
                int type=JOptionPane.YES_NO_CANCEL_OPTION;
                String title="信息提示";
                JOptionPane.showMessageDialog(null, msg, title, type);
                response.sendRedirect("mysave.jsp");
                stmt.close();
                con.close();
            %>
    </body>

  

你可能感兴趣的:(模块)