sde增加刪除

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("插入語句錯誤");
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章