package sde; import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.util.Vector; import com.esri.sde.sdk.client.*; /** *

Title: ArcSDE access using Java client

*

Description:

*

Copyright: Copyright (c) GEOG 465 2005

*

Department of Geography, University of Washington

* * @author Guirong Zhou, modified by Matt Wilson * @version 1.0 */ public class SDEAccess extends JFrame{ JLabel jLabel1; JLabel jLabel2; /** * Constructor: * * */ public SDEAccess() { super("SDE map display"); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); try { jbInit(); } catch (Exception ex) { ex.printStackTrace(); } } /** * Run this application by instantiating the class * */ public static void main(String[] args) { new SDEAccess(); } /** * This mutator method will do these steps: *
  • Initiate a connection to ArcSDE
  • *
  • Get the layers list
  • *
  • Get a feature from a layer
  • *
  • Display the feature' attrbutes using labels
  • * */ private void jbInit() throws Exception { Container window = getContentPane(); //Add three window controls to the window jLabel1 = new JLabel(); jLabel2 = new JLabel(); //prepare connection parameters SeConnection conn = null; String server = "collab.geog.washington.edu"; int instance = 5151; String database = "classdata"; String user = "geog465"; String password = "geogwin_05"; try { //connect to SDE conn = new SeConnection(server, instance, database, user, password); //get the list of layers available in this SDE database Vector layerList = conn.getLayers(); jLabel1.setText("There are " + layerList.size() + " layers in this SDE database."); //get a layer & changed location to 0 to equal USA_cities int i = this.getLayerByName(layerList, "usa_cities"); SeLayer theLayer = (SeLayer) layerList.elementAt(i); SeSqlConstruct sqlConstruct = new SeSqlConstruct(theLayer.getName(),"STATE_FIPS='53'"); String[] cols = new String[2]; cols[0] = "city_name"; cols[1] = "shape"; SeQuery query = new SeQuery(conn, cols, sqlConstruct); query.prepareQuery(); query.execute(); // create a Vector that stores all city names as Strings SeRow myRow; Vector myVec = new Vector(); while ((myRow = query.fetch()) != null) { myVec.addElement(myRow.getString(0)); } // determines the number of columns in the window int numCols = 5; // String which stores the HTML tags to display the cities in a table String printStuff = ""; // for-loop which creates the HTML tags for a table for (int count = 0; count"); int rowCounter = count; for (int x = 0; x < numCols; x++) { // determines if you have run out of city Strings if (rowCounter < myVec.size()) { String outRow = (String)myVec.elementAt(rowCounter); printStuff = printStuff.concat(""); rowCounter++; } } printStuff = printStuff.concat(""); } printStuff = printStuff.concat("
    " + outRow + "
    "); // label object is set to store the HTML tags jLabel2.setText(printStuff); //it is always important to release resource when done query.close(); conn.close(); window.add(jLabel1, BorderLayout.NORTH); window.add(jLabel2, BorderLayout.SOUTH); // Size window and make it visible. pack(); setVisible(true); } catch (SeException e) { e.printStackTrace(); } } /** * This accessor method will do these steps: *
  • Provide a Vector and a search String
  • *
  • Return an index location in the Vector, type int
  • * */ private int getLayerByName(Vector v, String name) { int n = -1; for(int i=0; i