pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Exception "The connection attempt failed." (didn't find anything elsewhere)
3+ messages / 2 participants
[nested] [flat]

* Exception "The connection attempt failed." (didn't find anything elsewhere)
@ 2005-04-08 20:26 Pedro n/a <napalmblood@hotmail.com>
  2005-04-08 23:02 ` Re: Exception "The connection attempt failed." (didn't find Oliver Jowett <oliver@opencloud.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Pedro n/a @ 2005-04-08 20:26 UTC (permalink / raw)
  To: pgsql-jdbc

Hello,

  I'm quite new at java and I was just following a tutorial when I bumped 
with a horrible exception. The program compiled smoothly, but when I runned 
it I got an error message and I wasn't able to found any track through the 
internet. So I would like to know if any of you could help me. Here is the 
error track spilled from my VM:

org.postgresql.util.PSQLException: The connection attempt failed.
org.postgresql.util.PSQLException: The connection attempt failed.
        at 
org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:136)
        at 
org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)
        at 
org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:117)
        at 
org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
        at 
org.postgresql.jdbc3.Jdbc3Connection.<init>(Jdbc3Connection.java:24)
        at org.postgresql.Driver.connect(Driver.java:235)
        at java.sql.DriverManager.getConnection(DriverManager.java:512)
        at java.sql.DriverManager.getConnection(DriverManager.java:171)
        at HelloPostgresql.<init>(HelloPostgresql.java:34)
        at HelloPostgresql.main(HelloPostgresql.java:118)
Caused by: java.net.UnknownHostException: localhost
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:153)
        at java.net.Socket.connect(Socket.java:452)
        at java.net.Socket.connect(Socket.java:402)
        at java.net.Socket.<init>(Socket.java:309)
        at java.net.Socket.<init>(Socket.java:124)
        at org.postgresql.core.PGStream.<init>(PGStream.java:58)
        at 
org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:77)
        ... 9 more

A little note: I think that attempt to connect wasn't completed because it 
didn't generate any entry in the postgresql log files.

Here's the code I compiled...

/**
* A demo program to show how jdbc works with postgresql
* Nick Fankhauser 10/25/01
* nickf@ontko.com or nick@fankhausers.com
* This program may be freely copied and modified
* Please keep this header intact on unmodified versions
* The rest of the documentation that came with this demo program
* may be found at http://www.fankhausers.com/postgresql/jdbc
*/


import java.sql.*;   // All we need for JDBC
import java.text.*;
import java.io.*;

public class HelloPostgresql
{
  Connection       db;        // A connection to the database
  Statement        sql;       // Our statement to run queries with
  DatabaseMetaData dbmd;      // This is basically info the driver delivers
                              // about the DB it just connected to. I use
                              // it to get the DB version to confirm the
                              // connection in this example.

  //CONSTRUCTOR
  public HelloPostgresql(String argv[]) throws ClassNotFoundException, 
SQLException
  {
    String database = argv[0];
    String username = argv[1];
    String password = argv[2];

    Class.forName("org.postgresql.Driver"); //load the driver

    db = DriverManager.getConnection("jdbc:postgresql:"+database,
                                     username,
                                     password); //connect to the db

    dbmd = db.getMetaData(); //get MetaData to confirm connection

    System.out.println("Connection to "+dbmd.getDatabaseProductName()+" "+
                       dbmd.getDatabaseProductVersion()+" successful.\n");

    sql = db.createStatement(); //create a statement that we can use later


    String sqlText = "create table jdbc_demo (code int, text varchar(20))";
    System.out.println("Executing this command: "+sqlText+"\n");
    sql.executeUpdate(sqlText);


    sqlText = "insert into jdbc_demo values (1,'One')";
    System.out.println("Executing this command: "+sqlText+"\n");
    sql.executeUpdate(sqlText);


    sqlText = "insert into jdbc_demo values (3,'Four')";
    System.out.println("Executing this command twice: "+sqlText+"\n");
    sql.executeUpdate(sqlText);
    sql.executeUpdate(sqlText);


    sqlText = "update jdbc_demo set text = 'Three' where code = 3";
    System.out.println("Executing this command: "+sqlText+"\n");
    sql.executeUpdate(sqlText);
    System.out.println (sql.getUpdateCount()+
                        " rows were update by this statement\n");


    System.out.println("\n\nNow demostrating a prepared statement...");
    sqlText = "insert into jdbc_demo values (?,?)";
    System.out.println("The Statement looks like this: "+sqlText+"\n");
    System.out.println("Looping three times filling in the fields...\n");
    PreparedStatement ps = db.prepareStatement(sqlText);
    for (int i=10;i<13;i++)
    {
      System.out.println(i+"...\n");
      ps.setInt(1,i);         //set column one (code) to i
      ps.setString(2,"HiHo"); //Column two gets a string
      ps.executeUpdate();
    }
    ps.close();


    System.out.println("Now executing the command: "+
                       "select * from jdbc_demo");
    ResultSet results = sql.executeQuery("select * from jdbc_demo");
    if (results != null)
    {
      while (results.next())
      {
        System.out.println("code = "+results.getInt("code")+
                           "; text = "+results.getString(2)+"\n");
      }
    }
    results.close();


    sqlText = "drop table jdbc_demo";
    System.out.println("Executing this command: "+sqlText+"\n");
    sql.executeUpdate(sqlText);


    db.close();
  }

  public static void correctUsage()
  {
    System.out.println("\nIncorrect number of arguments.\nUsage:\n "+
                       "java   \n");
    System.exit(1);
  }

  public static void main (String args[])
  {
    if (args.length != 3) correctUsage();
    try
    {
      HelloPostgresql demo = new HelloPostgresql(args);
    }
    catch (Exception ex)
    {
      System.out.println("***Exception:\n"+ex);
      ex.printStackTrace();
    }
  }
}

Thanks In Advance,
Pedro

_________________________________________________________________
MSN Messenger: converse online com seus amigos .  
http://messenger.msn.com.br




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Exception "The connection attempt failed." (didn't find
  2005-04-08 20:26 Exception "The connection attempt failed." (didn't find anything elsewhere) Pedro n/a <napalmblood@hotmail.com>
@ 2005-04-08 23:02 ` Oliver Jowett <oliver@opencloud.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Oliver Jowett @ 2005-04-08 23:02 UTC (permalink / raw)
  To: Pedro n/a <napalmblood@hotmail.com>; +Cc: pgsql-jdbc

Pedro n/a wrote:

>  I'm quite new at java and I was just following a tutorial when I bumped
> with a horrible exception. [...] 

> Caused by: java.net.UnknownHostException: localhost
>        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:153)

This is the underlying problem. If you do not specify a host to connect
to, the JDBC driver defaults to connecting to localhost, but for some
reason your system cannot resolve localhost.

The solution is to fix hostname resolution on your system. I can't give
you any suggestions as to how to do this as you didn't say anything
about the OS that you are using, but something is pretty broken if you
can't resolve localhost.

A workaround might be to provide an explicit IP address in the driver
URL, e.g. instead of 'jdbc:postgresql:databasename' use something like
'jdbc:postgresql://127.0.0.1/databasename'.

-O



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* FW: Re: Exception "The connection attempt failed." (didn't find anything
@ 2005-04-09 02:18 Pedro n/a <napalmblood@hotmail.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Pedro n/a @ 2005-04-09 02:18 UTC (permalink / raw)
  To: pgsql-jdbc

I'm ashamed, the problem was exactly there!

I'm using a FreeBSD 5.4#PRE-RELEASE box and I was editing the 'hosts' file 
the other day, experimenting some things with samba just when I forgot to 
uncomment some tests I did... Don't know where I was with my mind!!!

Thanks again!
Pedro

From: Oliver Jowett <oliver@opencloud.com>
To: Pedro n/a <napalmblood@hotmail.com>
CC: pgsql-jdbc@postgresql.org
Subject: Re: [JDBC] Exception "The connection attempt failed." (didn't find 
anything elsewhere)
Date: Sat, 09 Apr 2005 11:02:26 +1200

Pedro n/a wrote:

 >  I'm quite new at java and I was just following a tutorial when I bumped
 > with a horrible exception. [...]

 > Caused by: java.net.UnknownHostException: localhost
 >        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:153)

This is the underlying problem. If you do not specify a host to connect
to, the JDBC driver defaults to connecting to localhost, but for some
reason your system cannot resolve localhost.

The solution is to fix hostname resolution on your system. I can't give
you any suggestions as to how to do this as you didn't say anything
about the OS that you are using, but something is pretty broken if you
can't resolve localhost.

A workaround might be to provide an explicit IP address in the driver
URL, e.g. instead of 'jdbc:postgresql:databasename' use something like
'jdbc:postgresql://127.0.0.1/databasename'.

-O

_________________________________________________________________
Chegou o que faltava: MSN Acesso Grátis. Instale Já! 
http://www.msn.com.br/discador




^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2005-04-09 02:18 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-04-08 20:26 Exception "The connection attempt failed." (didn't find anything elsewhere) Pedro n/a <napalmblood@hotmail.com>
2005-04-08 23:02 ` Re: Exception "The connection attempt failed." (didn't find Oliver Jowett <oliver@opencloud.com>
2005-04-09 02:18 FW: Re: Exception "The connection attempt failed." (didn't find anything Pedro n/a <napalmblood@hotmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox