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) (省略) } |
如何实现Struts 数据库应用程序
最新推荐文章于 2024-11-15 22:11:31 发布
如何实现Struts 数据库应用程序
2008-07-22 13:19