【发布时间】:2013-05-17 20:03:54
【问题描述】:
我有一个简单的 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
有什么想法吗? 提前致谢
【问题讨论】:
-
请发布堆栈跟踪以获得真正的帮助。
-
你捕捉到异常了吗?你应该:
try { ... } catch(Exception e){ e.printStackTrace(); } -
顺便问一下,你不需要一个端口来连接你的数据库吗?看起来
jdbc:postgresql://localhost/db需要它。 -
@LuiggiMendoza:你不需要端口。如果未指定任何内容,驱动程序将使用默认端口。但是如果没有实际的错误消息,这个问题是不可能回答的。
-
@MichaelBarSinai:看看这些答案:stackoverflow.com/search?q=postgres+osx+connection+problem
标签: java macos postgresql jdbc