package com.ninemax.util;
import java.util.ArrayList;
import java.util.List;
import com.tietoenator.trip.jtk.*;
import com.ninemax.database.trip.*;
import com.ninemax.entity.GeneralResult;
/**
* Trip資料庫測試類-模糊檢索-隻顯示相關的段落
* @author han
*/
public class MyTrip {
/**
* 将字元串由ISO8859-1轉位GBK編碼
* @param str 要轉碼的字元串
* @return String
*/
public static String convert(String str) {
try {
byte[] bytesStr = str.getBytes("ISO-8859-1");
return new String(bytesStr, "GBK");
} catch (Exception ex) {
return str;
}
}
* 将字元串由GBK轉為ISO8859-1編碼
* @param str 需要轉碼的字元串
public static String deConvert(String str) {
byte[] bytesStr = str.getBytes("GBK");
return new String(bytesStr, "ISO-8859-1");
* 對TRIP資料庫進行模糊檢索-隻顯示相關的段落
* @param keyword 檢索的關鍵詞
* @return List集合
public List<GeneralResult> getResult(int intPageSize,long intPagex,String keyword){
/*
說明:GeneralReSult是一個封裝類。裡面是一些我們需要的記錄的屬性。
*/
//intPage表示目前頁
int intPage = (int) intPagex;
/**這是模糊查找 */
String strfind ="content="+keyword;
//要連接配接的主機IP位址
String tripIP = "192.168.15.11";
//要連接配接的資料庫的名稱
String tripDB = "mydb";
//TRIP資料庫的登陸者名稱
String tripUserName = "system";
//TRIP資料庫的登陸者密碼
String tripPassword = "trip413";
int FindRecordNumber = 0;
double second1 = 0.0, second2 = 0.0;
//下面為初始化TRIP資料庫的連接配接池
TripPoolManage tpm = TripPoolManage.getInstance();
TripPoolManager TPool = tpm.getTripPool();
if (TripPoolManager.IsExistPool() == false) {
int poolSize =1;//連接配接池最大使用者數
String usernames[]=new String[poolSize];
String passwrods[]=new String[poolSize];
for(int i=0;i<poolSize;i++){
usernames[i]=tripUserName;
passwrods[i]=tripPassword;
}
tpm.createPool( tripDB, tripIP, usernames,passwrods,poolSize,10000,0);
}
TripSession TSession;
Database db = null;
Search srh = null;
Record rec = null;
TField fld = null;
Session trip = null;//SessionManager.session();
TSession = TPool.getConnection("UserPool1", "User1", tripIP);
trip = TSession.getSession();
db = trip.database(tripDB);
db.open();
second1 = System.currentTimeMillis();
//執行CCL中的find指令,即:find jgdm=xxx
trip.find(deConvert(strfind));
second2 = System.currentTimeMillis();
//session對象的getRecordCount()方法:擷取find方法最新搜尋發現的記錄數
FindRecordNumber = trip.getRecordCount();
try{
//Session對象的search(int p)方法:表示建立一個新的搜尋對象,當p=0時,表示最新的搜尋被使用
srh = trip.search(0);
}catch (Exception e){
trip = TSession.getSession();
//session對象的database(String dbNames):建立一個新的資料庫對象
db = trip.database(tripDB);
//打開要通路的資料庫
db.open();
//session對象的getRecordCount()方法:擷取find方法最新搜尋發現的記錄數
FindRecordNumber = trip.getRecordCount();
String strauthor = "";
String strcontent = "";
/*
說明:strauthor , strcontent是trip資料庫mydb中的兩個字段名稱,同時也是我們自己封裝的類GeneralResult的兩個屬性。
不同的資料庫的字段不同。我們所要查的字段也不同,我們需要查那些字段,在此就寫那些字段!!!
int rid = 0;
//k1表示:每一個list集合中的第一個記錄
int k1 = intPageSize * (intPage - 1) + 1;
int x = 0;
if(FindRecordNumber>0){
if (FindRecordNumber - (intPageSize * (intPage - 1)) < intPageSize) {
//當剩下的記錄不足一個intPageSize時,我們要自己設定新的每頁顯示條數
x = FindRecordNumber - (intPageSize * (intPage - 1));
} else {
x = intPageSize;
List<GeneralResult> list = new ArrayList<GeneralResult>();
for (int i = k1; i < (k1 + x); i++) {
GeneralResult result=new GeneralResult();
result.setIndexNo(i);
result.setTotalNum(convert(String.valueOf(FindRecordNumber)));
result.setUseTime(convert(String.valueOf((second2 - second1) / 1000.0)));
rec = srh.record(i);
rid = rec.getRID();
result.setRid(rid);
//author字段
fld = rec.field("author");
strauthor = fld.getValue();
if(strauthor == null) {
strauthor = "";
result.setAuthor(convert(strauthor));
說明:上面的幾行代碼就查出資料庫裡author字段的某個記錄,
并封裝到GeneralReSult類中。
//content字段
fld = rec.field("content");
strcontent = fld.getValue();
//拿到該記錄中該字段中段落數,即:paraNum
int paraNum = fld.getParagraphCount();
//下面要拿到每一個段落的内容
for(int j = 1; j<=paraNum; j++) { //注意:段落也是從1開始的。
String value1 = null;
String value2 = null;
String value = null;
if(j == paraNum) {
//拿到最後一段的内容
value = fld.getValue(paraNum);
} else {
//拿到除最後一段的每一段的内容
value1 = fld.getValue(j);
value2 = fld.getValue(j+1);
value = value1.substring(0,value1.indexOf(value2));
}
//判斷每一段是否包含檢索詞
/**
* 2011.12.28日修改,如果一篇文章中有一個符合要求的段落,
* 那麼我們結束該篇文章段落的循環→節省資源,提高性能!
*/
if(convert(value.trim()).contains(keyword)) {
//如果該段落包含檢索詞,我們将該段落以及相應的作者封裝到GeneralResult5對象中
result.setContent(convert(strcontent));
result.setNum(j);
result.setParagraph(convert(value));
break;
說明:以上的綠色部分,視具體情況而定,此時是我做的一個例子!如果你不需要查相應的段落,就不需要的!! 隻要最後把result對象裝入相應的集合中就行了!
list.add(result);
TPool.closeConnection(TSession);
return list;
} catch (Exception e) {
trip.endSession();
TPool.destroyPool();
TPool.cleanUp();
return new ArrayList<GeneralResult>();
}
}
你在此處做一個main方法,就可以測試上面的代碼了!!我就不做了!真的需要的話,去下載下傳就行了!我上傳過例子!!
————————————————————————————————————————————————————
* 封裝了檢索出來的一些資訊
* @author hanlw
*
public class GeneralResult {
//相應的作者
String author;
//符合要求的段落
String paragraph;
//記錄一下該段落是第幾段
int num;
//整篇的内容
String content;
//記錄号
int rid;
//總的記錄數
String totalNum;
//查詢費時
String useTime;
//起始位置
int indexNo;
//以下是get…set方法。。。。
----------------------------------------------------------------
下面是一個工具類:
public class TripPoolManage
{
private static TripPoolManage pool = new TripPoolManage();
private static int intConnectTimes = 0;
private static int cleanupNumber = 0;
private static TripPoolManager TPool = TripPoolManager.getInstance();
public static TripPoolManage getInstance()
{
return pool;
}
public TripPoolManager getTripPool() {
return TPool;
public void createPool(String tripDB, String tripIP, String[] tripUserName, String[] tripPassword, int connectNumber, int cleanNumber, long refreshTimes)
if (!TripPoolManager.IsExistPool())
{
TPool.setPoolsAmount(1);
TPool.setPoolParam("UserPool1", 1, tripIP, 23457,
tripUserName, tripPassword, "trip628.ini", connectNumber,
connectNumber, 1, 40000L, "16450", "16898", 50000L);
TPool.createTripConnectPool();
cleanupNumber = cleanNumber;
TripPoolManager.setCleanUpTime(refreshTimes);
TripPoolManager.StartCleanUp();
public TripSession getConnection(String username, String ip)
throws IOException
intConnectTimes += 1;
if (intConnectTimes >= cleanupNumber)
TPool.cleanUp();
intConnectTimes = 0;
return TPool.getConnection("UserPool1", username, ip);
public int getConnectTimes() {
return intConnectTimes;
本文轉自韓立偉 51CTO部落格,原文連結:http://blog.51cto.com/hanchaohan/779535,如需轉載請自行聯系原作者