上次九歌分享了绑定以及删除修改查询;这次来分享模糊查询发布评论以及include指令。这几天因为种种原因没有及时更新很不好意思。
代码如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>查找新闻--管理后台</title>
<link href="/Project01/css/admin.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header">
<div id="welcome">欢迎使用新闻管理系统!</div>
<div id="nav">
<div id="logo"><img src="/Project01/images/logo.jpg" alt="新闻中国" /></div>
<div id="a_b01"><img src="/Project01/images/a_b01.gif" alt="" /></div>
</div>
</div>
<div id="admin_bar">
<div id="status">管理员: <%=request.getParameter("aa") %>      <a href="#">退出</a></div>
<div id="channel"> </div>
</div>
<div id="main">
<div id="opt_list">
<ul>
<li><a href="/Project01/news/add.jsp">添加新闻</a></li>
<li><a href="/Project01/news/admin.jsp">编辑新闻</a></li>
<li><a href="/Project01/news/find.jsp">查找新闻</a></li>
<li><a href="/Project01/news/zt.jsp">添加主题</a></li>
<li><a href="/Project01/news/ztadmin.jsp">编辑主题</a></li>
</ul>
</div>
<div id="opt_area">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<form action="/Project01/news/find.jsp" method="post">
<input type="text" name="title"/>
<input type="submit" value="查询"/>
</form>
<ul class="classlist">
<%
//
request.setCharacterEncoding("utf-8");
//接收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 news280 where nauthor like '%"+title+"%' order by nid desc";
//out.print(sql);
//获得执行对象
PreparedStatement ps=con.prepareStatement(sql);
//获得结果集
ResultSet rs=ps.executeQuery();
//循环遍历
while(rs.next()){
%>
<li> <a href="/s4/news/read.jsp?nid=<%=rs.getInt(1)%>"><%=rs.getString(2) %></a>
<span> 作者:<%=rs.getString(3) %>    
<a href='/s4/news/update.jsp?nid=<%=rs.getInt(1)%>'>修改</a>     
<a href='/s4/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] <a href="#">下一页</a> <a href="#">末页</a> </p>
</ul>
</div>
</div>
<div id="site_link"> <a href="#">关于我们</a><spssssan>|</span> <a href="#">Aboue Us</a><span>|</span> <a href="#">联系我们</a><span>|</span> <a href="#">广告服务</a><span>|</span> <a href="#">供稿服务</a><span>|</span> <a href="#">法律声明</a><span>|</span> <a href="#">招聘信息</a><span>|</span> <a href="#">网站地图</a><span>|</span> <a href="#">留言反馈</a> </div>
<%@include file="/../footb.jsp"%>
</body>
</html>
我们实现模糊查询的思路是:得到文本框中的内容,查找对应的数据库中的内容。将文本框的值传到查询的页面(即本页面),将值赋予给提前定义好的变量,拼接到SQL语句中,完成查询。
效果如下:
分割线---------------------------------------------------------------------------------------------------------------------------
先上效果图:
分割线-------------------------------------------------------------------------------------------------------
代码如下:这是界面的代码
<form action="dopl.jsp" method="post" onsubmit="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("/Project01/news/read.jsp?nid="+nid);
}
else{//说明发表失败
out.print("");
}
%>
实现评论的思路:将文本框中的内容提交到传值到数据库的页面,将内容增加到数据库中,但是对应的新闻页面通过隐藏域将对应的新闻的编号传到该页面,这样就能实现对应的评论出现在对应的新闻页面。
代码如下:
分割线------------------------------------------------------------------------------------------------------------------------------
include指令:为了减少代码量,将重复的内容封装到一个界面,然后用include指令将对应的界面包含到要用的界面中。
今天分享了模糊查询等内容,但是项目还不够完善,还有许多的地方有待完善,如果有错误的地方,欢迎指正,谢谢。等这个项目完善后,会进行分享。