Intellij Idea Java连接到Amazon Redshift

omjgkv6w  于 2024-01-05  发布在  Java
关注(0)|答案(3)|浏览(224)

我试图用我的Java代码连接到Amazon Redshift数据库。我在AWS网站上找到了一个应该可以工作的代码片段。但是,我在实现JDBC驱动程序时遇到了问题。这是网站和来自网站的代码:https://docs.aws.amazon.com/redshift/latest/mgmt/connecting-in-code.html

  1. package connection;
  2. import java.sql.*;
  3. import java.util.Properties;
  4. public class Docs {
  5. //Redshift driver: "jdbc:redshift://x.y.us-west-
  6. 2.redshift.amazonaws.com:5439/dev";
  7. //or "jdbc:postgresql://x.y.us-west-2.redshift.amazonaws.com:5439/dev";
  8. static final String dbURL = "***jdbc cluster connection string ****";
  9. static final String MasterUsername = "***master user name***";
  10. static final String MasterUserPassword = "***master user password***";
  11. public static void main(String[] args) {
  12. Connection conn = null;
  13. Statement stmt = null;
  14. try{
  15. //Dynamically load driver at runtime.
  16. //Redshift JDBC 4.1 driver: com.amazon.redshift.jdbc41.Driver
  17. //Redshift JDBC 4 driver: com.amazon.redshift.jdbc4.Driver
  18. Class.forName("com.amazon.redshift.jdbc.Driver");
  19. //Open a connection and define properties.
  20. System.out.println("Connecting to database...");
  21. Properties props = new Properties();
  22. //Uncomment the following line if using a keystore.
  23. //props.setProperty("ssl", "true");
  24. props.setProperty("user", MasterUsername);
  25. props.setProperty("password", MasterUserPassword);
  26. conn = DriverManager.getConnection(dbURL, props);
  27. //Try a simple query.
  28. System.out.println("Listing system tables...");
  29. stmt = conn.createStatement();
  30. String sql;
  31. sql = "select * from information_schema.tables;";
  32. ResultSet rs = stmt.executeQuery(sql);
  33. //Get the data from the result set.
  34. while(rs.next()){
  35. //Retrieve two columns.
  36. String catalog = rs.getString("table_catalog");
  37. String name = rs.getString("table_name");
  38. //Display values.
  39. System.out.print("Catalog: " + catalog);
  40. System.out.println(", Name: " + name);
  41. }
  42. rs.close();
  43. stmt.close();
  44. conn.close();
  45. }catch(Exception ex){
  46. //For convenience, handle all errors here.
  47. ex.printStackTrace();
  48. }finally{
  49. //Finally block to close resources.
  50. try{
  51. if(stmt!=null)
  52. stmt.close();
  53. }catch(Exception ex){
  54. }// nothing we can do
  55. try{
  56. if(conn!=null)
  57. conn.close();
  58. }catch(Exception ex){
  59. ex.printStackTrace();
  60. }
  61. }
  62. System.out.println("Finished connectivity test.");
  63. }

字符串
}
我得到了我的连接凭据,但我得到了以下错误。

java.lang.ClassNotFoundException:com.amazon.redshift.jdbc4.Driver

这是由这条线引起的:

  1. Class.forName("com.amazon.redshift.jdbc.Driver");


我没有在任何地方实现这个驱动程序,所以这个错误是有道理的。问题是,IntelliJ IDEA有一个插件(数据库导航器),它不像预期的那样工作,我在他们的论坛上得不到任何帮助。
有没有其他方法可以包含JDBC驱动程序,以便代码可以使用它(在IntelliJ中)?

编辑:

在添加了作为外部库的MySQL之后,我的项目看起来像这样:


的数据
然而,当我运行代码时,我得到了同样的错误。

izj3ouym

izj3ouym1#

您可以像这样导入Amazon Redshift JDBC驱动程序:

  • 点击文件
  • 项目结构(Windows/Linux上为CTRL + ALT + S,Mac OS X上为CTRL + ALT + S)
  • 单击左侧的模块模块
  • 点击相关内容
  • “+”→ JAR
iqjalb3h

iqjalb3h2#

  • 从下面的URL下载Amazon Redshift JDBC驱动程序。

https://docs.aws.amazon.com/redshift/latest/mgmt/configure-jdbc-connection.html#download-jdbc-driver

  • 将下载的Jar添加到类路径中。

会成功的
您可以按照@Corentin建议的步骤执行步骤#2。

e4eetjau

e4eetjau3#

请添加以下Maven/Gradle依赖项或从以下URL下载Amazon Redshift JDBC驱动程序
https://mvnrepository.com/artifact/com.amazon.redshift/redshift-jdbc42/2.1.0.24
通过传递正确的dbURL和凭据来运行程序。

相关问题