0
点赞
收藏
分享

微信扫一扫

JavaWeb(模糊查询&include指令)

洒在心头的阳光 2022-04-02 阅读 61

模糊查询:

界面:

 代码:

<form action="/s5/news/admin.jsp" method="post">
		标题:<input type="text" name="title" />
		<input type="submit" value="查询"/>
	</form>
    <ul class="classlist">
    <%
  		 //接收title
	 	 String title=request.getParameter("title");
	 	 if(title==null){
	 		 title="";//相当于查询全部 
	 	 }
	 	 //破碎重组
	 	 title=new String(title.getBytes("iso-8859-1"),"utf-8");
    
    	//jdbc连接Oracle查询所有的新闻 : id 新闻标题 作者
    	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 tb_fb where ntitle like '%"+title+"%' order by nid desc";
    	//获取执行对象
    	PreparedStatement ps=con.prepareStatement(sql);
    	//获得结果集
    	ResultSet rs=ps.executeQuery();
    	//循环便利
    	while(rs.next()){
    %>
      <li><a href="/s5/news/read.jsp?nid=<%= rs.getInt(1)%>"><%= rs.getString(2) %></a>
      	<span> 作者:<%=rs.getString(3) %>&#160;&#160;&#160;&#160;
       		<a href='/s5/news/update.jsp?nid=<%= rs.getInt(1)%>'>修改</a> &#160;&#160;&#160;&#160; 
       		<a href='/s5/news/dodelete.jsp?nid=<%= rs.getInt(1)%>' onclick='return clickdel()'>删除</a> 
       	</span> 
      </li>
      <%
    	}
    	//关闭资源
    	if(con!=null&&!con.isClosed()){
    		con.close();
    	}
    	if(ps!=null){
    		ps.close();
    	}
    	if(rs!=null){
    		rs.close();
    	}
      %>
      <li class='space'></li>
      <p align="right"> 当前页数:[1/3]&nbsp; <a href="#">下一页</a> <a href="#">末页</a> </p>
    </ul>

效果:

 

include指令

概念:include 指令是以静态方式包含文件,也就是说,被包含文件将原封不动地插入 JSI 文件中,因此,在所包含的文件中不能使用 <html></html>、<body></body> 标记,否则会因为与原有的 JSP 文件有相同标记而产生错误。 另外,因为原文件和被包含文件可以相互访问彼此定义的变量和方法,所以要避免变量和方法在命名上产生冲突,可以用于多个界面

代码块:

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

 foota.jsp界面:

 

 footb.jsp界面:

 完整效果:

这次的分享就到这里了 下次再见 

举报

相关推荐

0 条评论