2017-05-06 347 views
-1

我想使用JDBC连接并将数据插入到Amazon Redshift表中。我写了下面的代码,但在保持线得到一个错误的Class.forName使用JDBC连接AWS Redshift

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工作台同一集群。我的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> 
+0

在你的课程路径上是你的Driver类吗?什么是异常堆栈跟踪? – SMA

+0

抛出java.lang.ClassNotFoundException:com.amazon.redshift.jdbc41.Driver \t在java.net.URLClassLoader.findClass(URLClassLoader.java:381) \t在java.lang.ClassLoader.loadClass(ClassLoader.java:424) \t在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:331) \t在java.lang.ClassLoader.loadClass(ClassLoader.java:357) \t在java.lang.Class.forName0(本机方法) \t at java.lang.Class.forName(Class.java:264) – Nick

+0

那么,你如何运行这个程序?你把驱动程序jar放在* runtime * classpath中吗? (编译*代码时不需要,因为你只使用标准的JDBC类)。 –

回答

1

我找到了答案,事情是,Maven不承认红移依赖。您将不得不手动下载并添加jar(外部jar)。

+2

如果maven不能识别外部依赖项,则可以将以下行添加到pom。 <库> 红移 http://redshift-maven-repository.s3-website-us-east-1.amazonaws.com/release iec2011007

0
  1. 添加存储库中第一pom.xml中,

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

  • 现在添加亚马逊红移驱动程序jar行家dependeny
  • <dependency> 
     
        <groupId>com.amazon.redshift</groupId> 
     
        <artifactId>redshift-jdbc42</artifactId> 
     
        <version>1.2.1.1001</version> 
     
    </dependency>

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

    清洁构建项目,应该现在的工作。