Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JDBC Connect Error Postgresql

613 views
Skip to first unread message

Chase Preuninger

unread,
May 26, 2008, 7:53:48 AM5/26/08
to
I started my Postgres DBMS and I can connect to it through the
services window in Netbeans but I can seem to connect using code.
Also I do have the dirver int the class path which passes the
Class.forName("driver"); test.

System.setProperty("jdbc.drivers", "org.postgresql.Driver");
Connection conn = DriverManager.getConnection("jdbc:postgresql:////
127.0.0.1//Notes", "myun", "mypw");

Exception in thread "main" org.postgresql.util.PSQLException: The
connection attempt failed.
at
org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:
137)
at
org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:
66)
at
org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:
124)
at
org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:
30)
at
org.postgresql.jdbc3.Jdbc3Connection.<init>(Jdbc3Connection.java:24)
at org.postgresql.Driver.makeConnection(Driver.java:386)
at org.postgresql.Driver.connect(Driver.java:260)
at java.sql.DriverManager.getConnection(DriverManager.java:
582)
at java.sql.DriverManager.getConnection(DriverManager.java:
185)
at
com.cpsoft.mynotes.database.accounts.DatabaseAccount.main(DatabaseAccount.java:
17)
Caused by: java.net.UnknownHostException: /
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:180)
at org.postgresql.core.PGStream.<init>(PGStream.java:62)
at
org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:
77)
... 9 more
Java Result: 1

Chase Preuninger

unread,
May 26, 2008, 7:58:31 AM5/26/08
to
I think I found the problem because it works if the database URL is
"jdbc:postgresql:Notes" but why?

Lew

unread,
May 26, 2008, 8:53:55 AM5/26/08
to
Chase Preuninger wrote:
> I think I found the problem because it works if the database URL is
> "jdbc:postgresql:Notes" but why?

Your original syntax shows way too many slash characters.

> Connection conn = DriverManager.getConnection(
> "jdbc:postgresql:////127.0.0.1//Notes", "myun", "mypw");

I suspect this:


> System.setProperty("jdbc.drivers", "org.postgresql.Driver");

I've never done such a thing with JDBC code before. Initializing the driver
with Class.forName(), which is NOT a "test", btw, but how you initialize the
driver, has always sufficed.

Setting a System property in code is where you went wrong. The whole point of
System properties is that they are set by the *system*, and then read in code.
The typical approach to setting them is a "-D" parameter on the java command
line. This is, in fact, how you're supposed to use this particular property.
According to the documentation, it is used so that
> the JVM will attempt to load the driver as part of its initialization.
<http://jdbc.postgresql.org/documentation/83/load.html>

That means that since you didn't provide the value at the right time, it
wasn't available *during initialization*.

Just use Class.forName() to load the driver.

Try the correct number of slashes. I don't think the extra one after the IP
address hurts, but I really don't know about the one after the protocol. The
documentation sure doesn't indicate the use of four slashes.

You could also try specifying the port if you aren't using the 5432 default.

--
Lew

Lew

unread,
May 26, 2008, 8:55:02 AM5/26/08
to
Lew wrote:
> Try the correct number of slashes. I don't think the extra one after
> the IP address hurts, but I really don't know about the one after the
> protocol. The documentation sure doesn't indicate the use of four slashes.

Also, Chase, you've been around long enough to know better:

Provide an SSCCE for God's sake!

--
Lew

Arne Vajhøj

unread,
May 26, 2008, 9:19:25 AM5/26/08
to
Chase Preuninger wrote:
>>Connection conn = DriverManager.getConnection("jdbc:postgresql:////127.0.0.1//Notes", "myun", "mypw");

> I think I found the problem because it works if the database URL is
> "jdbc:postgresql:Notes" but why?

Connection conn =
DriverManager.getConnection("jdbc:postgresql://localhost/Notes", "myun",
"mypw");

should work. And you will need to specify the host if it
is not localhost.

Arne


Chase Preuninger

unread,
May 26, 2008, 11:38:02 AM5/26/08
to
Also I put in the extra slashes because it was a string literal so
therefore every two slashes would become just one slash.

Daniele Futtorovic

unread,
May 26, 2008, 11:59:16 AM5/26/08
to
On 2008-05-26 17:38 +0100, Chase Preuninger allegedly wrote:
> Also I put in the extra slashes because it was a string literal so
> therefore every two slashes would become just one slash.

BACKslashes.

If it doesn't work with two slashes, check whether eth0 is up.

--
DF.
to reply privately, change the top-level domain
in the FROM address from "invalid" to "net"

Arne Vajhøj

unread,
May 26, 2008, 2:27:06 PM5/26/08
to
Chase Preuninger wrote:
> Also I put in the extra slashes because it was a string literal so
> therefore every two slashes would become just one slash.

That is necessary for \ not for /.

Arne

0 new messages