TRIP数据库检索的一个主要方法

简介:
 
  1. package com.ninemax.util; 
  2. import java.util.ArrayList; 
  3. import java.util.List; 
  4. import com.tietoenator.trip.jtk.*; 
  5. import com.ninemax.database.trip.*; 
  6. import com.ninemax.entity.GeneralResult; 
  7. /** 
  8.  * Trip数据库测试类-模糊检索-只显示相关的段落 
  9.  * @author han 
  10.  */ 
  11. public class MyTrip { 
  12.     /** 
  13.      * 将字符串由ISO8859-1转位GBK编码 
  14.      * @param str 要转码的字符串 
  15.      * @return String 
  16.      */ 
  17.     public static String convert(String str) { 
  18.         try { 
  19.             byte[] bytesStr = str.getBytes("ISO-8859-1"); 
  20.             return new String(bytesStr, "GBK"); 
  21.         } catch (Exception ex) { 
  22.             return str; 
  23.         } 
  24.     } 
  25.     /** 
  26.      * 将字符串由GBK转为ISO8859-1编码 
  27.      * @param str 需要转码的字符串 
  28.      * @return String 
  29.      */ 
  30.     public static String deConvert(String str) { 
  31.         try { 
  32.             byte[] bytesStr = str.getBytes("GBK"); 
  33.             return new String(bytesStr, "ISO-8859-1"); 
  34.         } catch (Exception ex) { 
  35.             return str; 
  36.         } 
  37.     } 
  38.  
  39.     /** 
  40.      * 对TRIP数据库进行模糊检索-只显示相关的段落 
  41.      * @param keyword 检索的关键词 
  42.      * @return List集合 
  43.      */ 
  44.     public List<GeneralResult> getResult(int intPageSize,long intPagex,String keyword){ 
  45.     /* 
  46. 说明:GeneralReSult是一个封装类。里面是一些我们需要的记录的属性。  
  47. */ 
  48.  
  49.         //intPage表示当前页 
  50.         int intPage = (int) intPagex;  
  51.         /**这是模糊查找 */ 
  52.         String strfind ="content="+keyword; 
  53.          
  54.         //要连接的主机IP地址 
  55.         String tripIP = "192.168.15.11"
  56.         //要连接的数据库的名称 
  57.         String tripDB = "mydb"
  58.         //TRIP数据库的登陆者名称 
  59.         String tripUserName = "system"
  60.         //TRIP数据库的登陆者密码 
  61.         String tripPassword = "trip413"
  62.          
  63.         int FindRecordNumber = 0
  64.         double second1 = 0.0, second2 = 0.0
  65.          
  66.         //下面为初始化TRIP数据库的连接池 
  67.          TripPoolManage tpm = TripPoolManage.getInstance(); 
  68.             TripPoolManager TPool = tpm.getTripPool(); 
  69.             if (TripPoolManager.IsExistPool() == false) { 
  70.                 int poolSize =1;//连接池最大用户数 
  71.                 String usernames[]=new String[poolSize]; 
  72.                 String passwrods[]=new String[poolSize]; 
  73.                 for(int i=0;i<poolSize;i++){ 
  74.                     usernames[i]=tripUserName; 
  75.                     passwrods[i]=tripPassword; 
  76.                 } 
  77.                 tpm.createPool( tripDB, tripIP, usernames,passwrods,poolSize,10000,0); 
  78.             } 
  79.          
  80.         TripSession TSession; 
  81.         Database db = null
  82.         Search srh = null
  83.         Record rec = null
  84.         TField fld = null
  85.         Session trip = null;//SessionManager.session(); 
  86.  
  87.         TSession = TPool.getConnection("UserPool1""User1", tripIP); 
  88.         try { 
  89.             trip = TSession.getSession(); 
  90.             db = trip.database(tripDB); 
  91.             db.open(); 
  92.             second1 = System.currentTimeMillis(); 
  93.             //执行CCL中的find命令,即:find jgdm=xxx 
  94.             trip.find(deConvert(strfind)); 
  95.             second2 = System.currentTimeMillis(); 
  96.             //session对象的getRecordCount()方法:获取find方法最新搜索发现的记录数 
  97.             FindRecordNumber = trip.getRecordCount(); 
  98.              
  99.             try
  100. //Session对象的search(int p)方法:表示创建一个新的搜索对象,当p=0时,表示最新的搜索被使用 
  101.                 srh = trip.search(0); 
  102.             }catch (Exception e){ 
  103.                 trip = TSession.getSession(); 
  104.                 //session对象的database(String dbNames):创建一个新的数据库对象 
  105.                 db = trip.database(tripDB); 
  106.                 //打开要访问的数据库 
  107.                 db.open(); 
  108.     //session对象的getRecordCount()方法:获取find方法最新搜索发现的记录数 
  109.                 FindRecordNumber = trip.getRecordCount(); 
  110.                 srh = trip.search(0); 
  111.             } 
  112.              
  113.             String strauthor = ""
  114.             String strcontent = ""
  115. /* 
  116. 说明:strauthor  ,  strcontent是trip数据库mydb中的两个字段名称,同时也是我们自己封装的类GeneralResult的两个属性。 
  117.  
  118. 不同的数据库的字段不同。我们所要查的字段也不同,我们需要查那些字段,在此就写那些字段!!! 
  119. */ 
  120.      
  121.             int rid = 0
  122.             //k1表示:每一个list集合中的第一个记录 
  123.             int k1 = intPageSize * (intPage - 1) + 1
  124.             int x = 0
  125.             if(FindRecordNumber>0){ 
  126.                 if (FindRecordNumber - (intPageSize * (intPage - 1)) < intPageSize) { 
  127.             //当剩下的记录不足一个intPageSize时,我们要自己设置新的每页显示条数 
  128.                     x = FindRecordNumber - (intPageSize * (intPage - 1)); 
  129.                 } else { 
  130.                     x = intPageSize; 
  131.                 } 
  132.             } 
  133.              
  134.             List<GeneralResult> list = new ArrayList<GeneralResult>(); 
  135.              
  136.             for (int i = k1; i < (k1 + x); i++) { 
  137.                 GeneralResult result=new GeneralResult();  
  138.                 result.setIndexNo(i); 
  139.                  
  140.                 result.setTotalNum(convert(String.valueOf(FindRecordNumber))); 
  141.                 result.setUseTime(convert(String.valueOf((second2 - second1) / 1000.0))); 
  142.                  
  143.                 rec = srh.record(i); 
  144.                 rid = rec.getRID(); 
  145.                  
  146.                 result.setRid(rid); 
  147.  
  148.                 //author字段 
  149.                 fld = rec.field("author"); 
  150.                 strauthor = fld.getValue(); 
  151.                 if(strauthor == null) { 
  152.                     strauthor = ""
  153.                 } 
  154.         result.setAuthor(convert(strauthor)); 
  155.  
  156. /* 
  157. 说明:上面的几行代码就查出数据库里author字段的某个记录, 
  158. 并封装到GeneralReSult类中。 
  159. */ 
  160.  
  161.                              
  162.                 //content字段 
  163.                 fld = rec.field("content"); 
  164.                 strcontent = fld.getValue(); 
  165.                  
  166.                 //拿到该记录中该字段中段落数,即:paraNum 
  167.                 int paraNum = fld.getParagraphCount(); 
  168.                  
  169.                 //下面要拿到每一个段落的内容 
  170.                 for(int j = 1; j<=paraNum; j++) { //注意:段落也是从1开始的。 
  171.                     String value1 = null
  172.                     String value2 = null
  173.                     String value = null
  174.                      
  175.                     if(j == paraNum) { 
  176.                         //拿到最后一段的内容 
  177.                         value = fld.getValue(paraNum); 
  178.                     } else { 
  179.                         //拿到除最后一段的每一段的内容 
  180.                         value1 = fld.getValue(j); 
  181.                         value2 = fld.getValue(j+1); 
  182.                         value = value1.substring(0,value1.indexOf(value2)); 
  183.                     } 
  184.                      
  185.                     //判断每一段是否包含检索词 
  186.                     /** 
  187.                      * 2011.12.28日修改,如果一篇文章中有一个符合要求的段落, 
  188.                      * 那么我们结束该篇文章段落的循环→节省资源,提高性能! 
  189.                      */ 
  190.                     if(convert(value.trim()).contains(keyword)) { 
  191.                         //如果该段落包含检索词,我们将该段落以及相应的作者封装到GeneralResult5对象中 
  192.                         result.setContent(convert(strcontent)); 
  193.                         result.setNum(j); 
  194.                         result.setParagraph(convert(value)); 
  195.                         break
  196.                     } 
  197.                 } 
  198.          
  199. /* 
  200. 说明:以上的绿色部分,视具体情况而定,此时是我做的一个例子!如果你不需要查相应的段落,就不需要的!!  只要最后把result对象装入相应的集合中就行了! 
  201. */ 
  202.      
  203.                 list.add(result); 
  204.             } 
  205.             TPool.closeConnection(TSession); 
  206.             return list; 
  207.         } catch (Exception e) { 
  208.             TPool.closeConnection(TSession); 
  209.             trip.endSession(); 
  210.             TPool.destroyPool(); 
  211.             TPool.cleanUp(); 
  212.             return new ArrayList<GeneralResult>(); 
  213.         } 
  214.     }    
  215.  
  216. /* 
  217.     你在此处做一个main方法,就可以测试上面的代码了!!我就不做了!真的需要的话,去下载就行了!我上传过例子!! 
  218. */ 
  219.  
  220. ———————————————————————————————————————————————————— 
  221. /** 
  222.  * 封装了检索出来的一些信息 
  223.  * @author hanlw 
  224.  * 
  225.  */ 
  226. public class GeneralResult { 
  227.  
  228.     //相应的作者 
  229.     String author; 
  230.     //符合要求的段落 
  231.     String paragraph; 
  232.     //记录一下该段落是第几段 
  233.     int num; 
  234.     //整篇的内容 
  235.     String  content; 
  236.     //记录号 
  237.     int rid; 
  238.     //总的记录数 
  239.     String totalNum; 
  240.     //查询费时 
  241.     String useTime; 
  242.     //起始位置 
  243.     int indexNo; 
  244.     //以下是get…set方法。。。。 
  245.      
  246.  
  247. ---------------------------------------------------------------- 
  248. 下面是一个工具类: 
  249. public class TripPoolManage 
  250.   private static TripPoolManage pool = new TripPoolManage(); 
  251.   private static int intConnectTimes = 0
  252.   private static int cleanupNumber = 0
  253.   private static TripPoolManager TPool = TripPoolManager.getInstance(); 
  254.  
  255.   public static TripPoolManage getInstance() 
  256.   { 
  257.     return pool; 
  258.   } 
  259.  
  260.   public TripPoolManager getTripPool() { 
  261.     return TPool; 
  262.   } 
  263.  
  264.   public void createPool(String tripDB, String tripIP, String[] tripUserName, String[] tripPassword, int connectNumber, int cleanNumber, long refreshTimes) 
  265.   { 
  266.     if (!TripPoolManager.IsExistPool()) 
  267.     { 
  268.       TPool.setPoolsAmount(1); 
  269.  
  270.       TPool.setPoolParam("UserPool1"1, tripIP, 23457,  
  271.         tripUserName, tripPassword, "trip628.ini", connectNumber,  
  272.         connectNumber, 1, 40000L, "16450""16898", 50000L); 
  273.       TPool.createTripConnectPool(); 
  274.       cleanupNumber = cleanNumber; 
  275.       TripPoolManager.setCleanUpTime(refreshTimes); 
  276.       TripPoolManager.StartCleanUp(); 
  277.     } 
  278.   } 
  279.  
  280.   public TripSession getConnection(String username, String ip) 
  281.     throws IOException 
  282.   { 
  283.     intConnectTimes += 1
  284.  
  285.     if (intConnectTimes >= cleanupNumber) 
  286.     { 
  287.       TPool.cleanUp(); 
  288.       intConnectTimes = 0
  289.     } 
  290.  
  291.     return TPool.getConnection("UserPool1", username, ip); 
  292.   } 
  293.  
  294.   public int getConnectTimes() { 
  295.     return intConnectTimes; 
  296.   } 

 

     本文转自韩立伟 51CTO博客,原文链接:http://blog.51cto.com/hanchaohan/779535,如需转载请自行联系原作者







相关文章
|
1月前
|
SQL 缓存 PHP
PHP技术探究:优化数据库查询效率的实用方法
本文将深入探讨PHP中优化数据库查询效率的实用方法,包括索引优化、SQL语句优化以及缓存机制的应用。通过合理的优化策略和技巧,可以显著提升系统性能,提高用户体验,是PHP开发者不容忽视的重要议题。
|
1月前
|
存储 缓存 NoSQL
利用Redis List实现数据库分页快速查询的有效方法
利用Redis List实现数据库分页快速查询的有效方法
|
3月前
|
设计模式 NoSQL Java
常用的设计模式以及操作Redis、MySQL数据库、各种MQ、数据类型转换的方法
常用的设计模式以及操作Redis、MySQL数据库、各种MQ、数据类型转换的方法
|
1月前
|
SQL 关系型数据库 MySQL
【MySQL】— —熟练掌握用SQL语句实现数据库和基本表的创建。熟练掌握MySQL的安装、客户端登录方法;熟练掌握MySQL的编码、数据类型等基础知识;掌握实体完整性的定义和维护方法、掌握参照完整性
【MySQL】— —熟练掌握用SQL语句实现数据库和基本表的创建。熟练掌握MySQL的安装、客户端登录方法;熟练掌握MySQL的编码、数据类型等基础知识;掌握实体完整性的定义和维护方法、掌握参照完整性
99 1
|
1月前
|
Oracle Java 关系型数据库
java实现遍历树形菜单方法——数据库表的创建
java实现遍历树形菜单方法——数据库表的创建
11 0
|
1月前
|
SQL 关系型数据库 数据库连接
描述一下在 Python 中使用数据库的常见方法。
【2月更文挑战第26天】【2月更文挑战第91篇】描述一下在 Python 中使用数据库的常见方法。
|
1月前
|
存储 机器学习/深度学习 自然语言处理
社区供稿 | Yuan2.0大模型,联合向量数据库和Llama-index,助力检索增强生成技术
本文将以Yuan2.0最新发布的Februa模型为例进行测试验证,用更小规模的模型达到更好的效果。
|
2月前
|
存储 数据库连接 数据库
数据存储与检索:Python 与数据库的交互
在当今的数据驱动世界中,有效地存储和检索数据是至关重要的。Python 作为一种强大的编程语言,提供了多种方式与数据库进行交互,使得处理和管理数据变得更加便捷。在本文中,我们将探讨 Python 与数据库交互的基本概念和技术,包括使用 Python 的数据库驱动程序、连接数据库、执行查询和操作数据等。
|
3月前
|
SQL 关系型数据库 MySQL
数据库查询——索引优化查询方法
数据库查询——索引优化查询方法
33 0
|
4月前
|
NoSQL Java Redis
Redis【应用 01】Java实现动态切换写入不同Redis数据库的方法实例(动态切换Redis数据库)
Redis【应用 01】Java实现动态切换写入不同Redis数据库的方法实例(动态切换Redis数据库)
80 0