sde增加删除

来源:互联网 发布:广州多益网络员工收入 编辑:程序博客网 时间:2024/06/05 17:41

DataStoredataStore = DataStoreFinder.getDataStore(map);



package com.hdsx.query;



import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.NoSuchElementException;


import net.sf.jsqlparser.statement.truncate.Truncate;


import org.geotools.data.DataStore;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.Transaction;
import org.geotools.data.collection.ListFeatureCollection;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureStore;
import org.geotools.feature.FeatureFactory;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.filter.text.cql2.CQL;
import org.geotools.filter.text.cql2.CQLException;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
import org.opengis.filter.Filter;
import org.opengis.util.InternationalString;


public class BaseQuery {
private SimpleFeatureStore simpleDataStore;
public BaseQuery(DataStore dataStore,String statement){
try {
simpleDataStore = (SimpleFeatureStore) dataStore.getFeatureSource(statement);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 根据条件查询数据库的一条记录放入实体bean中
* @param where
* @param t
* @return
* @throws IntrospectionException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
public <T> T queryOne(String where,T t) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
try {
Filter filter=CQL.toFilter(where);
SimpleFeatureCollection simpleCollection = simpleDataStore.getFeatures(filter);
SimpleFeatureIterator iterator = simpleCollection.features();


while( iterator.hasNext() ){
SimpleFeature feature = iterator.next();
/*查询所有字段名称*/
Collection<Property> colpro = feature.getProperties();
//属性
BeanInfo bean = Introspector.getBeanInfo(t.getClass());
PropertyDescriptor[] pd =  bean.getPropertyDescriptors();
ss:for(PropertyDescriptor pda:pd){
String p = pda.getDisplayName().toUpperCase();
for(Property pro:colpro){
if(pro.getName().toString().equals(p)){
pda.getWriteMethod().invoke(t, pro.getValue());
break ss;
}
}


}
System.out.println("dddddddd");
}
} catch (CQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return t;
}
/**
* 为bean的String属性赋值
* @param t
* @return
* @throws IntrospectionException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
public <T> T get(T t) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
BeanInfo bean = Introspector.getBeanInfo(t.getClass());
PropertyDescriptor[] pd =  bean.getPropertyDescriptors();
for(PropertyDescriptor pda:pd){
Object ob= pda.getPropertyType();
System.out.println(ob);
if( ob==java.lang.String.class){
pda.getWriteMethod().invoke(t, "aas");
}
}
return t;
}
/**
* 根据条件查询多条记录放入list中
* SimpleFeature simpleFeature = iterator.next();
* Collection<Property> properties = simpleFeature.getProperties();
* 访问次数多导致程序不执行
* @throws IllegalAccessException 
* @throws InstantiationException 
* @throws IntrospectionException 
* @throws InvocationTargetException 
* @throws IllegalArgumentException 
*/
public <T> List<T> queryList(String where,T t) throws InstantiationException, IllegalAccessException, IntrospectionException, IllegalArgumentException, InvocationTargetException {
List<T> list=new ArrayList<T>();
try {
Filter filter=CQL.toFilter(where);
SimpleFeatureCollection simpleCollection = simpleDataStore.getFeatures(filter);
SimpleFeatureIterator iterator = simpleCollection.features();
BeanInfo bean = Introspector.getBeanInfo(t.getClass());
PropertyDescriptor[] pd =  bean.getPropertyDescriptors();
System.out.println(simpleCollection.size());
while(iterator.hasNext()){
SimpleFeature simpleFeature = iterator.next();
Collection<Property> properties = simpleFeature.getProperties();
int i=0;
//属性
T oa = (T) t.getClass().newInstance();
for(PropertyDescriptor pda:pd){
adad:for(Property propertie:properties){
if(propertie.getName().toString().equals(pda.getName().toUpperCase())){
if(propertie.getValue()!=null)
pda.getWriteMethod().invoke(oa, propertie.getValue());
break adad;
}
}
++i;
System.out.println("xxxxxxxxx");
}
System.out.println("tianjia");
list.add(oa);
}
} catch (CQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public <T> void insert(T t){
SimpleFeatureType sf = simpleDataStore.getSchema();
SimpleFeatureBuilder simpleBuilder = new SimpleFeatureBuilder(sf);
List<AttributeDescriptor> description = sf.getAttributeDescriptors();
System.out.println("bbbbbb");
Object[] param = null;

for(AttributeDescriptor attributeDescriptor:description){
//属性
BeanInfo bean;
try {
bean = Introspector.getBeanInfo(t.getClass());
PropertyDescriptor[] pd =  bean.getPropertyDescriptors();


ss:for(PropertyDescriptor pda:pd){
String p = pda.getDisplayName().toUpperCase();
if(attributeDescriptor.getName().toString().equals(p)){
try {
System.out.println(p);
simpleBuilder.set(p,pda.getReadMethod().invoke(t, param));
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break ss;
}
}
} catch (IntrospectionException e) {
System.out.println("bean有错误");
}

}
SimpleFeature simpleFeature = simpleBuilder.buildFeature("in");
Transaction tr=new DefaultTransaction();
SimpleFeatureCollection simpleFeatureCollection=new ListFeatureCollection(sf);
simpleFeatureCollection.add(simpleFeature);
System.out.println("开始");
simpleDataStore.setTransaction(tr);
try {System.out.println("开始事务");
simpleDataStore.addFeatures(simpleFeatureCollection);
tr.commit();
tr.close();
System.out.println("结束");
} catch (IOException e) {
try {
tr.rollback();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("插入语句错误");
}
}
}