空間資料類型範例

下載 JDBC 驅動程式

此適用於 SQL Server 的 Microsoft JDBC 驅動程式範例應用程式示範如何建立、插入,以及擷取空間資料類型 (幾何和地理)。

此範例的程式碼檔案名稱為 SpatialDataTypes.java,並位於下列位置:

\<installation directory>\sqljdbc_<version>\<language>\samples\datatypes

需求

若要執行此範例應用程式,您必須將 Classpath 設定為包含 mssql-jdbc jar 檔案。 如需如何設定 classpath 的詳細資訊,請參閱使用 JDBC 驅動程式

注意

適用於 SQL Server 的 Microsoft JDBC 驅動程式提供 mssql-jdbc 類別庫檔案,可根據您慣用的 Java Runtime Environment (JRE) 設定使用。 如需選擇哪個 JAR 檔案的詳細資訊,請參閱 JDBC Driver 的系統需求

範例

在下列範例中,範例程式碼會建立一個名為 SpatialDataTypesTable_JDBC_Sample 的資料表,其中包含 'Geometry' 和 'Geography' 資料行。

此範例會先從代表某個 POINT 的 Well-Known-Text (WKT),建立 'Geometry' 和 'Geography' 物件。 其會搭配參數化查詢使用 SQLServerPreparedStatement,來將資料對應到每個資料行。

最後,此範例會將資料插入到資料表,並加以擷取。 這些資料會以 WKT 的形式顯示。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import com.microsoft.sqlserver.jdbc.Geography;
import com.microsoft.sqlserver.jdbc.Geometry;
import com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement;
import com.microsoft.sqlserver.jdbc.SQLServerResultSet;

public class SpatialDataTypes {

    private static String tableName = "SpatialDataTypesTable_JDBC_Sample";

    public static void main(String[] args) {

        // Create a variable for the connection string.
        String connectionUrl = "jdbc:sqlserver://<server>:<port>;encrypt=true;databaseName=<database>;user=<user>;password=<password>";
        // Establish the connection.
        try (Connection con = DriverManager.getConnection(connectionUrl); Statement stmt = con.createStatement();) {
            dropAndCreateTable(stmt);

            // TODO: Implement Sample code
            String geoWKT = "POINT(3 40 5 6)";
            Geometry geomWKT = Geometry.STGeomFromText(geoWKT, 0);
            Geography geogWKT = Geography.STGeomFromText(geoWKT, 4326);

            try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) con
                    .prepareStatement("insert into " + tableName + " values (?, ?)");) {
                pstmt.setGeometry(1, geomWKT);
                pstmt.setGeography(2, geogWKT);
                pstmt.execute();

                SQLServerResultSet rs = (SQLServerResultSet) stmt.executeQuery("select * from " + tableName);
                rs.next();

                System.out.println("Geometry data: " + rs.getGeometry(1));
                System.out.println("Geography data: " + rs.getGeography(2));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void dropAndCreateTable(Statement stmt) throws SQLException {
        stmt.executeUpdate("if object_id('" + tableName + "','U') is not null" + " drop table " + tableName);

        stmt.executeUpdate("Create table " + tableName + " (c1 geometry, c2 geography)");
    }
}

另請參閱

使用 JDBC 資料類型