gpt4 book ai didi

java - 使用 JDBC 连接 AWS Redshift

转载 作者:行者123 更新时间:2023-12-01 14:05:36 43 4
gpt4 key购买 nike

我想使用 JDBC 连接数据并将数据插入到我的 Amazon Redshift 表中。我编写了以下代码,但在 Class.forName 行不断收到错误

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class RedShiftDataEmitter {
static final String redshiftUrl = "jdbc:redshift://xxxxxxxxx:5439/xxxxxx";
static final String masterUsername = "xxxxxxx";
static final String password = "xxxxxxx";

public static void main(String[] args) {
Connection connection = null;
Statement statement = null;

try {
Class.forName("com.amazon.redshift.jdbc41.Driver");
Properties properties = new Properties();
properties.setProperty("user", masterUsername);
properties.setProperty("password", password);
connection = DriverManager.getConnection(redshiftUrl, properties);
// Further code to follow
} catch(ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
}

提醒一下,我可以使用 SQL Workbench 连接到同一个集群。我的 pom.xml 如下
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-redshift -->
<dependency>
<groupId>com.amazon.redshift</groupId>
<artifactId>redshift-jdbc41</artifactId>
<version>1.2.1.1001</version>
</dependency>

最佳答案

  • 首先在 pom.xml 中添加存储库,


  • <repositories>
    <repository>
    <id>redshift</id>
    <url>http://redshift-maven-repository.s3-website-us-east-1.amazonaws.com/release</url>
    </repository>
    </repositories>


  • 现在添加 Amazon redshift Driver jar maven 依赖


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


    使用此驱动程序 com.amazon.redshift.jdbc42.Driver

    清洁 构建 该项目,它现在应该可以工作了。

    关于java - 使用 JDBC 连接 AWS Redshift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43822806/

    43 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com