javaWeb06 模糊查询&评论&include指令

一、模糊查询

代码展示:
admin界面


<form action="/web05/news/admin.jsp" method="post">
    <select name="type">
        <option value='nauthor'>作者</option>
        <option value='ntitle'>标题</option>
    </selecte>
    <input type="text" name='cxk' />
<!--     标题:<input type="text" name="title"/> -->
    <input type="submit" value="查询新闻" />
</form>
    <ul class="classlist">
    <%
        request.setCharacterEncoding("utf-8");
        //接收下拉框的值
        String type = request.getParameter("type");
        //out.print(type);
        //接收文本框的值
        String cxk = request.getParameter("cxk");
        //判断
        if(type==null){undefined
            type="ntitle";
        }
        if(cxk==null){undefined
            cxk="";//相当于查询全部
        }
        //破碎重组
        cxk= new String(cxk.getBytes("iso-8859-1"),"utf-8");
        //jdbc连接Oracle查询所有的新闻:新闻标题 新闻作者 新闻的编号nid
        //OracleDriver;
        String CNAME = "oracle.jdbc.driver.OracleDriver";
        String URL = "jdbc:oracle:thin:@localhost:1521:orcl"; 
        //加载驱动
        Class.forName(CNAME);
        //创建连接
        Connection con = DriverManager.getConnection(URL, "scott", "tiger");
        //定义sql语句
        String sql = " select nid,ntitle,nauthor from news280 where "+type+" like '%"+cxk+"%' order by nid desc";
        //out.print(sql);
        //获得执行对象
        PreparedStatement ps = con.prepareStatement(sql);
        //获得结果集
        ResultSet rs = ps.executeQuery();
        //循坏遍历
        while(rs.next()){undefined
            
    %>
      <li> <a href="/web05/news/read.jsp?nid=<%=rs.getInt(1)%>"><%=rs.getString(2) %> </a>
       <span> 作者: <%=rs.getString(3) %>     
      <a href='/web05/news/update.jsp?nid=<%=rs.getInt(1)%>'>修改</a>      
      <a href='/web05/news/dodelete.jsp?nid=<%=rs.getInt(1) %>' οnclick='return clickdel()'>删除</a> 
      </span> 
      </li>
      <%
      
        }
        
        //关闭资源
        if(con!=null&&!con.isClosed()){undefined
            con.close();
        }
        if(ps!=null){undefined
            ps.close();
        }
        if(rs!=null){undefined
            rs.close();
        }

      %>

二、评论

代码展示:

read界面&dopl界面

<script type="text/javascript">
              function check(){undefined
                  var cauthor = document.getElementById("cauthor");
                  var content = document.getElementById("ccontent");
                  if(cauthor.value == ""){undefined
                      alert("用户名不能为空!!");
                      return false;
                  }else if(content.value == ""){undefined
                      alert("评论内容不能为空!!");
                      return false;
                  }
                  return true;
              }
              
              function clickdel(){undefined
                  return confirm("你确定要删除嘛");
              }
          </script>
</head>
<body>
<%
        //接收主页面传过来的值 nid
        String nid = request.getParameter("nid");
        //思路:根据nid 拿到对应的信息=单个查询
        //jdbc连接Oracle查询所有的新闻:新闻标题 新闻作者 新闻的编号id
        //OracleDriver;
        String CNAME = "oracle.jdbc.driver.OracleDriver";
        String URL = "jdbc:oracle:thin:@localhost:1521:orcl"; 
        //加载驱动
        Class.forName(CNAME);
        //创建连接
        Connection con = DriverManager.getConnection(URL, "scott", "tiger");
        //定义SQL语句
        String sql = "select * from news280 where nid="+nid;
        //获得执行对象
        PreparedStatement ps = con.prepareStatement(sql);
        //获得结果集
        ResultSet rs = ps.executeQuery();
        //扩大作用域
        String title = "";
        String author = "";
        String addtime = "";
        String content = "";
        int count = 0;
        //循坏遍历
        if(rs.next()){undefined
            //赋值
            title = rs.getString(3);
            author = rs.getString(4);
            addtime = rs.getString(7);
            content = rs.getString(6);
            count = rs.getInt(8);
        }
        
        //修改点击量
        sql = "update news280 set ncount=nvl(ncount,0)+1 where nid="+nid;
        ps = con.prepareStatement(sql);
        int n = ps.executeUpdate();
        
%>
<div id="header">
  <div id="top_login">
    <label> 登录名 </label>
    <input type="text" id="uname" value="" class="login_input" />
    <label> 密  码 </label>
    <input type="password" id="upwd" value="" class="login_input" />
    <input type="button" class="login_sub" value="登录" οnclick="login()"/>
    <label id="error"> </label>
    <a href="index.html" class="login_link">返回首页</a> <img src="/web05/images/friend_logo.gif" alt="Google" id="friend_logo" /> </div>
  <div id="nav">
    <div id="logo"> <img src="/web05/images/logo.jpg" alt="新闻中国" /> </div>
    <div id="a_b01"> <img src="/web05/images/a_b01.gif" alt="" /> </div>
    <!--mainnav end-->
  </div>
