|
阅读:1976回复:2
如何连接和操作personal geodatabase
import com.esri.arcgis.datasourcesGDB.AccessWorkspaceFactory;<br>import com.esri.arcgis.geodatabase.ICursor;<br>import com.esri.arcgis.geodatabase.IFeatureWorkspace;<br>import com.esri.arcgis.geodatabase.IFeatureWorkspaceProxy;<br>import com.esri.arcgis.geodatabase.IQueryFilter;<br>import com.esri.arcgis.geodatabase.IRow;<br>import com.esri.arcgis.geodatabase.ITable;<br>import com.esri.arcgis.geodatabase.IWorkspace;<br>import com.esri.arcgis.geodatabase.IWorkspaceFactory;<br>import com.esri.arcgis.geodatabase.QueryFilter;<br>import com.esri.arcgis.system.EngineInitializer;<br>import com.esri.arcgis.system.AoInitialize;<br>import com.esri.arcgis.system.esriLicenseExtensionCode;<br>import com.esri.arcgis.system.esriLicenseProductCode;<br>import com.esri.arcgis.systemUI.esriCommandStyles;<br>import com.linar.jintegra.AutomationException;<br>import java.io.IOException;<br><br>import javax.swing.*;<br>import java.awt.*;<br><br><br>public class Geodatabase extends JFrame{<br> <br> String people = null;<br> JFrame frame;<br> JLabel label; <br> JLabel peoplela;<br> <br> JButton ok; <br> <br> JTextArea number;<br> <br> <br> IFeatureWorkspace pFeatWorkspace;<br> IWorkspace pWorkspace;<br> String dbLocation;<br> <br> <br> <br> public Geodatabase () throws Exception{<br> <br> setSize(400,200);<br> setTitle("Selected road population");<br> getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());<br> JLabel label = new JLabel();<br> JLabel peoplela = new JLabel(this.people);<br> JButton ok = new JButton();<br> setVisible(true); <br> <br> // add label<br> label.setFont(new java.awt.Font("Arial", 0, 12));<br> label.setText("Selected road population is : ");<br> getContentPane().add(label, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 200, 20));<br> <br> // add ok button<br> ok.setFont(new java.awt.Font("Arial", 0, 10));<br> ok.setToolTipText("ok");<br> ok.setLabel("ok");<br> ok.setMargin(new java.awt.Insets(2, 2, 2, 2));<br> getContentPane().add(ok, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 80, 60, 20));<br> ok.addActionListener(new java.awt.event.ActionListener() {<br> public void actionPerformed(java.awt.event.ActionEvent evt) {<br> okActionPerformed(evt);<br> }<br> });<br> this.dbLocation ="D:\\08\\database.mdb";<br> this.openPDB();<br> <br> }<br> private static void licenseCheckOut(){<br> try{<br> AoInitialize aoInit = new AoInitialize(); aoInit.initialize(<br> esriLicenseProductCode.esriLicenseProductCodeEngine );<br> aoInit.checkOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst);<br> System.out.println("licenseChecked out");<br> }catch (IOException e){<br> System.out.println("Program Exit: Unable to initialize ArcObjects");<br> System.exit(0);<br> }<br> }<br> // open a personal database with a known path and name<br> //http://edndoc.esri.com/arcobjects/9.2/NET/c778d2bb-eb36-4793-9c89-20795811c5eb.htm<br> public void openPDB() throws Exception { <br> IWorkspaceFactory pWorkFactory = new AccessWorkspaceFactory(); <br> pWorkspace = pWorkFactory.openFromFile(this.dbLocation, 0); <br> System.out.println("good");<br> this.pFeatWorkspace = new IFeatureWorkspaceProxy(pWorkspace);<br> <br> }<br> // Opens the SelectedRd table from database<br> private ITable openTable()throws Exception {<br> <br> ITable table = this.pFeatWorkspace.openTable("Bomb_type");<br> return table;<br> }<br> //Specifies a query filter that retrieves all the values from the TNT column.<br> private IQueryFilter queryFilter() throws Exception{<br> IQueryFilter queryFilter = new QueryFilter();<br> return queryFilter;<br> }<br> // return database location<br> <br> public String locationPDB()throws Exception {<br> String path = this.pWorkspace.getPathName();<br> return path;<br> }<br> private int rows()throws Exception {<br> ITable table=openTable();<br> IQueryFilter queryFilter = queryFilter();<br> int count1 = table.rowCount(queryFilter);////The number of Rows selected by the specified query.<br> return count1;<br> <br> }<br> <br> // Gets all the values from the TNT column from the<br> //table as a String array. <br> <br> public String[] getValues() throws Exception {<br> Geodatabase database = new Geodatabase();<br> int rowscount = database.rows();//<br> String [] fields = new String [rowscount];<br> IQueryFilter queryFilter = queryFilter();<br> ITable table = openTable();<br> int fieldind = table.findField("TNT");<br> // ITable_search.An object cursor that can be used to fetch row objects selected by the specified query.<br> //return value. An reference to a com.esri.arcgis.geodatabase.ICursor<br> ICursor curs = table.ITable_search(queryFilter, false);<br> //Provides access to members that return information about the row, <br> //the table the row belongs to and storing and deleting the row.<br> IRow row = curs.nextRow();<br> int j= 0;<br> while (row != null){ <br> fields [j] = String.valueOf(row.getValue(fieldind));<br> row = curs.nextRow();<br> j++;<br> }<br> <br> return fields;<br> }<br> // add all the value fromTNT together. <br> public int count(String[]field)throws Exception{<br> System.out.println("count");<br> int count = 0;<br> field = this.getValues(); <br> for (int i=0; i<=field.length;i++){<br> count += Integer.parseInt(field);<br> System.out.println(count);<br> }<br> return count;<br> <br> <br> }<br> public void okActionPerformed(java.awt.event.ActionEvent evt){<br> <br> System.exit(0);<br> }<br> public void printVa (int countPeople){<br> <br> <br> }<br> public static void main (String []args){<br> <br> System.out.println("I'm initializing the use of ArcObjects");<br> EngineInitializer.initializeVisualBeans();<br> System.out.println("done.\nNext licensing:");<br> licenseCheckOut();<br> System.out.println("No errors. We may have succeeded!");<br> try {<br> Geodatabase test = new Geodatabase(); <br> System.out.println("Geodatabase()called"); <br> }<br> catch (Exception e){<br> System.out.println("exception occur");<br> }<br> <br> }<br> <br> <br> <br><br>}
|
|
|
1楼#
发布于:2008-08-18 20:31
这是我第一次发贴,出了一些错. 我前面写的话怎莫没了. <br>
上面是我的代码,但是好像有点毛病. 好像还是没有连到我的数据库, 因为执行有没有打印出来System.out.println(count);<br> 请高手们帮我看看, 那里出了错. <br> 先谢了 |
|
|
2楼#
发布于:2008-12-05 21:43
<img src="images/post/smile/dvbbs/em01.gif" />
|
|