-1

我有一个简单的 Java 应用程序,它需要连接到在本地主机上运行的 PostgresSQL。数据库已启动并正在运行,我可以使用 psql 命令行实用程序使用用户名和密码连接到它。这是代码:

try {
   Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException ex) {
   Logger.getLogger(DbLoader.class.getName()).log(Level.SEVERE, null, ex);
}

try ( 
    Connection con = DriverManager.getConnection("jdbc:postgresql://localhost/db",
                    "user",
                    "pass");
 ){
// PROBLEM: con is null at this point
 } catch ( SQLException sqex ) {
    sqex.printStackTrace( System.out );
 }

使用 Mac OS X.8.3。没有抛出异常。

这是 pg_config 的输出:

BINDIR = /Applications/Postgres.app/Contents/MacOS/bin
DOCDIR = /Applications/Postgres.app/Contents/MacOS/share/doc
HTMLDIR = /Applications/Postgres.app/Contents/MacOS/share/doc
INCLUDEDIR = /Applications/Postgres.app/Contents/MacOS/include
PKGINCLUDEDIR = /Applications/Postgres.app/Contents/MacOS/include
INCLUDEDIR-SERVER = /Applications/Postgres.app/Contents/MacOS/include/server
LIBDIR = /Applications/Postgres.app/Contents/MacOS/lib
PKGLIBDIR = /Applications/Postgres.app/Contents/MacOS/lib
LOCALEDIR = /Applications/Postgres.app/Contents/MacOS/share/locale
MANDIR = /Applications/Postgres.app/Contents/MacOS/share/man
SHAREDIR = /Applications/Postgres.app/Contents/MacOS/share
SYSCONFDIR = /Applications/Postgres.app/Contents/MacOS/etc
PGXS = /Applications/Postgres.app/Contents/MacOS/lib/pgxs/src/makefiles/pgxs.mk
CONFIGURE = '--prefix=/Users/mattt/Code/heroku/PostgresApp/Postgres/Vendor/postgres' '--enable-thread-safety' '--without-docdir' '--with-openssl' '--with-gssapi' '--with-bonjour' '--with-krb5' '--with-libxml' '--with-libxslt' '--with-perl' '--with-python'
CC = gcc
CPPFLAGS = -I/usr/include/libxml2 
CFLAGS = -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif- labels -Wformat-security -fno-strict-aliasing -fwrapv
CFLAGS_SL = 
LDFLAGS = -Wl,-dead_strip_dylibs
LDFLAGS_EX = 
LDFLAGS_SL = 
LIBS = -lpgport -lxslt -lxml2 -lssl -lcrypto -lgssapi_krb5 -lz -lreadline -lm 
VERSION = PostgreSQL 9.1.4

有任何想法吗?提前致谢

4

1 回答 1

1

明白了 - 程序员(就是我!)错误。我正在将连接传递给另一个类,并且该类有一个空检查,它没有查看正确的变量。

外卖 - 在怀疑 PostgreSQL 之前先怀疑自己。

感谢所有的评论!-- 米

于 2013-05-18T01:31:40.110 回答