//和sonymusic得两个类放在一个包里
//PageQuery.java
package pagequery;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import db.DBConfig;
public class PageQuery {
//private String sql;
//private Connection conn;
//返回当前页号
private int curPage;
//返回分页大小
private int pageSize;
//返回总页数
private int pageCount;
//返回当前页的记录条数
private int pageRowsCount;
//返回总记录行数
private int rowsCountget;
//返回文件路径
private String FilePath;
//返回传入参数
private Map map=new HashMap();
public PageQuery()
{
}
/*
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public int getCurPage() {
return curPage;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageSize() {
return pageSize;
}
*/
public int getPageRowsCount() {
return pageRowsCount;
}
public void SetQuery(String key,String value)
{
map.put(key,value);
}
public void SetMap(HttpServletRequest req){
Enumeration parameterNames=req.getParameterNames();
while (parameterNames.hasMoreElements())
{
String name=(String) parameterNames.nextElement();
String values[]=req.getParameterValues(name);
//for(int i=0;i<values.length;i++){
String value=values[0];
if (name.equals("curpage")||name.equals("pagesize")||name.equals("Submit")||name.equals("submit")||value==null||value.length()==0)
{}
else{
map.put(name,value);
}
}
}
public String getSqlQuery(){
if (map.isEmpty())
{
return "";
}
else
{
StringBuffer strb = new StringBuffer(" where 1=1 ");
Iterator imap=map.entrySet().iterator();
while(imap.hasNext())
{strb.append( "and " );
Map.Entry entry=(Map.Entry)imap.next();
String key=(String)entry.getKey();
try {
int value=Integer.parseInt((String)entry.getValue());
strb.append( key );
strb.append( " =" );
strb.append( value );
strb.append( " " );
}
catch (NumberFormatException e){
e.printStackTrace();
String value=(String)entry.getValue();
strb.append( key );
strb.append( " like'%" );
strb.append( value );
strb.append( "%' " );
}
}
return strb.toString();
}
}
public ResultSet myQuery(Connection conn,String sql, HttpServletRequest req) throws Exception {
return myQuery( conn, sql, req,15);
}
public ResultSet myQuery(Connection conn,String sql, HttpServletRequest req ,int ps) throws Exception {
//this.conn=conn;
//this.sql=query;
PreparedStatement pstmt=null;
try {
pstmt = conn.prepareStatement(sql);
ResultSet rset = pstmt.executeQuery();
Pageable rs=null;
rs=new PageableResultSet2(rset);//构造一个Pageable
this.rowsCountget=rs.getRowsCount();
try{
curPage=Integer.parseInt(req.getParameter("curpage"));}
catch (Exception e) { curPage=1;
}
try{
pageSize=Integer.parseInt(req.getParameter("pagesize"));
}
catch (Exception e) {
// e.printStackTrace();
pageSize=ps;
}
pageSize=pageSize>rowsCountget?rowsCountget:pageSize;
rs.setPageSize(pageSize);//每页N个记录
rs.gotoPage(curPage);//跳转到第N页
this.pageCount=rs.getPageCount();
this.pageRowsCount=rs.getPageRowsCount();
this.FilePath=req.getRequestURI();
/*
for(int i=0; i<pageRowsCount; i++){//循环处理
content.append(rs.getString(1));
content.append(" ");
content.append(rs.getString(2));
content.append("<br>");
rs.next();
}
*/
return rs;
}
catch (Exception e)
{
return null;
}
}
public String javascript(){
StringBuffer javascript = new StringBuffer("");
javascript.append(" <script language=JavaScript>\n");
javascript.append("function viewPage(ipage){\n");
javascript.append("document.asdffsdsdffdgfgfgf.curpage.value=ipage;\n");
javascript.append("document.asdffsdsdffdgfgfgf.submit();\n");
javascript.append(" } </script>\n");
return javascript.toString();
}
public String PageLegend(){
StringBuffer strb = new StringBuffer("");
strb.append("<form name=asdffsdsdffdgfgfgf method=post action="+FilePath+">");
strb.append( "本页");
strb.append(pageRowsCount);
strb.append( "条记录 共" + rowsCountget + "条记录 ");
if(pageCount==0||curPage==1){
strb.append( " 首页 ");
strb.append( " 前页 ");}
else {
strb.append( "\n<A href=javascript:viewPage(1)>首页</A> ");
strb.append( " \n<A href=javascript:viewPage(" + (curPage-1)+")>前页</A> ");}
if(curPage==1||pageCount==curPage){
strb.append( " 后页 ");
strb.append( " 尾页 ");}
else
{
strb.append( " \n<A href=javascript:viewPage(" + (curPage+1)+")>后页</A> ");
strb.append( " \n<A href=javascript:viewPage(" + (pageCount)+")>尾页</A>");
}
strb.append( " 页次:" + curPage + "/" + pageCount + "页 ");
strb.append( "\n<input name=pagesize type=text size=2 value="+pageSize+">" + "条/页 " );
strb.append("\n<select name=curpage>\n");
for (int i=1;i<=pageCount;i++){
//if (i==curPage)
// strb.append("<option value="+i+" selected>"+i+"</option>\n");
// else
strb.append("<option value="+i+">"+i+"</option>\n");
}
strb.append("</select>");
if (!map.isEmpty()){
Iterator imap=map.entrySet().iterator();
while(imap.hasNext())
{
Map.Entry entry=(Map.Entry)imap.next();
String key=(String)entry.getKey();
String value=(String)entry.getValue();
strb.append("<input type=\"hidden\" name=\"");
strb.append(key);
strb.append("\" value=\"");
strb.append(value);
strb.append("\">\n");
}}
strb.append("页 <input type=button name=Submit value=Go language=javascript onclick=viewPage(document.asdffsdsdffdgfgfgf.curpage.value)></form>");
return strb.toString();
}
}
//GetParameter.java
package pagequery;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetParameter {
private Map map=new HashMap();
public GetParameter(){};
public String getMap(HttpServletRequest req){
Enumeration parameterNames=req.getParameterNames();
while (parameterNames.hasMoreElements())
{
String name=(String) parameterNames.nextElement();
String values[]=req.getParameterValues(name);
//for(int i=0;i<values.length;i++){
String value=values[0];
if (name.equals("curpage")||name.equals("pagesize")||name.equals("Submit")||name.equals("submit")||value==null||value.length()==0)
{}
else{
map.put(name,value);
}
// }
}
if (map.isEmpty())
{
return "";
}
else
{
StringBuffer strb = new StringBuffer(" where 1=1 ");
Iterator imap=map.entrySet().iterator();
while(imap.hasNext())
{strb.append( "and " );
Map.Entry entry=(Map.Entry)imap.next();
String key=(String)entry.getKey();
try {
int value=Integer.parseInt((String)entry.getValue());
strb.append( key );
strb.append( " =" );
strb.append( value );
strb.append( " " );
}
catch (NumberFormatException e){
e.printStackTrace();
String value=(String)entry.getValue();
strb.append( key );
strb.append( " like'%" );
strb.append( value );
strb.append( "%' " );
}
}
return strb.toString();
}
}
}
//news.jsp
<%@include file="loginforce.jsp"%>
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.sql.*,parameter.*" %>
<jsp:useBean id="PageQuery" class="pagequery.PageQuery" scope="page"/>
<%--jsp:useBean id="GetParameter" class="pagequery.GetParameter" scope="page"/--%>
<jsp:useBean id="dbconfig" class="db.DBConfig" scope="page"/>
<html>
<head>
<title>新闻管理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="/css/gccss.css" type="text/css">
<script language="Javascript">
function CheckAll(curElement)
{
var sTable = curElement.parentElement.parentElement.parentElement.parentElement;
if(curElement.checked){
for(var i=1;i<sTable.rows.length;i++){
sTable.rows(i).children[0].children[0].checked=true;
}
}else{
for(var i=1;i<sTable.rows.length;i++){
sTable.rows(i).children[0].children[0].checked=false;
}
}
}
function newBigWindow(url, windowName)
{
window.open(url, windowName, "width=800,height=600,left=100,top=0,scrollbars=yes,resizeable=yes");
}
function isChecked(){
for (var i=0;i<document.forms[0].elements.length;i++){
var e = document.forms[0].elements[i];
if (e.name == 'items')
if(e.checked) return true;
}
return false;
}
function doDel(){
if(isChecked()){
if(confirm("确定删除?")){
document.forms[0].submit();
return true;
}
return false;
}
return false;
}
</script>
</head>
<body>
<br>
<center><span class=head>新闻管理</span></center>
<br>
<FORM NAME=main_form METHOD=POST ACTION="newsdelete.jsp">
<table width="95%" border="0" cellspacing="2" cellpadding="2" align="center" class="table_a">
<tr class="title">
<td width="5%" align="center"><input type="checkbox" name="checkbox" value="checkbox" onclick="CheckAll(this)"></td>
<td width="5%" align="center">修 改</td>
<td width="10%">类型</td>
<td width="50%">标题</td>
<td width="15%">发布日期</td>
<td width="7%">是否推荐</td>
<td width="8%">点击数</td>
<%
// DBConfig dbconfig;
Connection conn;
//dbconfig=new DBConfig();
conn=dbconfig.GetConnect();
ParameterParser parser = new ParameterParser(request);
String _types=parser.getStringParameter("types","");
if (_types.length()==0)
{
_types="1=1";
}
else
{
_types=" types ="+_types ;
}
String _hot=parser.getStringParameter("hot","");
if (_hot.length()==0)
{
_hot="1=1";
}
else
{
_hot=" hot ="+_hot ;
}
String _title=parser.getStringParameter("title","");
if (_title.length()==0)
{
_title="1=1";
}
else
{
_title=" title like '%"+_title+"%'" ;
}
PageQuery.SetMap(request);
//String ds=GetParameter.getMap(request);
//String ds=PageQuery.getSqlQuery();
String sql="select id,types,title,news_date,hot,count from news where "+_types+" and "+_title+" and " +_hot+ " order by id desc";
out.println(sql);
//out.println("<br>");
//StringBuffer content = new StringBuffer("");
ResultSet rs;
rs=PageQuery.myQuery(conn,sql,request,4);
//PageQuery.SetQuery("types",parser.getStringParameter("types", ""));
int pageRowsCount=PageQuery.getPageRowsCount();
int t=0;String tt="t2";
for(int i=0; i<pageRowsCount; i++){//循环处理
t++;
int id=rs.getInt(1);
String types=rs.getString(2);
String title=rs.getString(3);
String news_date=rs.getString(4);
int hot=rs.getInt(5);
int count=rs.getInt(6);
String hot_to_str;
switch (hot){
case 0:hot_to_str="否"; break;
case 1:hot_to_str="是"; break;
default:hot_to_str="否";}
tt=t%2==0?"t2":"t1";
//content.append(rs.getString(1));
//content.append(" ");
//content.append(rs.getString(2));
//content.append("<br>");
%>
<tr class="<%=tt%>">
<td align="center"><input type="checkbox" name="items" value="<%=id%>"></td>
<td align="center"><a href="newsmod.jsp?id=<%=id%>">修改</a></td>
<td><%=types%></td>
<td><a href="javascript:newBigWindow('newsshow.jsp?id=<%=id%>','_blank')" title="查看详细"><%=title%></a></td>
<td><%=news_date%></td>
<td><%=hot_to_str%></td>
<td><%=count%></td>
</tr>
<%
rs.next();
}
conn.close();
%>
</table></FORM>
<hr noshade size="1">
<table width="95%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td> <div align="right"><%out.println(PageQuery.PageLegend());
out.println(PageQuery.javascript());%>
</div></td>
</tr>
</table>
<%
//out.println(content.toString());
//ResultSetMetaData resultsMeta=rs.getMetaData();
//int columns=resultsMeta.getColumnCount();
// out.println("<br>");
//out.println(columns);
%>
<br>
<div align="center">
<INPUT TYPE="button" name="createnew" value="增 加" onclick="location.href='newsadd.jsp'">
<INPUT TYPE="button" name="del" value="删 除" onclick="return doDel();">
</div>
<br>
<br>
<div>
<FORM NAME="main_form" METHOD="post" ACTION="news.jsp">
<table width="60%" border="0" cellspacing="2" cellpadding="2" class="table_a" align="center">
<tr class="title">
<td colspan="4"> 请选择查询条件</td>
</tr>
<tr><td with="20" class="t1"><div align="center">类 型</div></td>
<td class="t2">
<select name="types">
<option value="">忽略</option>
<option value="1">1</option>
</select>
</td>
</tr>
<tr>
<td width="20%" class="t1">
<div align="center">是否推荐</div>
</td>
<td class="t2">
<select name="hot">
<option value="">忽略</option>
<option value="1">否</option>
<option value="2">是</option>
</td>
</tr>
</tr>
<tr>
<td width="20%" class="t1">
<div align="center">标 题</div>
</td>
<td class="t2">
<input type="text" name="title" size=37 value="">
</td>
</tr>
</table><br>
<div align="center"><input type="submit" name="query" value="查询"></div>
</FORM>
</div>
</body>
</html>
|