ArcSDE SDK Java 快速实践 5

I. 数据操作

· 添加

添加数据操作封装在SeInsert类中,如下的代码展示了如何在一个名为“PT”的点图层中添加数据:

SeConnection conn = getConn();

if (conn != null) {

try {

SeInsert insert = new SeInsert(conn);

insert.intoTable("PT", new String[] { "SHAPE", "NAME" });

insert.setWriteMode(true);// 开启buffer

SeLayer layer = new SeLayer(conn, "PT", "SHAPE");

SeCoordinateReference cr = layer.getCoordRef();

for (int i = 0; i < 10; i++) {

SeRow row = insert.getRowToSet();

SeShape shape = new SeShape(cr);

String wkt = "POINT("+i+" " + i+")";

shape.generateFromText(wkt);

row.setShape(0, shape);

row.setNString(1, "名称" + i);

insert.execute();

}

insert.flushBufferedWrites();

insert.close();

} catch (SeException e) {

e.printStackTrace();

} finally {

try {

conn.close();

} catch (SeException e) {

e.printStackTrace();

}

}

}

· 查询

查询数据操作封装在SeQuery类中,如下的代码展示了如何在一个名为“PT”的点图层中查询数据,其中包括属性和空间过滤条件:

SeConnection conn = getConn();

if (conn != null) {

try {

SeLayer layer = new SeLayer(conn, "PT", "SHAPE");

SeCoordinateReference cr = layer.getCoordRef();

SeShape inputShape = new SeShape(cr);

inputShape.generateFromText("POLYGON((2 2,2 4,4 4,4 2,2 2))");

SeSqlConstruct sqlC = new SeSqlConstruct("PT");

sqlC.setWhere("OBJECTID>4");

String[] cols = new String[] { "OBJECTID", "SHAPE","NAME" };

SeQuery query = new SeQuery(conn, cols, sqlC);

query.prepareQuery();

SeFilter filter = new SeShapeFilter("PT", "SHAPE", inputShape, SeShapeFilter.METHOD_AI);

SeFilter[] filters = new SeFilter[] { filter };

query .setSpatialConstraints(SeQuery.SE_SPATIAL_FIRST, true, filters);

query.execute();

SeRow row = query.fetch();

while (row != null) {

try {

int objectid = row.getInteger(0);

SeShape shape = row.getShape(1);

String name = row.getNString(2);

System.out.println(name);

} catch (Exception e) {

e.printStackTrace();

}

row = query.fetch();

}

} catch (SeException e) {

e.printStackTrace();

} finally {

try {

conn.close();

} catch (SeException e) {

e.printStackTrace();

}

}

}

查询还可以进行表连接,比如一个空间表连接一个业务表:

SeConnection conn = getConn();

if (conn != null) {

try {

String t1 = "CITIES";

String t2 = "WUYF_CITIES_JOIN";

SeTable table1 = new SeTable(conn, t1);

SeColumnDefinition[] tableDef1 = table1.describe();

int numCols1 = tableDef1.length;

SeTable table2 = new SeTable(conn, t2);

SeColumnDefinition[] tableDef2 = table2.describe();

int numCols2 = tableDef2.length;

String[] cols = new String[numCols1 + numCols2];

for (int i = 0; i < numCols1; i++) {

cols[i] = tableDef1[i].getName();

}

for (int i = 0; i < numCols2; i++) {

cols[i + numCols1] = tableDef2[i].getName();

}

SeSqlConstruct sqlCons = new SeSqlConstruct(

new String[] { t1, t2 }, "");

SeQuery query = new SeQuery(conn, cols, sqlCons);

SeQueryInfo queryInfo = new SeQueryInfo();

queryInfo.setQueryType(SeQueryInfo.SE_QUERYTYPE_ATTRIBUTE_FIRST);

query.prepareQueryInfo(queryInfo);

query.execute();

SeRow row = query.fetch();

while (row != null) {

for (int j = 0; j < cols.length; j++) {

System.out.println(row.getObject(j).toString());

}

System.out.println("-----------------------");

row = query.fetch();

}

} catch (SeException e) {

e.printStackTrace();

} finally {

try {

conn.close();

} catch (SeException e) {

e.printStackTrace();

}

}

}

· 修改

修改数据操作封装在SeUpdate类中,如下的代码展示了如何在一个名为“PT”的点图层中修改数据:

SeConnection conn = getConn();

if (conn != null) {

try {

SeUpdate update = new SeUpdate(conn);

update.toTable("PT", new String[] { "NAME" }, "OBJECTID=5");

SeRow row = update.getRowToSet();

row.setNString(0, "修改");

update.execute();

update.close();

} catch (SeException e) {

e.printStackTrace();

} finally {

try {

conn.close();

} catch (SeException e) {

e.printStackTrace();

}

}

}

· 删除

删除数据操作封装在SeDelete类中,如下的代码展示了如何在一个名为“PT”的点图层中删除数据:

SeConnection conn = getConn();

if (conn != null) {

try {

SeDelete delete = new SeDelete(conn);

delete.fromTable("PT", "OBJECTID>4");

delete.close();

} catch (SeException e) {

e.printStackTrace();

} finally {

try {

conn.close();

} catch (SeException e) {

e.printStackTrace();

}

}

}

你可能感兴趣的:(java,J#)