通过上几次的博客,我们大致完成了管理员的功能,现在是用户的功能,用户点击后到用户信息页面
给本页面绑定数据库,查询到数据库中新闻表的数据
用户信息页面数据代码
<div class="main">
<div class="class_type"> <img src="images/class_type.gif" alt="新闻中心" /> </div>
<div class="content">
<ul class="class_date">
<li id='class_month'>
<%
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection con = DriverManager.getConnection(url, "scott", "tiger");
//查询主题
PreparedStatement ps = con.prepareStatement("select * from subject");
ResultSet rs = ps.executeQuery();
while(rs.next()){
%>
<a href='index.jsp?tid=<%=rs.getInt(1)%>'><b> <%=rs.getString(2) %> </b></a>
<%} %>
</li>
</ul>
<ul class="classlist">
<%
//接收新闻主题
String id = request.getParameter("tid");
String countSql = "select count(*) from news";
String pageSql = "select * from (select a.*,rownum mid from news a)b where mid>=? and mid<=?";
int tid = 1;
if(id!=null){
//要执行 根据主题查询 的 分页
tid = Integer.valueOf(id);
countSql = "select count(*) from news where tid="+tid;//根据主题编号从新闻表中获取相关主题新闻的总条数
//根据主题编号查到相关主题的新闻然后进行分页查询
pageSql = "select * from (select a.*,rownum mid from news a where tid="+tid+")b where mid>=? and mid<=?";
}
//查询新闻总条数
ps = con.prepareStatement(countSql);
rs = ps.executeQuery();
int count = 0;
if(rs.next()){
count = rs.getInt(1);
}
//获取页面
String index = request.getParameter("pageIndex");
int pageIndex = 1;
if(index!=null){
pageIndex = Integer.valueOf(index);
}
//查询新闻
int pageSize = 5;
//求出最大页码
int maxPage = count/pageSize;
if(count%pageSize != 0){
maxPage++;
}
int start = (pageIndex-1)*pageSize+1;
int end = pageIndex*pageSize;
ps = con.prepareStatement(pageSql);
ps.setInt(1, start);
ps.setInt(2, end);
rs = ps.executeQuery();
while(rs.next()){
%>
<li>
<a href='newspages/news_add.html'><%=rs.getString("ntitle") %> </a>
<span> <%=rs.getDate("ndate") %> </span>
</li>
<li class='space'></li>
<%} %>
<p align="right">
<a href="index.jsp<%
if(id!=null){
out.print("?tid="+tid);
}
%>">首页</a>
<a href="index.jsp?pageIndex=<%=pageIndex>1?pageIndex-1:1%><%
if(id!=null){
out.print("&tid="+tid);
}
%>">上一页</a>
当前页数:[<%=pageIndex %>/<%=maxPage %>]
<a href="index.jsp?pageIndex=<%=pageIndex<maxPage?pageIndex+1:maxPage%><%
if(id!=null){
out.print("&tid="+tid);
}
%>">下一页</a>
<a href="index.jsp?pageIndex=<%=maxPage%><%
if(id!=null){
out.print("&tid="+tid);
}
%>">末页</a>
</p>
</ul>
</div>