|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
先说优点,首先和C,C++这些语言比起来,java很简单,去掉指针的java,非常好理解,自动垃圾回收机制也很好,自从JDK1.5推出以后,性能上又有了很大提高。分页
//和sonymusic得两个类放在一个包里
//PageQuery.java
packagepagequery;
importjava.sql.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
importjava.util.*;
importdb.DBConfig;
publicclassPageQuery{
//privateStringsql;
//privateConnectionconn;
//前往以后页号
privateintcurPage;
//前往分页巨细
privateintpageSize;
//前往总页数
privateintpageCount;
//前往以后页的纪录条数
privateintpageRowsCount;
//前往总纪录行数
privateintrowsCountget;
//前往文件路径
privateStringFilePath;
//前往传进参数
privateMapmap=newHashMap();
publicPageQuery()
{
}
/*
publicvoidsetCurPage(intcurPage){
this.curPage=curPage;
}
publicintgetCurPage(){
returncurPage;
}
publicvoidsetPageSize(intpageSize){
this.pageSize=pageSize;
}
publicintgetPageSize(){
returnpageSize;
}
*/
publicintgetPageRowsCount(){
returnpageRowsCount;
}
publicvoidSetQuery(Stringkey,Stringvalue)
{
map.put(key,value);
}
publicvoidSetMap(HttpServletRequestreq){
EnumerationparameterNames=req.getParameterNames();
while(parameterNames.hasMoreElements())
{
Stringname=(String)parameterNames.nextElement();
Stringvalues[]=req.getParameterValues(name);
//for(inti=0;i<values.length;i++){
Stringvalue=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);
}
}
}
publicStringgetSqlQuery(){
if(map.isEmpty())
{
return"";
}
else
{
StringBufferstrb=newStringBuffer("where1=1");
Iteratorimap=map.entrySet().iterator();
while(imap.hasNext())
{strb.append("and");
Map.Entryentry=(Map.Entry)imap.next();
Stringkey=(String)entry.getKey();
try{
intvalue=Integer.parseInt((String)entry.getValue());
strb.append(key);
strb.append("=");
strb.append(value);
strb.append("");
}
catch(NumberFormatExceptione){
e.printStackTrace();
Stringvalue=(String)entry.getValue();
strb.append(key);
strb.append("like%");
strb.append(value);
strb.append("%");
}
}
returnstrb.toString();
}
}
publicResultSetmyQuery(Connectionconn,Stringsql,HttpServletRequestreq)throwsException{
returnmyQuery(conn,sql,req,15);
}
publicResultSetmyQuery(Connectionconn,Stringsql,HttpServletRequestreq,intps)throwsException{
//this.conn=conn;
//this.sql=query;
PreparedStatementpstmt=null;
try{
pstmt=conn.prepareStatement(sql);
ResultSetrset=pstmt.executeQuery();
Pageablers=null;
rs=newPageableResultSet2(rset);//机关一个Pageable
this.rowsCountget=rs.getRowsCount();
try{
curPage=Integer.parseInt(req.getParameter("curpage"));}
catch(Exceptione){curPage=1;
}
try{
pageSize=Integer.parseInt(req.getParameter("pagesize"));
}
catch(Exceptione){
//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(inti=0;i<pageRowsCount;i++){//轮回处置
content.append(rs.getString(1));
content.append("");
content.append(rs.getString(2));
content.append("<br>");
rs.next();
}
*/
returnrs;
}
catch(Exceptione)
{
returnnull;
}
}
publicStringjavascript(){
StringBufferjavascript=newStringBuffer("");
javascript.append("<scriptlanguage=JavaScript>
");
javascript.append("functionviewPage(ipage){
");
javascript.append("document.asdffsdsdffdgfgfgf.curpage.value=ipage;
");
javascript.append("document.asdffsdsdffdgfgfgf.submit();
");
javascript.append("}</script>
");
returnjavascript.toString();
}
publicStringPageLegend(){
StringBufferstrb=newStringBuffer("");
strb.append("<formname=asdffsdsdffdgfgfgfmethod=postaction="+FilePath+">");
strb.append("本页");
strb.append(pageRowsCount);
strb.append("笔记录共"+rowsCountget+"笔记录");
if(pageCount==0||curPage==1){
strb.append("首页");
strb.append("前页");}
else{
strb.append("
<Ahref=javascript:viewPage(1)>首页</A>");
strb.append("
<Ahref=javascript:viewPage("+(curPage-1)+")>前页</A>");}
if(curPage==1||pageCount==curPage){
strb.append("后页");
strb.append("尾页");}
else
{
strb.append("
<Ahref=javascript:viewPage("+(curPage+1)+")>后页</A>");
strb.append("
<Ahref=javascript:viewPage("+(pageCount)+")>尾页</A>");
}
strb.append("页次:"+curPage+"/"+pageCount+"页");
strb.append("
<inputname=pagesizetype=textsize=2value="+pageSize+">"+"条/页");
strb.append("
<selectname=curpage>
");
for(inti=1;i<=pageCount;i++){
//if(i==curPage)
//strb.append("<optionvalue="+i+"selected>"+i+"</option>
");
//else
strb.append("<optionvalue="+i+">"+i+"</option>
");
}
strb.append("</select>");
if(!map.isEmpty()){
Iteratorimap=map.entrySet().iterator();
while(imap.hasNext())
{
Map.Entryentry=(Map.Entry)imap.next();
Stringkey=(String)entry.getKey();
Stringvalue=(String)entry.getValue();
strb.append("<inputtype="hidden"name="");
strb.append(key);
strb.append(""value="");
strb.append(value);
strb.append("">
");
}}
strb.append("页<inputtype=buttonname=Submitvalue=Golanguage=javascriptonclick=viewPage(document.asdffsdsdffdgfgfgf.curpage.value)></form>");
returnstrb.toString();
}
}
//GetParameter.java
packagepagequery;
importjava.sql.*;
importjava.util.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
publicclassGetParameter{
privateMapmap=newHashMap();
publicGetParameter(){};
publicStringgetMap(HttpServletRequestreq){
EnumerationparameterNames=req.getParameterNames();
while(parameterNames.hasMoreElements())
{
Stringname=(String)parameterNames.nextElement();
Stringvalues[]=req.getParameterValues(name);
//for(inti=0;i<values.length;i++){
Stringvalue=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
{
StringBufferstrb=newStringBuffer("where1=1");
Iteratorimap=map.entrySet().iterator();
while(imap.hasNext())
{strb.append("and");
Map.Entryentry=(Map.Entry)imap.next();
Stringkey=(String)entry.getKey();
try{
intvalue=Integer.parseInt((String)entry.getValue());
strb.append(key);
strb.append("=");
strb.append(value);
strb.append("");
}
catch(NumberFormatExceptione){
e.printStackTrace();
Stringvalue=(String)entry.getValue();
strb.append(key);
strb.append("like%");
strb.append(value);
strb.append("%");
}
}
returnstrb.toString();
}
}
}
//news.jsp
<%@includefile="loginforce.jsp"%>
<%@pagecontentType="text/html;charset=gb2312"%>
<%@pageimport="java.sql.*,parameter.*"%>
<jsp:useBeanid="PageQuery"class="pagequery.PageQuery"scope="page"/>
<%--jsp:useBeanid="GetParameter"class="pagequery.GetParameter"scope="page"/--%>
<jsp:useBeanid="dbconfig"class="db.DBConfig"scope="page"/>
<html>
<head>
<title>旧事办理</title>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312">
<linkrel="stylesheet"href="/CSS/gccss.css"type="text/css">
<scriptlanguage="Javascript">
functionCheckAll(curElement)
{
varsTable=curElement.parentElement.parentElement.parentElement.parentElement;
if(curElement.checked){
for(vari=1;i<sTable.rows.length;i++){
sTable.rows(i).children[0].children[0].checked=true;
}
}else{
for(vari=1;i<sTable.rows.length;i++){
sTable.rows(i).children[0].children[0].checked=false;
}
}
}
functionnewBigWindow(url,windowName)
{
window.open(url,windowName,"width=800,height=600,left=100,top=0,scrollbars=yes,resizeable=yes");
}
functionisChecked(){
for(vari=0;i<document.forms[0].elements.length;i++){
vare=document.forms[0].elements[i];
if(e.name==items)
if(e.checked)returntrue;
}
returnfalse;
}
functiondoDel(){
if(isChecked()){
if(confirm("断定删除?")){
document.forms[0].submit();
returntrue;
}
returnfalse;
}
returnfalse;
}
</script>
</head>
<body>
<br>
<center><spanclass=head>旧事办理</span></center>
<br>
<FORMNAME=main_formMETHOD=POSTACTION="newsdelete.jsp">
<tablewidth="95%"border="0"cellspacing="2"cellpadding="2"align="center"class="table_a">
<trclass="title">
<tdwidth="5%"align="center"><inputtype="checkbox"name="checkbox"value="checkbox"onclick="CheckAll(this)"></td>
<tdwidth="5%"align="center">修改</td>
<tdwidth="10%">范例</td>
<tdwidth="50%">题目</td>
<tdwidth="15%">公布日期</td>
<tdwidth="7%">是不是保举</td>
<tdwidth="8%">点击数</td>
<%
//DBConfigdbconfig;
Connectionconn;
//dbconfig=newDBConfig();
conn=dbconfig.GetConnect();
ParameterParserparser=newParameterParser(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="titlelike%"+_title+"%";
}
PageQuery.SetMap(request);
//Stringds=GetParameter.getMap(request);
//Stringds=PageQuery.getSqlQuery();
Stringsql="selectid,types,title,news_date,hot,countfromnewswhere"+_types+"and"+_title+"and"+_hot+"orderbyiddesc";
out.println(sql);
//out.println("<br>");
//StringBuffercontent=newStringBuffer("");
ResultSetrs;
rs=PageQuery.myQuery(conn,sql,request,4);
//PageQuery.SetQuery("types",parser.getStringParameter("types",""));
intpageRowsCount=PageQuery.getPageRowsCount();
intt=0;Stringtt="t2";
for(inti=0;i<pageRowsCount;i++){//轮回处置
t++;
intid=rs.getInt(1);
Stringtypes=rs.getString(2);
Stringtitle=rs.getString(3);
Stringnews_date=rs.getString(4);
inthot=rs.getInt(5);
intcount=rs.getInt(6);
Stringhot_to_str;
switch(hot){
case0:hot_to_str="否";break;
case1: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>");
%>
<trclass="<%=tt%>">
<tdalign="center"><inputtype="checkbox"name="items"value="<%=id%>"></td>
<tdalign="center"><ahref="newsmod.jsp?id=<%=id%>">修正</a></td>
<td><%=types%></td>
<td><ahref="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>
<hrnoshadesize="1">
<tablewidth="95%"border="0"cellspacing="0"cellpadding="0">
<tr>
<td><divalign="right"><%out.println(PageQuery.PageLegend());
out.println(PageQuery.javascript());%>
</div></td>
</tr>
</table>
<%
//out.println(content.toString());
//ResultSetMetaDataresultsMeta=rs.getMetaData();
//intcolumns=resultsMeta.getColumnCount();
//out.println("<br>");
//out.println(columns);
%>
<br>
<divalign="center">
<INPUTTYPE="button"name="createnew"value="增加"onclick="location.href=newsadd.jsp">
<INPUTTYPE="button"name="del"value="删除"onclick="returndoDel();">
</div>
<br>
<br>
<div>
<FORMNAME="main_form"METHOD="post"ACTION="news.jsp">
<tablewidth="60%"border="0"cellspacing="2"cellpadding="2"class="table_a"align="center">
<trclass="title">
<tdcolspan="4">请选择查询前提</td>
</tr>
<tr><tdwith="20"class="t1"><divalign="center">类型</div></td>
<tdclass="t2">
<selectname="types">
<optionvalue="">疏忽</option>
<optionvalue="1">1</option>
</select>
</td>
</tr>
<tr>
<tdwidth="20%"class="t1">
<divalign="center">是不是保举</div>
</td>
<tdclass="t2">
<selectname="hot">
<optionvalue="">疏忽</option>
<optionvalue="1">否</option>
<optionvalue="2">是</option>
</td>
</tr>
</tr>
<tr>
<tdwidth="20%"class="t1">
<divalign="center">标题</div>
</td>
<tdclass="t2">
<inputtype="text"name="title"size=37value="">
</td>
</tr>
</table><br>
<divalign="center"><inputtype="submit"name="query"value="查询"></div>
</FORM>
</div>
</body>
</html>
JAVA学习必须明确这是一项投资,对于大多数的人来说,学习JAVA是为了就业,还有就是刚走向工作位置的朋友想尽快赶上工作的节奏。 |
|