空間データ型のサンプル

JDBC ドライバーのダウンロード

この SQL Server 用 Microsoft JDBC ドライバー サンプル アプリケーションでは、空間データ型 (Geometry と Geography) を作成、挿入、取得する方法を示します。

このサンプルのコード ファイルは SpatialDataTypes.java という名前で、次の場所にあります。

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

必要条件

このサンプル アプリケーションを実行するには、クラスパスを設定して mssql-jdbc jar ファイルを含める必要があります。 クラスパスを設定する方法の詳細については、「JDBC ドライバーの使用」を参照してください。

注意

SQL Server 用 Microsoft JDBC ドライバー には、必要な Java ランタイム環境 (JRE) 設定に応じて使用される mssql-jdbc クラス ライブラリ ファイルが用意されています。 選択する JAR ファイルの詳細については、「JDBC Driver のシステム要件」を参照してください。

次の例では、サンプル コードによって、'Geometry' 列と 'Geography' 列を含む SpatialDataTypesTable_JDBC_Sample というテーブルが作成されます。

サンプルでは、最初に、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 データ型の処理