首先建立數據庫控件canton.jcx
這裡主要用到該控件的getAllBCanton
/**
* @jc:sql command-type="grid"
* rowset-name="B_CANTONRowSet"
* max-rows="1000"
* statement="SELECT CANTON_ID,PLACENAME,ZIP,PRE_PHONE,REMARK,DELETED,CANTON_LEVEL,UPPER_CANTON_ID FROM HAPPYTREE.HT_B_CANTON {sql: filter.getWhereClause ()} {sql: filter.getOrderByClause ()}"
*/
public RowSet getAllBCanton(DatabaseFilter filter)
throws SQLException;
可以看見上面方法的參數是DatabaseFilter類型,我們的主要任務就是傳遞一個參數到該方法中,方法返回RowSet類型的數據
構建一個jpf頁面流
在該頁面流中有一個jsp頁面負責輸入查詢的條件select.jsp
代碼如下:
新建 Web 應用程序頁
Canton_id:
有一個負責顯示查詢結果的頁面grid.jsp
代碼為:
select頁面表單的action為select,這樣在頁面流中的方法select如下:
/**
* @jpf:action
* @jpf:forward name="success" path="grid.jsp"
*/
protected Forward select(DatabaseForm form)
{
sortFilterService = SortFilterService.getInstance(getRequest());
//實例化一個SortFilterService
DatabaseFilter filter = new DatabaseFilter();
//創建一個DatabaseFilter對象filter
List list = new ArrayList();//用於存儲查詢條件FilterTerm
DatabaseFilter.FilterTerm query = new DatabaseFilter.FilterTerm();
query.sColumnName = "canton_id";//查詢的列為caton_id,canton_id數據庫中的一個字段
query.op = DatabaseFilter.opGreater;//比較條件是大於
//這裡還有大於、小於、不等於、為空、不為空等條件,根據情況具體設定
//query.value = "000000";//比較的值
query.value = form.getCanton_id();
list.add(query);//比較條件加入
DatabaseFilter.FilterTerm[] term = new DatabaseFilter.FilterTerm[list.size()];
//設定FilterTerm把比較的內容,即where後面的條件
for(int i=0;i term[i] = (DatabaseFilter.FilterTerm)list.get(i);//把條件加入terms
}
DatabaseFilter.SortTerm[] sterm = new DatabaseFilter.SortTerm[1];//設定排序字段
sterm[0] = new DatabaseFilter.SortTerm();
sterm[0].sColumnName = "placename";//排序字段
sterm[0].op = DatabaseFilter.opDesc;//降序排列
filter = new DatabaseFilter(term,sterm);//構造databasefilter
try{
allRows = myControl.getAllBCanton(filter);//執行查詢語句,並付給allRows
}catch(Exception e){
e.printStackTrace();
}
return new Forward("success",new DatabaseForm());
}
至此就ok了,這裡只是一個簡單的例子,多條件查詢只要往list內加入條件就行了.