windows环境下 java 使用sqlite-jdbc 加载mod_spatialite用于地理空间处理

  由于项目需要,将sqlite中的数据使用空间函数(st_astext(), st_geomfromtext()等)处理空间坐标数据,这就需要加载mod_spatialite组件,从网上找了很多方法,也问了gpt,也从官网上(http://www.gaia-gis.it/gaia-sins/spatialite-cookbook/html/java.html)找到了加载mod_spatialite组件的示例,但是就是不好使,要么报“无权限”,要么报“找不到指定路径",这个问题前前后后折磨了我半个月,最终还是解决了,本着共享的原则,share出步骤

  1.去官网(https://www.gaia-gis.it/fossil/libspatialite/index)下载mod_spatialite组件压缩包,并解压

 2.解压后的 所有文件 都放到C:\Windows\System32文件夹(笔者使用的是windows64位系统),这一步特别重要,也是持续折磨笔者2个周的问题所在,不然会报无权限或找不到指定路径(有的说放在java项目的根目录,或者放在同一个文件夹,都不好使)

 3.使用maven加载sqlite-jdbc, 3.8版本以上都可以,笔者使用的是3.35.0

<dependency>
     <groupId>org.xerial</groupId>
     <artifactId>sqlite-jdbc</artifactId>
      <version>3.35.0</version>
</dependency>

4.创建Connnection 时,要开启加载扩展插件的权限,附上代码,在同事的电脑上也试过, 亲测好使

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.sqlite.SQLiteConfig;

public class SpatialiteSample {


    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // load the sqlite-JDBC driver using the current class loader
        Class.forName("org.sqlite.JDBC");

        Connection conn = null;
        try {
            // enabling dynamic extension loading
            // absolutely required by SpatiaLite
            System.out.println(org.sqlite.SQLiteJDBCLoader.getVersion());
//            System.setProperty("java.library.path", "E:\\google-download\\mod_spatialite-5.0.1-win-amd64\\mod_spatialite-5.0.1-win-amd64");
            SQLiteConfig config = new SQLiteConfig();
            config.enableLoadExtension(true);
            conn = config.createConnection("jdbc:sqlite:spatialite-test.sqlite");
            // create a database connection
//            conn = DriverManager.getConnection("jdbc:sqlite:spatialite-test.sqlite?enable_load_extension=1"
//                    );
//            conn.enableLoadExtension(true);
            Statement stmt = conn.createStatement();
            stmt.setQueryTimeout(30); // set timeout to 30 sec.

            // loading SpatiaLite
            stmt.execute("SELECT load_extension('mod_spatialite')");

            // checking SQLite and SpatiaLite version + target CPU
            String sql = "SELECT spatialite_version(), spatialite_target_cpu()";
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                // read the result set
                String msg = "SQLite version: ";
                msg += rs.getString(1);
                System.out.println(msg);
                msg = "SpatiaLite version: ";
                msg += rs.getString(2);
                System.out.println(msg);
            }
            // enabling Spatial Metadata
            // this automatically initializes SPATIAL_REF_SYS and GEOMETRY_COLUMNS
//            sql = "SELECT InitSpatialMetadata(1)";
//            stmt.execute(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

最后附上截图:

 

posted @ 2023-05-26 13:26  发奋推墙  阅读(703)  评论(0编辑  收藏  举报