由于最近接手的項目中大量用到EJB1.0的BMP,數(shù)據(jù)量過大的時候就不得不采取分頁顯示的解決方法。由于對BMP了解并不太久,下面我采用的方法,由于不得不為每個要分頁的BMP加兩個方法感覺也是權(quán)益之計,不知道是否有更好的方法分頁,僅當拋磚引玉吧。

1,在遠程Home接口中加下面兩個方法:

public interface MyTableHome extends EJBHome
{
    
//
    public Collection findByPage(String strCond,int rownumbegin,int rownumend) throws RemoteException, FinderException;
    
public int getCount(String strCond) throws RemoteException;
}

2,在BMP中也加相應(yīng)的方法:

public class MyTableBeanBMP extends MyTableBean
{
    
//
    public Collection ejbFindByPage(String strCond,int rownumbegin,int rownumend) 
    
{
        Connection connection 
= null;
        PreparedStatement statement 
= null;
        
try 
        
{
            connection 
= dataSource.getConnection();
            statement 
= connection.prepareStatement("SELECT  ID from (select t.*, rownum  row_num from MYTABLE t "+strCond+") a  where a.row_num between "+rownumbegin+" and "+rownumend+"");
            ResultSet resultSet 
= statement.executeQuery();
            Vector keys 
= new Vector();
            
while (resultSet.next()) 
            
{
                String cCode 
= resultSet.getString(1);
                keys.addElement(cCode);
            }

            
return keys;
        }

        
catch(SQLException e) 
        
{
            
throw new EJBException("Error executing SQL SELECT  ID from (select t.*, rownum  row_num from MYTABLE t "+strCond+") a  where a.row_num between "+rownumbegin+" and "+rownumend+" : " + e.toString());
        }

        
finally 
        
{
            closeConnection(connection, statement);
        }

    }

    
public int ejbHomeGetCount(String strCond) 
    
{
        Connection connection 
= null;
        PreparedStatement statement 
= null;

        
try 
        
{
            connection 
= dataSource.getConnection();
            statement 
= connection.prepareStatement("SELECT count(*) from MYTABLE "+strCond);
            ResultSet resultSet 
= statement.executeQuery();
            
int count = 1;
            
while (resultSet.next()) 
            
{
                count 
= resultSet.getInt(1);
            }

            
return count;
        }

        
catch(SQLException e) 
        
{
            
throw new EJBException("Error executing SELECT count(*) from MYTABLE "+strCond+" : " + e.toString());
        }

        
finally 
        
{
            closeConnection(connection, statement);
        }

    }

}

3.調(diào)用:

int maxrowinpage = 15;//每頁顯示15條記錄
int Page = 1;
int rowcount = 0
int maxpage = 0
int rownumbegin =1
int rownumend =maxrowinpage; 
String strCond 
=" "//查詢條件



Context context 
= new InitialContext(); 
Object 
ref = context.lookup("MyTable"); 
MyTableHome home 
= (MyTableHome) ref
rowcount 
= home.getCount(strCond); 
maxpage 
= (rowcount + maxrowinpage - 1/ maxrowinpage; 
rownumbegin 
= (Page-1)*maxrowinpage+1
rownumend 
= Page*maxrowinpage; 
Collection c 
= home.findByPage(strCond,rownumbegin,rownumend);