TRIP数据库的JDBC ,代码如下:
欢迎关注本人公众号进行交流:
GetKnowledge+
package com.dareway.seftrain.trip.compare;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.tietoenator.trip.jxp.TdbException;
import com.tietoenator.trip.jxp.TdbFieldType;
import com.tietoenator.trip.jxp.TdbLanguage;
import com.tietoenator.trip.jxp.control.TdbDatabaseList;
import com.tietoenator.trip.jxp.data.TdbComponent;
import com.tietoenator.trip.jxp.data.TdbField;
import com.tietoenator.trip.jxp.data.TdbPhraseField;
import com.tietoenator.trip.jxp.data.TdbRecord;
import com.tietoenator.trip.jxp.data.TdbRecordSet;
import com.tietoenator.trip.jxp.data.TdbTextField;
import com.tietoenator.trip.jxp.database.TdbDatabaseDesign;
import com.tietoenator.trip.jxp.database.TdbFieldDesign;
import com.tietoenator.trip.jxp.pool.TdbConnectionPool;
import com.tietoenator.trip.jxp.pool.TdbPooledSession;
import com.tietoenator.trip.jxp.pool.TdbTripNetConnectionPool;
import com.tietoenator.trip.jxp.session.TdbSession;
/**
* TRIP数据库操作工具类
* @author Leraing_LB
* @version V1.2.0
* @最新修改时间 2016-12-17
*/
public class TripUtil {
static TdbPooledSession session = null;
static TdbConnectionPool pool = null;
public static TdbConnectionPool Login(String ip,String port,String username,String pw) throws TdbException{
pool=new TdbTripNetConnectionPool("127.0.0.1",
23457, "system","z", TdbLanguage.English, 5);
pool.setIdleTimeout(10);
pool.setPurgeInterval(2);
pool.setMinIdle(10);
return pool;
}
//创建数据库,并添加字段
/**
* @param name :数据库名字
* @param fName :字段名
* @param fType :对应字段类型
* @throws TdbException
* */
public static void creatDB(String name,String fName [],String fType []) throws TdbException{
try {
//创建数据库
session=Login(null, null, null, null).acquire();
TdbDatabaseDesign db=new TdbDatabaseDesign(session);
db.setBafFile(name+".BAF");
db.setBifFile(name+".BIF");
db.setVifFile(name+".VIF");
//添加字段
TdbFieldDesign field=new TdbFieldDesign();
for (int i = 0; i < fType.length; i++) {
field.setName(fName[i]);
field.setType(fType[i]);
db.addField(field);
}
db.put(name);
System.out.println("创建数据库成功!");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("创建数据库失败!");
}finally{
session.logout();
pool.close();
}
}
//插入记录
/**
* @param dbName :数据库名字
* @param fNa :字段名
* @param val :对应字段值
* @throws TdbException
* */
public static int insert(String dbName,String [] fNa,String [] val ) throws TdbException{
try {
session=Login(null, null, null, null).acquire();
TdbDatabaseDesign db=new TdbDatabaseDesign(session);
db.get(dbName);
TdbRecord rec=new TdbRecord(session, db, false);
TdbComponent c=rec.getHead();
for (int i = 0; i < val.length; i++) {
TdbPhraseField naField= (TdbPhraseField) c.createField(fNa[i],c.getField(fNa[i]).getType());
naField.appendValue(val[i]);
}
rec.commit();
db.index();
System.out.println("添加记录成功!");
return 1;
} catch (TdbException e) {
System.out.println("添加记录失败!");
return 0;
}finally{
session.logout();
pool.close();
}
}
//获取结果
/**
* @param dbName :数据库名字
* @param tem :字段名
* @throws TdbException
* */
public static List<Map<String,Object>> getResult(String dbName,String [] tem) throws TdbException{
List<Map<String,Object>> list=new ArrayList<Map<String,Object> >();
try {
TdbRecordSet rset=new TdbRecordSet(session);
rset.setDatabase(dbName);
rset.setQuerySQL(true);
rset.setFrom(1);
TdbRecord template=new TdbRecord(session);
for (int i = 0; i < tem.length; i++) {
template.addToTemplate(tem[i]);
}
rset.setRetrievalTemplate(template);
rset.get();
for (TdbRecord r : rset.records()) {
Map<String,Object> map=new HashMap<String,Object>();
map.put("id", r.getRecordId());
for (TdbField field : r.getHead().fields())
{
map.put(field.getName(), field.toString());
}
list.add(map);
}
return list;
} catch (TdbException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}finally{
session.logout();
pool.close();
}
}
//修改记录
/**
* @param dbName :数据库名字
* @param id :记录号
* @param field :字段名
* @param val :对应字段值
* @throws TdbException
* */
public static int update(String dbName,String id,String [] field,String [] val) throws TdbException{
session=Login(null, null, null, null).acquire();
try {
TdbDatabaseDesign db=new TdbDatabaseDesign(session);
db.get(dbName);
TdbRecordSet rset=new TdbRecordSet(session);
rset.setDatabase(dbName);
TdbRecord rec=new TdbRecord(session, db, true);
int r_id=Integer.parseInt(id);
rec.setRecordId(r_id);
rec.get();
for (int i = 0; i < val.length; i++) {
rec.getHead().getField(field[i]).setValue(0, val[i]);
}
rec.commit();
db.index();
System.out.println("修改记录成功!");
return 1;
} catch (TdbException e) {
// TODO Auto-generated catch block
System.out.println("添加记录失败!");
e.printStackTrace();
return 0;
}finally{
session.logout();
pool.close();
}
}
//删除记录
/***
*
* @param dbName :数据库名
* @param id :记录号
* @return
* @throws TdbException
*/
public static int delete(String dbName,String id) throws TdbException{
session=Login(null, null, null, null).acquire();
try {
TdbRecord rec=new TdbRecord(session);
rec.setDatabaseName(dbName);
rec.setRecordId(Integer.parseInt(id));
rec.delete();
TdbDatabaseDesign db = new TdbDatabaseDesign(session);
db.get(dbName);
db.reindex();
System.out.println("删除记录成功!");
return 1;
} catch (TdbException e) {
// TODO Auto-generated catch block
System.out.println("删除记录失败!");
e.printStackTrace();
return 0;
}finally{
session.logout();
pool.close();
}
}
/***
* @param 精确查找
* @param dbName 数据库名
* @param tem 想要获取的字段名称数组
* @param arg1 想要检索的字段
* @param arg2 想要检索的内容
* @return List集合
* @throws Exception
*/
public static List<Map<String,Object>> query(String dbName,String [] tem,String [] arg1,String []arg2) throws Exception{
session=Login(null, null, null, null).acquire();
List<Map<String,Object>> list=new ArrayList<Map<String,Object> >();
try {
TdbRecordSet rset=new TdbRecordSet(session);
rset.setDatabase(dbName);
rset.setQueryCCL(true);
StringBuilder sb =new StringBuilder();
sb.append("F ");
for (int i = 0; i < arg1.length; i++) {
sb.append(arg1[i]+" = '" +arg2[i]+"' ");
sb.append(" and ");
}
sb.append(" r = fr 1");
sb.toString();
System.out.println(sb.toString());
rset.setQuery(sb.toString());
rset.setFrom(1);
TdbRecord template=new TdbRecord(session);
for (int i = 0; i < tem.length; i++) {
template.addToTemplate(tem[i]);
}
rset.setRetrievalTemplate(template);
rset.get();
for (TdbRecord r : rset.records()) {
Map<String,Object> map=new HashMap<String,Object>();
map.put("id", r.getRecordId());
for (TdbField field : r.getHead().fields())
{
map.put(field.getName(), field.toString());
}
list.add(map);
}
return list;
} catch (TdbException e) {
// TODO Auto-generated catch block
throw new Exception();
}finally{
session.logout();
pool.close();
}
}
/***
* @param 模糊查找
* @param dbName 数据库名
* @param tem 想要获取的字段名称数组
* @param arg1 想要检索的字段
* @param arg2 想要检索的内容
* @return List集合
* @throws TdbException
*/
public static List<Map<String,Object>> queryLike(String dbName,String [] tem,String [] arg1,String []arg2) throws TdbException{
session=Login(null, null, null, null).acquire();
List<Map<String,Object>> list=new ArrayList<Map<String,Object> >();
try {
TdbRecordSet rset=new TdbRecordSet(session);
rset.setDatabase(dbName);
rset.setQueryCCL(true);
StringBuilder sb =new StringBuilder();
sb.append("F ");
if(arg1!=null){
for (int i = 0; i < arg1.length; i++) {
if(arg2[i].length()>0){
sb.append(arg1[i]+" = " +arg2[i]+" ");
sb.append(" and ");
}
}
}else{
sb.append(" "+arg2[0]);
}
sb.append(" and r = fr 1");
sb.toString();
System.out.println(sb.toString());
rset.setQuery(sb.toString());
rset.setFrom(1);
TdbRecord template=new TdbRecord(session);
for (int i = 0; i < tem.length; i++) {
template.addToTemplate(tem[i]);
}
rset.setRetrievalTemplate(template);
rset.get();
for (TdbRecord r : rset.records()) {
Map<String,Object> map=new HashMap<String,Object>();
map.put("id", r.getRecordId());
for (TdbField field : r.getHead().fields())
{
map.put(field.getName(), field.toString());
}
list.add(map);
}
session.logout();
return list;
} catch (TdbException e) {
// TODO Auto-generated catch block
return null;
}finally{
session.logout();
pool.close();
}
}
/**
* 文本抽取
* @param session
* @param filename
* @throws TdbException
*/
public static void wbcq(TdbSession ses,String filename) throws TdbException{
try {
session=Login(null, null, null, null).acquire();
TdbDatabaseDesign db=new TdbDatabaseDesign(session);
db.get("LWFJ");
TdbRecord record = new TdbRecord(session, db, false);
TdbComponent head = record.getHead();
TdbPhraseField field1= (TdbPhraseField) head.createField("FILE_NAME", TdbFieldType.PhraseField);
field1.appendValue(filename);
TdbTextField field =(TdbTextField) head.createField("FILE_TEXT", TdbFieldType.TextField);
field.getTextExtractionInfo().setExtractText(true);
field.getTextExtractionInfo().setBinaryCopyField("FILE_BLOB");
field.getTextExtractionInfo().setFileName(filename);
File file= new File(filename);
FileInputStream is=new FileInputStream(file);
field.getTextExtractionInfo().setStream(is);
field.getTextExtractionInfo().setPropertyNameField("PROP_NAME");
field.getTextExtractionInfo().setPropertyValueField("PROP_VALUE");
record.commit();
is.close();
db.index();
//延迟5秒,留足抽取时间
Thread.sleep(5000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
session.logout();
pool.close();
}
}
/**
* 获取数据库的所有用户名
* @throws TdbException
*/
public static void getUser() throws TdbException{
session=Login(null, null, null, null).acquire();
try {
TdbDatabaseList tdbList = new TdbDatabaseList(session);
TdbDatabaseDesign db=new TdbDatabaseDesign(session);
for (int i = 0; i < tdbList.size(); i++) {
String DbName = tdbList.get(i).getName();
db.get(DbName);
for (int j = 0; j < db.getFieldCount(); j++) {
}
}
} catch (TdbException e) {
e.printStackTrace();
}finally{
session.logout();
pool.close();
}
}
/**
* 获取数据库的所有用户名
* @throws TdbException
*/
public static void getUser1() throws TdbException{
session=Login(null, null, null, null).acquire();
try {
TdbDatabaseList tdbList = new TdbDatabaseList(session);
TdbDatabaseDesign db=new TdbDatabaseDesign(session);
for (int i = 0; i < tdbList.size(); i++) {
String DbName = tdbList.get(i).getName();
db.get(DbName);
}
} catch (TdbException e) {
e.printStackTrace();
}finally{
session.logout();
pool.close();
}
}
public static ArrayList<Map<String,Object>> getDbName(String DBname) throws TdbException{
session = pool.acquire();
ArrayList<Map<String,Object>>list= new ArrayList<Map<String,Object>>();
try {
TdbDatabaseList tdbList = new TdbDatabaseList(session);
for (int i = 0; i < tdbList.size(); i++) {
Map<String,Object> map =new HashMap<String, Object>();
if(DBname==null){
map.put("DbName", tdbList.get(i).getName());
map.put("OwnerName", tdbList.get(i).getOwnerName());
map.put("LastModifiedDate", tdbList.get(i).getModifiedDate()+" "+tdbList.get(i).getModifiedTime());
list.add(map);
}else{
if(tdbList.get(i).getName().toUpperCase().equals(DBname.toUpperCase())){
map.put("DbName", tdbList.get(i).getName());
map.put("OwnerName", tdbList.get(i).getOwnerName());
map.put("LastModifiedDate", tdbList.get(i).getModifiedDate()+" "+tdbList.get(i).getModifiedTime());
list.add(map);
}
}
}
} catch (TdbException e) {
throw new TdbException("获取数据库用户错误");
}finally{
session.logout();
pool.close();
}
return list;
}
/**
*
* @param dbName
* @param tem
* @param arg1
* @return
* @throws TdbException
*/
public static List<Map<String,Object>> queryData(String dbName,String [] tem,String arg1) throws TdbException{
session=Login(null, null, null, null).acquire();
List<Map<String,Object>> list=new ArrayList<Map<String,Object> >();
try {
TdbRecordSet rset=new TdbRecordSet(session);
rset.setDatabase(dbName);
rset.setQueryCCL(true);
rset.setQuery(arg1);
rset.setFrom(1);
TdbRecord template=new TdbRecord(session);
for (int i = 0; i < tem.length; i++) {
template.addToTemplate(tem[i]);
}
rset.setRetrievalTemplate(template);
rset.get();
for (TdbRecord r : rset.records()) {
Map<String,Object> map=new HashMap<String,Object>();
map.put("id", r.getRecordId());
for (TdbField field : r.getHead().fields())
{
map.put(field.getName(), field.toString());
}
list.add(map);
}
session.logout();
return list;
} catch (TdbException e) {
// TODO Auto-generated catch block
return null;
}finally{
session.logout();
pool.close();
}
}
public static String convertFileSize(long size) {
long kb = 1024;
long mb = kb * 1024;
long gb = mb * 1024;
if (size >= gb) {
return String.format("%.1f GB", (float) size / gb);
} else if (size >= mb) {
float f = (float) size / mb;
return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
} else if (size >= kb) {
float f = (float) size / kb;
return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
} else
return String.format("%d B", size);
}
public static void main(String[] args) throws Exception {
}
/**
* 根据byte数组,生成文件
*/
public static void getFile(byte[] bfile, String filePath,String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
dir.mkdirs();
}
file = new File(filePath+"\\"+fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}