【问题标题】:Postgresql JDBC connection via eclipse not happening通过 Eclipse 的 Postgresql JDBC 连接没有发生
【发布时间】:2012-06-10 14:40:51
【问题描述】:

我正在尝试对 postgresql 数据库进行简单的连接测试,代码运行良好,但我收到以下消息:

PostgreSQL 9.0 JDBC4 (build 802) 发现于:jar:file:/C:/thales/Dropbox/study/java/jars/postgresql-9.0-802.jdbc4.jar!/org/postgresql/Driver.class

我使用的是地址http://jdbc.postgresql.org/download.html提供的jdbc文件

下面是代码:

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class connector {
  public static void main(String[] argv) {
  System.out.println("Checking if Driver is registered with DriverManager.");

  try {
    Class.forName("org.postgresql.Driver");
  } catch (ClassNotFoundException cnfe) {
    System.out.println("Couldn't find the driver!");
    System.out.println("Let's print a stack trace, and exit.");
    cnfe.printStackTrace();
    System.exit(1);
  }

  System.out.println("Registered the driver ok, so let's make a connection.");

  Connection c = null;

  try {
    // The second and third arguments are the username and password,
    // respectively. They should be whatever is necessary to connect
    // to the database.
    c = DriverManager.getConnection("jdbc:postgresql://localserver/test","ping", "pong");
  } catch (SQLException se) {
    System.out.println("Couldn't connect: print out a stack trace and exit.");
    se.printStackTrace();
    System.exit(1);
  }

  if (c != null)
    System.out.println("Hooray! We connected to the database!");
  else
    System.out.println("We should never get here.");
  }
}

【问题讨论】:

  • 您的问题令人困惑。您是说它正在显示该消息...但根据您给我们的消息,它只是说它找到了 JDBC 驱动程序...这不是问题...
  • 嗯,我期待任何 System.out.println 给出的任何输出,但我不确定可能出了什么问题。
  • 您是否在 IDE 中尝试过断点?也许您与数据库的连接由于某种原因需要花费大量时间等等......
  • 也不是,我在本地数据库下尝试,即使使用无效/有效的用户、密码、主机,我仍然得到与帖子中描述的相同的结果。
  • 显示程序执行的完整输出,以及调用它的命令行(例如java -classpath .... -jar .....)。这看起来像是一个简单的配置错误问题。

标签: java eclipse postgresql jdbc


【解决方案1】:

感谢所有反馈,但我设法解决了重新安装 eclipse (??) 的问题

使用相同的代码和相同的 jdbc 驱动程序,问题中描述的代码有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-13
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 2014-07-11
    相关资源
    最近更新 更多