使用 Apache Maven 配置 JDBC 连接 - Amazon Redshift
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

使用 Apache Maven 配置 JDBC 连接

Apache Maven 是一款软件项目管理及理解工具。适用于 Java 的 Amazon SDK 支持 Apache Maven 项目。有关更多信息,请参阅《适用于 Java 的 Amazon SDK 开发人员指南》中的将开发工具包与 Apache Maven 一起使用

如果您使用 Apache Maven,可以配置并构建您的项目,以使用 Amazon Redshift JDBC 驱动程序与 Amazon Redshift 集群连接。为此,在项目的 pom.xml 文件中将 JDBC 驱动程序添加为依赖项。如果您使用 Maven 生成项目并希望使用 JDBC 连接,请执行以下部分中的步骤。

将 JDBC 驱动程序配置为 Maven 依赖项
  1. 将 Amazon 存储库或 Maven Central 存储库添加到您的 pom.xml 文件的存储库部分。

    注意

    以下代码示例中的 URL 如果用在浏览器中,将返回错误。仅在 Maven 项目的上下文中使用此 URL。

    要使用安全套接字层 (SSL) 进行连接,请将以下存储库添加到您的 pom.xml 文件。

    <repositories> <repository> <id>redshift</id> <url>https://s3.amazonaws.com/redshift-maven-repository/release</url> </repository> </repositories>

    对于 Maven Central 存储库,请将以下内容添加到您的 pom.xml 文件。

    <repositories> <repository> <id>redshift</id> <url>https://repo1.maven.org/maven2</url> </repository> </repositories>
  2. pom.xml 文件的依赖项部分中,声明您要使用的驱动程序版本。

    Amazon Redshift 向与 JDBC 4.2 API 兼容的工具提供驱动程序。有关这些驱动程序支持的功能的信息,请参阅 下载 Amazon Redshift JDBC 驱动程序版本 2.1

    将以下示例中的 driver-version 替换为您的驱动程序版本,例如 2.1.0.1。对于兼容 JDBC 4.2 的驱动程序,请使用以下内容。

    <dependency> <groupId>com.amazon.redshift</groupId> <artifactId>redshift-jdbc42</artifactId> <version>driver-version</version> </dependency>

    此驱动程序的类名是 com.amazon.redshift.Driver

当您使用 IAM 数据库身份验证时,Amazon Redshift Maven 驱动程序需要以下可选依赖项。

<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-core</artifactId> <version>1.12.23</version> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-redshift</artifactId> <version>1.12.23</version> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-sts</artifactId> <version>1.12.23</version> <scope>runtime</scope> <optional>true</optional> </dependency>

要将 Amazon Redshift JDBC 驱动程序升级或更改到最新版本,请先将依赖项的版本部分修改为驱动程序的最新版本。然后用 Maven Clean Plugin 清除您的项目,如下所示。

mvn clean