如何实现Struts 数据库应用程序(2)

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

[相关链接]

《JavaWebStudio系列开发工具Visaul Struts版本简介》http://dev.csdn.net/develop/article/28/28871.shtm

《如何实现最简单的Struts程序》http://dev.csdn.net/develop/article/28/28946.shtm

《Struts下拉框的实现》http://dev.csdn.net/develop/article/28/28956.shtm

《Sruts单选框的实现》http://dev.csdn.net/develop/article/29/29042.shtm

《Struts复选框的实现》http://dev.csdn.net/develop/article/29/29043.shtm

《Struts程序各文件之间的关系(1)》http://dev.csdn.net/develop/article/29/29131.shtm

《Struts程序各文件之间的关系(2)》http://dev.csdn.net/develop/article/29/29133.shtm

《如何实现Struts 数据库应用程序(1)》http://dev.csdn.net/develop/article/29/29135.shtm

《如何实现Struts 数据库应用程序(2)》

《如何在Struts 数据库应用程序中实现分页显示》

《如何在Struts 数据库应用程序中实现记录的删除、更新及链接》

《如何在Struts 数据库应用程序中实现记录的添加》

《如何建立一个带登陆页面的Struts数据库应用程序》

《如何建立一个带登陆页面及角色的Struts数据库应用程序》

上述例子开发环境:Java Web Studio系列开发环境visual strutst版本。

最新JavaWebStudio下载地址:ftp://210.36.64.79/kui

[url=ftp://210.36.64.79/kui][/url]

[程序源代码]

dbList.jsp文件:

(省略)

dbListOut.jsp文件:

(省略)

dbList.java文件:

(省略)

dbListDAO.java文件:

这里定义一个DAO类,用于实现数据加的查找、删除、更新、添加及生成分页字符串等功能。

package emptyprj;

import emptyprj.dbList;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Collection;

import java.util.ArrayList;

public class dbListDAO {

private Connection con;

private int rowCount;

private int pageCount;

private int length;

private String pagestr;

public int getLength() { return (this.length); }

public void setLength(int length) { this.length=length; }

public String getPagestr(int ipage)

{

String strPage="";

if(getLength()>0)

{

strPage+="共";

strPage+=String.valueOf(rowCount);

strPage+="条记录,共";

strPage+=String.valueOf(pageCount);

strPage+="页,当前是第";

strPage+=String.valueOf(ipage);

strPage+="页, ";

int istart,iend;

istart=ipage-5;

if(istart<0) {istart=0;}

iend=istart+10;

if(iend>pageCount) {iend=pageCount;}

istart=iend-10;

if(istart<0) {istart=0;}

for(int i=istart;i<iend;i++)

{

strPage+="<a href='dbListAction.do?action=find&search=search&page=";

strPage+=String.valueOf(i+1);

strPage+="'>";

strPage+=String.valueOf(i+1);

strPage+="</a>";

strPage+=" ";

}

}

this.pagestr=strPage;

return strPage;

}

public dbListDAO(Connection con) {

this.con = con;

}

(省略)

public Collection findSQL(String sql,int ipage) {

PreparedStatement ps = null;

ResultSet rs = null;

ArrayList list = new ArrayList();

try {

if (con.isClosed()) {

throw new IllegalStateException("error.unexpected");

}

ps = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

rs = ps.executeQuery();

rs.absolute(-1);

rowCount=rs.getRow();

int offset=1;

int pagesize=getLength();

if(getLength()<1)

{

pagesize=rowCount;

pageCount=1;

}

else

{

pageCount=rowCount/getLength()+((rowCount%getLength())>0?1:0);

offset=(ipage-1)*getLength()+1;

if(offset<1)offset=1;

if(offset>rowCount)offset=rowCount;

}

rs.absolute(offset);

for(int i=0;i<pagesize&&offset<rowCount+1;i++,offset++) {

dbList m_dbList= new dbList();

//<find>

m_dbList.set序号(rs.getInt(1));

m_dbList.set姓名(rs.getString(2));

m_dbList.set备注(rs.getString(3));

//</find>

rs.next();

list.add(m_dbList);

}

return list;

} catch (SQLException e) {

//e.printStackTrace();

//throw new RuntimeException("error.unexpected");

return list;

} finally {

try {

if (ps != null)

ps.close();

if (rs != null)

rs.close();

} catch (SQLException e) {

e.printStackTrace();

throw new RuntimeException("error.unexpected");

}

}

}

}

dbListForm.java文件:

(省略)

dbListAction.java文件:

这里定义一个Action类,用于实现页面的转发功能,另外还包括了不同请求类型的不同操作。其中action变量表示操作类型,相当于事件类型。通过这个变量,实现了类似于.NET中的“事件驱动”的功能。

package emptyprj;

(省略)

public final class dbListAction extends Action

{

// 变量定义:

private Log log = LogFactory.getLog("org.apache.struts.webapp.Example");

private ConnectionPool pool;

public dbListAction() {

pool = ConnectionPool.getInstance();

}

//函数定义:

public ActionForward execute(ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

throws Exception

{

// Extract attributes and parameters we will need

Locale locale = getLocale(request);

MessageResources messages = getResources(request);

HttpSession session = request.getSession();

dbListForm m_dbListForm = (dbListForm) form;

Connection con = null;

try

{

con = pool.getConnection();

dbListDAO m_dbListDAO = new dbListDAO(con); //定义DAO对象,用于实现数据库的各种操作

String action=request.getParameter("action"); //动作类型

String search=request.getParameter("search"); //数据库查找方式

String expression=request.getParameter("expression"); //参数,这里用于保存记录(行)的ID数据

///////////////查找数据库

//<sqlstr>

String sql="SELECT * FROM dbo.Test";

//</sqlstr>

(省略)

//<分页>

m_dbListDAO.setLength(10);//设置每页显示记录数

int ipage;//当前页

try

{

String page=request.getParameter("page");//分页参数,读取请求的当前页

ipage=java.lang.Integer.parseInt(page,10);

}

catch (Exception e)

{

ipage=m_dbListForm.getPage();

}

if(ipage<1)ipage=1;

Collection col = m_dbListDAO.findSQL(sql,ipage);//通过DAO对象查询数据

request.setAttribute("dbList",col); //保存数据

String pagestr=m_dbListDAO.getPagestr(ipage);//分页字符串,分页内容

String s_find,str;

if(!"search".equals(search)) //查找和全部显示两个不同的操作,其分页字符串不同,在此进行处理。如果是全部显示则去掉"search=search&"

{

s_find="search=search&";

while(pagestr.indexOf(s_find)!=-1)

{

str=pagestr.substring(0,pagestr.indexOf(s_find));

str+=pagestr.substring(pagestr.indexOf(s_find)+s_find.length(),pagestr.length());

pagestr=str;

}

}

m_dbListForm.setPagestr(pagestr);

m_dbListForm.setAction(action);

request.setAttribute("dbListForm",m_dbListForm);

//</分页>

return mapping.findForward("success");

}

catch (SQLException e)

(省略)

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航