</div>
<div id="container">
  <div class="sidebar">
    <h1> <img src="/web05/images/title_1.gif" alt="国内新闻" />
    <h1> <img src="/web05/images/title_2.gif" alt="国际新闻" /> </h1>
    <h1> <img src="/web05/images/title_3.gif" alt="娱乐新闻" /> </h1>
    <div class="class_type"> <img src="/web05/images/class_type.gif" alt="新闻中心" /> </div>
    <div class="content">
      <ul class="classlist">
        <table width="80%" align="center">
          <tr width="100%">
            <td colspan="2" align="center"><%=title %></td>
          </tr>
          <tr>
            <td colspan="2"><hr />
            </td>
          </tr>
          <tr>
            <td align="center"><%=addtime %></td>
            <td align="left"><%=author %>   点击量:<%=count %></td>
          </tr>
          <tr>
            <td colspan="2" align="center"></td>
          </tr>
          <tr>
            <td colspan="2"> <%=content %> </td>
          </tr>
          <tr>
            <td colspan="2"><hr />
            </td>
          </tr>
        </table>
      </ul>
      <ul class="classlist">
        <table width="80%" align="center">
          <td colspan="6">
         <%
             sql = "select * from pl280 where nid="+nid;//查询该条新闻的评论
             ps=con.prepareStatement(sql);
             rs=ps.executeQuery();
             while(rs.next()){undefined
         %>
              <ul>
                  <li>评论内容:<%=rs.getString(5) %></li>
                  <li>评论人:<%=rs.getString(3) %> 评论时间:<%=rs.getString(6) %>  <a href="dodelpl.jsp?pid=<%=rs.getInt(1)%>&nid=<%=nid%>"  οnclick='return clickdel()'>删除</a></li>
              </ul>
              <br/>
          <%
             }
            //关闭资源
              if(con!=null&&!con.isClosed()){undefined
                  con.close();
              }
              if(ps!=null){undefined
                  ps.close();
              }
              if(rs!=null){undefined
                  rs.close();
              }
          %>
          </td>
          <tr>
            <td colspan="6"><hr />
            </td>
          </tr>
        </table>
      </ul>
      <ul class="classlist">
        <form action="dopl.jsp" method="post" οnsubmit="return check()">
          <table width="80%" align="center">
            <tr>
              <td> 评 论 </td>
            </tr>
            <tr>
              <td> 用户名: </td>
              <td><input id="cauthor" name="cauthor" value="这家伙很懒什么也没留下"/>
                IP:
                <input name="cip" value="127.0.0.1"readonly="readonly"/>
                  <!-- 隐藏域传值 -->
                  <input type="hidden" name="nid" value="<%=nid %>" />
              </td>
            </tr>
            <tr>
              <td colspan="2"><textarea id="ccontent"  name="ccontent" cols="70" rows="10"></textarea>
              </td>
            </tr>
            <td><input name="submit" value="发  表" type="submit"/>
              </td>
          </table>
        </form>
-----------------------------------------------------------------------------------------

<%
    //设置编码方式
    request.setCharacterEncoding("utf-8");
    //接收表单提交过来的值
    String nid=request.getParameter("nid");//标题
    String cip=request.getParameter("cip");
    String cauthor=request.getParameter("cauthor");
    String ccontent=request.getParameter("ccontent");
    String caddtime=new Date().toLocaleString();//取系统当前时间 
    /*主键:唯一且不为空
        1.标识列:触发器+序列 交给你们 
        2.取表主键的最大序号+1 
    */
    String URL="jdbc:oracle:thin:@localhost:1521:orcl";
    String CNAME="oracle.jdbc.driver.OracleDriver";
    Class.forName(CNAME);
    Connection con=DriverManager.getConnection(URL, "scott", "tiger");
    String sql="select nvl(max(pid),0) from pl280";
    PreparedStatement ps=con.prepareStatement(sql);
    ResultSet rs=ps.executeQuery();
    int pid = 0;//扩大作用域
    if(rs.next()){
        pid=rs.getInt(1)+1;//最大序号+1
    }
    //实现增加=插入操作
    sql="insert into pl280(pid,nid,pauthor,pip,pcontent,paddtime) values(?,?,?,?,?,?)";    
    //执行sql语句
    ps=con.prepareStatement(sql);
    //给占位符赋值
    ps.setInt(1, pid);
    ps.setInt(2, Integer.parseInt(nid));
    ps.setString(3, cauthor);
    ps.setString(4, cip);
    ps.setString(5, ccontent);
    ps.setString(6, caddtime);
    
    //获得影响行数
    int n=ps.executeUpdate();
    
    //关闭资源
    if(con!=null&&!con.isClosed()){
        con.close();
    }
    if(ps!=null){
        ps.close();
    }
    if(rs!=null){
        rs.close();
    }
    
    //做判断
    if(n>0){//说明发表成功
        //阅读界面
        response.sendRedirect("/web05/news/read.jsp?nid="+nid);
    }
    else{//说明发表失败
        out.print("<script>alert('发表失败');location.href='read.jsp?nid="+nid+"';</script>");
    }

三、include指令

<body>

<!--  指令包含-->
<%@include file="foota.jsp"%>
<%@include file="footb.jsp"%>

</body>

注意:在跨级别时 不能用绝对路径 要用../相当于底层代码

include的优势:减少代码量

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值