我想使用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>发布于 2017-05-13 02:41:32
我找到了答案,问题是Maven无法识别红移依赖。您必须手动下载并添加jar (外部jar)。
发布于 2017-07-31 17:54:28
<repositories>
<repository>
<id>redshift</id>
<url>http://redshift-maven-repository.s3-website-us-east-1.amazonaws.com/release</url>
</repository>
</repositories>
<dependency>
<groupId>com.amazon.redshift</groupId>
<artifactId>redshift-jdbc42</artifactId>
<version>1.2.1.1001</version>
</dependency>
使用此驱动程序com.amazon.redshift.jdbc42.Driver
Clean和build项目,现在应该可以工作了。
https://stackoverflow.com/questions/43822806
复制相似问题