这是我目前赶工的项目的一小段代码,主要是处理分页。因为数据库是用
ms access,所以受到不了不少限制,不管了,将就着用吧:)如果你要做
这方面的东西,这个可以做参考。
package cs3214;
public class searchBean extends odbcBean
{
/*    private String keywords;
    private String colName;
    private String tableName;
*/    
    public searchBean()
    {
        super();
    }    
/*    public void setKeywords(String s)
    {
        keywords = s;
    }    
    public String getKeywords()
    {
        return keywords;
    }    
    public void setColName(String s)
    {
        colName = s;
    }    
    public String getColName()
    {
        return colName;
    }    
    public void setTableName(String s)
    {
        tableName = s;
    }    
    public String getTableName()
    {
        return tableName;
    }    
*/    public String search(String keywords, String colName, String tableName, int currentPage, int pageSize) throws Exception
    {
        int allRecs = 0;
        int cursorPos = 1;
        String table= "";
        String unique = "";
        String author = ", author";
        boolean hasAuthor = true;        
        String sqlstr = "select count(callNumber) as A from "+tableName+" where "+colName+" like '%"+keywords+"%'";
        OpenConn();
        result = getResults(sqlstr);    
        if (result.next())
        {
        allRecs = result.getInt("A");
        if (allRecs == 0)
        {
            table = table+"
";
            table = table+" | No Records Found! | 
";
            table = table+"
";
            CloseStmt();
            CloseConn();
            return table;
        }
        }
        else
        {
            table = table+"
";
            table = table+" | No Records Found! | 
";
            table = table+"
";
            CloseStmt();
            CloseConn();
            return table;
        }                   
        cursorPos = (currentPage-1)*pageSize+1;
        if (cursorPos >= allRecs)
            cursorPos = (allRecs<=pageSize)?1:(allRecs - pageSize);        
        if (tableName.equals("boundPeriodical") || tableName.equals("newspaper") || tableName.equals("periodical"))
        {
            author = "";
            hasAuthor = false;
        }
        sqlstr = "select ID, title, callNumber "+author+" from "+tableName+" where "+colName+" like '%"+keywords+"%' order by title";
        result = getResults(sqlstr);
        moveCursor(cursorPos);        
        table = table+"
";
        table = table+"| No."+ | 
                      "Title"+ | 
                      "Call No."+ | 
                      "Author"+ | 
                      "Del | 
";
        int i = 0, id = 0;; 
        do 
        {
            id = result.getInt("ID");
            table = table + "| "+(cursorPos+i)+""+ | 
                            ""+result.getString("title")+""+ | 
                            ""+result.getString("callNumber")+""+ | 
                            ""+(hasAuthor?result.getString("author"):"No Author")+""+ | 
                            " | 
";
            i++;
        }
        while (i < pageSize && i < allRecs && result.next());
        
        String bottom = "";
        int allPages = allRecs/pageSize+1;
        String queryString = "keywords="+keywords+"&colName="+colName+"&tableName="+tableName+"&pageSize="+pageSize;
        if (currentPage != 1)
        {
            bottom = bottom+"First:::Previous:::";
        }
        
        if (currentPage != allPages)
        {
            bottom = bottom+"Next:::Last";
        }
        if (bottom != "")
            table = table+"| "+bottom+" | 
";
        bottom = "";
        if (allPages>1)
        {
            for (i = 1; i <= allPages; i++)
                bottom = bottom+""+i+" ";
            table = table+"| "+bottom+" | 
";            
        }
        table = table+"
";
        CloseStmt();
        CloseConn();
        return table;        
    }
}