geotools学习之连接数据库并获取数据

本文主要讲连接postgis数据库,并且获得数据,这里的数据不拘泥postgis数据库

一、demo示例

package org.geotools.WPS;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.postgis.PostgisNGDataStoreFactory;

import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.opengis.feature.simple.SimpleFeature;
public class postgis {
    /**
     * @param dbtype: 数据库类型,postgis or mysql
     * @param host: ip地址
     * @param port: 端口号
     * @param database: 需要连接的数据库
     * @param userName: 用户名
     * @param password: 密码
     * @param tableName: a需要连接的表名
     * @return: 返回为FeatureCollection类型
     */
    private static SimpleFeatureCollection  connAndgetCollection(String dbtype, String host, String port,  
            String database, String userName, String password,String tableName) {  
        Map<String, Object> params = new HashMap<String, Object>();   
        DataStore pgDatastore=null;
        params.put(PostgisNGDataStoreFactory.DBTYPE.key, dbtype); //需要连接何种数据库,postgis or mysql
        params.put(PostgisNGDataStoreFactory.HOST.key, host);//ip地址
        params.put(PostgisNGDataStoreFactory.PORT.key, new Integer(port));//端口号
        params.put(PostgisNGDataStoreFactory.DATABASE.key, database);//需要连接的数据库  
        params.put(PostgisNGDataStoreFactory.SCHEMA.key, "public");//架构
        params.put(PostgisNGDataStoreFactory.USER.key, userName);//需要连接数据库的名称 
        params.put(PostgisNGDataStoreFactory.PASSWD.key, password);//数据库的密码
        SimpleFeatureCollection  fcollection=null;
        try { 
        	//获取存储空间
            pgDatastore = DataStoreFinder.getDataStore(params); 
            //根据表名获取source
            SimpleFeatureSource fSource=pgDatastore.getFeatureSource(tableName);                     
            if (pgDatastore != null) {  
                System.out.println("系统连接到位于:" + host + "的空间数据库" + database  
                        + "成功!"); 
                 fcollection=fSource.getFeatures();
            } else {  
                System.out.println("系统连接到位于:" + host + "的空间数据库" + database  
                        + "失败!请检查相关参数"); 
               
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
            System.out.println("系统连接到位于:" + host + "的空间数据库" + database  
                    + "失败!请检查相关参数");                       
        }  
        return fcollection;
    }  
    public static void main(String[] args) {
    	//调用方法
    	SimpleFeatureCollection  featureColls=postgis.connAndgetCollection("postgis", "localhost", "5432", "sqlView", "postgres", "postgres","roa_4m");
        SimpleFeatureIterator itertor = featureColls.features();
        //循环读取feature,itertor.hasNext()表示游标下一个是否有数据,有返回ture,否则为false               
       while (itertor.hasNext())
       {
           //获取每一个要素
           SimpleFeature feature = itertor.next();
           System.out.println(feature.getAttribute("roa_4m_id"));
       } 
      
}
}

 

转载自:https://blog.csdn.net/weixin_40184249/article/details/83957572

You may also like...