【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-viewThingListAction.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
package action;
import java.sql.Connection;
import java.sql.ResultSet;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
/**
* @author dengziyun
* 登录的Action类
*/
public class viewThingListAction extends Action{
/**
* 在此方法中处理HTTP请求,并作响应
*/
public ActionForward execute(ActionMapping arg0, ActionForm arg1,
HttpServletRequest request, HttpServletResponse reponse) throws Exception {
int diPage=1;//当前页码
int pageSize=10;//每页显示的记录条数
int allRecordCount=0;//记录总条数
int pageRecordCount=0;//当前页记录条数
int pageCount=0;//总页数
ActionErrors errors=new ActionErrors();
String diPageStr=request.getParameter("diPage");
//------没有传来当前页码,则默认为1------
if(diPageStr==null||diPageStr.trim().length()<1)
diPage=1;
//------防止传来的数据有误,默认为1------
try{
diPage=Integer.parseInt(request.getParameter("diPage"));
}catch(Exception e){
diPage=1;
}
//------查询出数据,将查询结果数据集写入到response中------
try{
//------得到数据库连接------
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
Object obj = (Object) ctx.lookup("jdbc/sqlserver");
DataSource ds = (javax.sql.DataSource)obj;
Connection conn = ds.getConnection();
//------查询数据------
//------构造查询的SQL语句------
//sqlStr1为查询到当前页数据的SQL语句
//sqlStr2为查询到总记录条数的SQL语句
String sqlStr1="select top "+diPage*pageSize+" * from thing where "+
"thingId not in (select top "+(diPage-1)*pageSize+" thingId from"+
" thing order by thingId desc) order by thingId desc";
String sqlStr2="select count(*) as allCount from thing";
//------得到总页数、总记录条数与当前页记录条数------
java.sql.Statement sql1=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
java.sql.ResultSet rs1=null;
rs1=sql1.executeQuery(sqlStr2);
if(rs1!=null){
//---得到总页数、总记录条数与当前页记录条数---
rs1.next();
allRecordCount=rs1.getInt("allCount");
if(allRecordCount%pageSize==0)
pageCount=allRecordCount/pageSize;
else
pageCount=(int)(allRecordCount/pageSize)+1;
if(diPage==pageCount||pageCount==0)//最后一页或没有数据
pageRecordCount=allRecordCount-(diPage-1)*pageSize;
else
pageRecordCount=pageSize;
}
//------得到当前面数据------
java.sql.ResultSet rs2=null;
java.sql.Statement sql2=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs2=sql2.executeQuery(sqlStr1);
Object[][] rsArray=new Object[pageRecordCount][4];
int i=0;
while(rs2.next()){
rsArray[i][0]=rs2.getString("thingId");
rsArray[i][1]=rs2.getString("thingTitle");
rsArray[i][2]=rs2.getString("thingContent");
rsArray[i][3]=rs2.getString("thingAddTime");
i++;
}
//------将所有数据写入到request------
request.setAttribute("diPage",new Integer(diPage));
request.setAttribute("pageSize",new Integer(pageSize));
request.setAttribute("allRecordCount",new Integer(allRecordCount));
request.setAttribute("pageRecordCount",new Integer(pageRecordCount));
request.setAttribute("pageCount",new Integer(pageCount));
request.setAttribute("rsArray",rsArray);
//------关闭数据库连接------
if(conn!=null)
conn.close();
}catch(Exception e){
e.printStackTrace();
errors.add("DBOPError",new ActionMessage("DBOPError"));
saveErrors(request,errors);
}
return arg0.findForward("viewThingListJSP");
}
}
上一篇:
viewThingAction.java
下一篇:
关于大学英语教学