Thread: Problem with Java-SQL connection, postgresql.conf file

Problem with Java-SQL connection, postgresql.conf file

From
"Christopher Bruhn"
Date:
Hallo,
 
I want to establish a connection to an SQL Database from a
Java Applet which is situated on the same server. I used the
interface postgresql-8.2dev-503.jdbc3.jar. I use
NetBeans IDE 5.0 (Sum Microsystems) for the development of
Java Code. I copied postgresql-8.2dev-503.jdbc3.jar into the
build/classes folder of the project containing the Applet
and made it accessible for Compile and Run as a Library of
the project
(Project -> Properties -> Libraries -> added
postgresql-8.2dev-503.jdbc3.jar to Run and Compile). I also
added postgresql-8.2dev-503.jdbc3.jar as a driver
(Runtime -> Databases -> Drivers -> Add Driver -> PostgreSQL
(v7.0 and later) is accessible).
Moreover I unpacked the jar file into the build/classes folder
and uploaded all files contained in the build folder to the
server (SERVER_SOFTWARE: Apache/2.0.54 (Fedora)).
 
I used essentially the following Java code (Java 1.5.0_07)...
 
////////////////////////////
import java.applet.Applet;
import java.sql.*;
 
public class O17 extends Applet implements Runnable {
    private Thread worker;
 
    public synchronized void start() {
 if (worker == null) { 
            worker = new Thread(this);
     worker.start();
 }
    }
   
    public void run() {
 String url = "jdbc:postgresql://localhost/<DB>";
  
 try {
     Class.forName("org.postgresql.Driver");
 } catch(Exception ex) {
     setError("Can't find Database driver class: " + ex);
     return;
 }
 
 try {
     Connection con = DriverManager.getConnection(url,
      rightuser, rightpassword);
     Statement stmt = con.createStatement();
     stmt.executeUpdate(createTableCoffees);
    
     stmt.close();
     con.close();
 
 } catch(SQLException ex) {
     setError("SQLException: " + ex);
 }
    } 
}
////////////////////////////
 
... and different variations of the url which produced the following error codes:
 
jdbc:postgresql://localhost:5432/<DB>
jdbc:postgresql://localhost/<DB>
jdbc:postgresql://<DB>
->org.postgresql.util.PSQLException: Etwas Ungewöhnliches ist passiert,
das den Treiber fehlschlagen liess. Bitte teilen Sie diesen Fehler mit.
 
jdbc:postgresql://molekuelkueche.de/usr_web593_1
->org.postgresql.util.PSQLException: Der Verbindungsversuch schlug fehl.
(SERVER_NAME = www.molekuelkueche.de)
 
Moreover I searched for the postgresql.conf file but I could not find it
among the files extracted from postgresql-8.2dev-503.jdbc3.jar.
 
Can you tell me if I use the Driver in a wrong way, where I can find
the postgresql.conf file and if I need to make changes in this file?
 
Thank you very much for your help!
With kind regards,
Christopher Bruhn

Re: Problem with Java-SQL connection, postgresql.conf file

