使用java从mysql中的类型几何列中获取数据

时间:2014-09-01 16:02:28

标签: java mysql spatial

我有一个mysql数据库表,其中包含GEOMETRY类型的列geometry。 我之前已成功使用此语句仅从表中查询GEOMETRY列:

 String sql="SELECT AsText(GEOMETRY) from" +tableName+";";

现在,我想查询此表中的所有字段,如下所示:

 String sql="Select * from "+tableName+";";
 Statement st=null;
    ResultSet rs=null;
    try {
        st=(Statement) conn.createStatement();
        rs=st.executeQuery(sql);
        while(rs.next())
        {
          //...getting other fields
          String wkt=rs.getString("AsText(GEOMETRY)");//error
          //add all fields to an ArrayList
        }
   } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   

我在这里收到此错误:

 Select * from ZMAPERP_equi_GIS_DB;
 java.sql.SQLException: Column 'AsText(GEOMETRY)' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1084)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:973)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1162)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5728)
at com.hastha.marerp.googlemaps.DBUtils.getAllFeatures(DBUtils.java:340)
at com.hastha.marerp.googlemaps.LayerLoader.doGet(LayerLoader.java:48)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:2)

你的代码:String wkt=rs.getString("AsText(GEOMETRY)");是发生错误的地方,因为它正在寻找一个名为" AsText(GEOMETRY)"的列。但是查询中已不再存在..你有SELECT * FROM ...这就像说SELECT GEOMETRY FROM ...而不是AsText()部分

我会指定字段,因此请进行查询

SELECT field1, field2, AsText(GEOMETRY) FROM tablename

如果你不想写出每一个,那么你可以像这样添加astext到最后

SELECT *, AsText(GEOMETRY) FROM tablename