这种东西,网上一堆
http://blog.csdn.net/arielxp/archive/2004/06/27/27913.aspx

解决方案 »

  1.   

    [程序源代码]对于程序源代码,本例唯一与《如何实现Struts 数据库应用程序》中的程序源代码不同之处,是dbListOut.jsp文件。不同之处在于在表格最后多加了一行,这一行用于显示分页字符串,内容如下:<tr><td  align=middle colSpan=4 ><bean:write property='pagestr' name='dbListForm' filter='false' /></td></tr>其它文件完全相同,下页是dbListOut.jsp文件源代码:<%@ page contentType="text/html;charset=GB2312" language="java" %><%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %><%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %><%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %><%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %><html:html><head><title></title><html:base/></head><body bgcolor="white"><table width="98%" border="1" bgColor="#E6F2E7" borderColorDark="#FFFFFF" borderColorLight="#008000" cellPadding="3" cellSpacing="0" ><tr bgcolor='#90C090'><td><span CONTENTEDITABLE=true>序号</span></td><td><span CONTENTEDITABLE=true>姓名</span></td><td><span CONTENTEDITABLE=true>备注</span></td></tr><logic:iterate name='dbList' id='dbList' type='emptyprj.dbList' scope='request'><logic:notEqual name='dbListForm' property='action' value='edit'> <tr><td><bean:write name='dbList' property='序号' /></td><td><bean:write name='dbList' property='姓名' /></td><td><bean:write name='dbList' property='备注' /></td></tr></logic:notEqual> <logic:equal name='dbListForm' property='action' value='edit'> <tr><form  method='post' name='dbListForm' action="/DBApp/dbListAction.do?action=update&search=search&expression=<bean:write name='dbList' property='序号' />"><td><html:text name='dbList' property='序号' /></td><td><html:text name='dbList' property='姓名' /></td><td><html:text name='dbList' property='备注' /></td></tr></logic:equal> </logic:iterate> <tr><td  align=middle colSpan=4 ><bean:write property='pagestr' name='dbListForm' filter='false' /></td></tr></tr></table></body></html:html>  dbListDAO.java文件:  这里定义一个DAO类,用于实现数据加的查找、删除、更新、添加及生成分页字符串等功能。getPagestr(int ipage)函数用于生成分页字符串,int ipage参数用于转递当前页码。findSQL(String sql,int ipage)函数用于查找数据库数据,String sql参数用于转递SQL查询语句字符串,int ipage参数用于转递当前页码。。(省略)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 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");      }    }  } } 
      

  2.   


    dbListAction.java文件:   在<分页></分页>之间的代码是实现数据库分页显示的代码。。(省略)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数据                                  if(action==null)action="find";              if(search==null)search="UNsearch";              if(expression==null)expression="";              expression=toChinese(expression);  //处理中文问题,实现编码转换                            if("delete".equals(action))    //删除记录操作              {                  m_dbListDAO.removeID(expression);              }            ////////////              if("update".equals(action)||"insert".equals(action))    //更新、添加记录操作              {                 dbList m_dbList= new dbList();                      //<update>                    m_dbList.set序号(m_dbListForm.get序号());                    m_dbList.set姓名(m_dbListForm.get姓名());                    m_dbList.set备注(m_dbListForm.get备注());                    //</update>                     if("update".equals(action))    //调用DAO对象更新记录                              m_dbListDAO.update(m_dbList,expression);                     if("insert".equals(action))    //调用DAO对象添加记录                           m_dbListDAO.create(m_dbList);              }                 ///////////////查找数据库                 //<sqlstr>                 String sql="SELECT * FROM dbo.Test";                 //</sqlstr>               ////SQL字符处理                 if("search".equals(search))                 {                     //<search>                        sql+=" where ";                     sql+="序号";                     sql+="=";                     if("NULL".equals(expression)||"".equals(expression))                    {                            sql+=m_dbListForm .get序号();                        }                        else                        {                            sql+=expression;                        }                                                sql+="";                     //</search>                 }              //<分页>            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)         {                 e.printStackTrace();                 throw new RuntimeException("Unable to get connection.");           }         finally        {               try              {               if (con != null)              con.close();               }              catch (SQLException e)              {               throw new RuntimeException(e.getMessage());               }       }     }  public String toChinese(String ss) {    //处理中文问题,实现编码转换    if (ss != null) {      try {        String temp_p = ss;        byte[] temp_t = temp_p.getBytes("ISO8859-1");        ss = new String(temp_t);      }      catch (Exception e) {        System.err.println("toChinese exception:" + e.getMessage());        System.err.println("The String is:" + ss);      }    }    return ss;  } }
      

  3.   

    是用JavaWebStudio做的吧...
    看不清楚.能不能直接发到我的邮箱里啊?
    最好是源文件,,能看到效果的
      

  4.   

    petertangh(水漾蓝冰) :::::::::::::你有<<如何实现Struts 数据库应用程序》中的程序源代码吗?
      

  5.   

    我也要一份[email protected]
      

  6.   

    你按照JavaWebStudio做一遍不就会了?