From
Roland Walter
Date:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Christopher Bruhn schrieb:
> Hallo,
>
> I want to establish a connection to an SQL Database from a
> Java Applet which is situated on the same server. I used the
> interface postgresql-8.2dev-503.jdbc3.jar. I use
> NetBeans IDE 5.0 (Sum Microsystems) for the development of
> Java Code. I copied postgresql-8.2dev-503.jdbc3.jar into the
> build/classes folder of the project containing the Applet
> and made it accessible for Compile and Run as a Library of
> the project
> (Project -> Properties -> Libraries -> added
> postgresql-8.2dev-503.jdbc3.jar to Run and Compile). I also
> added postgresql-8.2dev-503.jdbc3.jar as a driver
> (Runtime -> Databases -> Drivers -> Add Driver -> PostgreSQL
> (v7.0 and later) is accessible).
> Moreover I unpacked the jar file into the build/classes folder
> and uploaded all files contained in the build folder to the
> server (SERVER_SOFTWARE: Apache/2.0.54 (Fedora)).
>
> I used essentially the following Java code (Java 1.5.0_07)...
>
> ////////////////////////////
> import java.applet.Applet;
> import java.sql.*;
>
> public class O17 extends Applet implements Runnable {
>     private Thread worker;
>
>     public synchronized void start() {
>  if (worker == null) {
>             worker = new Thread(this);
>      worker.start();
>  }
>     }
>
>     public void run() {
>  String url = "jdbc:postgresql://localhost/<DB>";
>
>  try {
>      Class.forName("org.postgresql.Driver");
>  } catch(Exception ex) {
>      setError("Can't find Database driver class: " + ex);
>      return;
>  }
>
>  try {
>      Connection con = DriverManager.getConnection(url,
>       rightuser, rightpassword);
>      Statement stmt = con.createStatement();
>      stmt.executeUpdate(createTableCoffees);
>
>      stmt.close();
>      con.close();
>
>  } catch(SQLException ex) {
>      setError("SQLException: " + ex);
>  }
>     }
> }
> ////////////////////////////
>
> ... and different variations of the url which produced the following
> error codes:
>
> jdbc:postgresql://localhost:5432/<DB>
> jdbc:postgresql://localhost/<DB>
> jdbc:postgresql://<DB>
> ->org.postgresql.util.PSQLException: Etwas Ungewöhnliches ist passiert,
> das den Treiber fehlschlagen liess. Bitte teilen Sie diesen Fehler mit.
>
> jdbc:postgresql://molekuelkueche.de/usr_web593_1
> ->org.postgresql.util.PSQLException: Der Verbindungsversuch schlug fehl.
> (SERVER_NAME = www.molekuelkueche.de <http://www.molekuelkueche.de>)
>
> Moreover I searched for the postgresql.conf file but I could not find it
> among the files extracted from postgresql-8.2dev-503.jdbc3.jar.
>
> Can you tell me if I use the Driver in a wrong way, where I can find
> the postgresql.conf file and if I need to make changes in this file?
>

It looks as if your database is not correct configured for a connection
over tcp/ip. The file postgresql.conf is in the directory of the
database. There you have to comment out the line with the
'listen_adresses' and give it the correct parameters. If that looks
correct you have to configure the access to the database in the file
pg_hba.conf in the same directory. Consider the handbook for the correct
parameters.

Regards,
Roland.

- --
Dipl.-Phys. Roland Walter
mailto: roland (dot) walter (dot) rwa (at) gmx (dot) net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFFCdyyxoOEAkary1ERAlxUAJ9DnGuNiS+UweU6ZC11FzsyZsT5ZwCcDIaE
hSzchqLHij86uKh2iBxayc0=
=cuI5
-----END PGP SIGNATURE-----

Re: Problem with Java-SQL connection, postgresql.conf file

From
Markus Schaber
Date:
Hi, Christopher,

Christopher Bruhn wrote:

> Moreover I searched for the postgresql.conf file but I could not find it
> among the files extracted from postgresql-8.2dev-503.jdbc3.jar.

> Can you tell me if I use the Driver in a wrong way, where I can find
> the postgresql.conf file and if I need to make changes in this file?

The postgresql.conf file is the main configuration file for the
PostgreSQL server. The exact place of installation depends on how the
Postmaster itsself is installed, but it is likely to be found in /etc or
the postgresql main cluster directory on unices.

If your postmaster is set up to only accept connections via unix
sockets, but not via TCP, then you have to change postgresql.conf and
pg_hba.conf. As all is on the same server, make him listen on 127.0.0.1
and allow access from there.

Btw, for testing, you can use command line access (if you have) and the
psql utility. If you can connect via "psql usr_web593_1", but not via
"psql -h localhost -p 5432 usr_web593_1" then postgresql does not accept
tcp connections via localhost.

Btw, it might also be that the server and client disagree on mandatory
SSL usage, but that likely leads to different exceptions.

Markus

--
Markus Schaber | Logical Tracking&Tracing International AG
Dipl. Inf.     | Software Development GIS

Fight against software patents in Europe! www.ffii.org
www.nosoftwarepatents.org