pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mykola Dudar <dmykola@ahpcc.unm.edu>
To: michel verplancke <michel.verplancke@wanadoo.be>
To: pgsql-jdbc@postgresql.org
Subject: Re: jdbc connection
Date: Fri, 24 Jan 2003 12:00:28 -0700
Message-ID: <001901c2c3da$dc8cff70$a8f41881@MINIME> (raw)
References: <000801c2bfed$76d489f0$010210ac@principalb>

Thats my code - works perfectly on WXP.

import java.sql.*;

public class tst_jdbc 
{
  public static void start ()
       throws SQLException
  {
    // Load the JDBC driver
    DriverManager.registerDriver(new org.postgresql.Driver());

    String url = "jdbc:postgresql://COMPUTERNAME/DATABASENAME";
    String userName = "USERNAME";
    String password = "PASSWORD";

    Connection conn =
      DriverManager.getConnection (url, userName, password);

    // Create a Statement
    Statement stmt = conn.createStatement ();

    // Select the all rows
    ResultSet rset = stmt.executeQuery ("select * from TABLENAME"); 

    while (rset.next ())
      System.out.println (rset.getString (1));
  } // start();
  public static void main (String args [])
       throws SQLException
  {
    start ();
  }
}
  ----- Original Message ----- 
  From: michel verplancke 
  To: pgsql-jdbc@postgresql.org 
  Sent: Sunday, January 19, 2003 12:03 PM
  Subject: [JDBC] jdbc connection


  Please can you help me

  I try to connect a java application from a windows W2K to a Linux postgresql with a very simple query, which is "select * from table".

  The error message is "connection is closed, operation is not permitted"

  here is the class definition

   import java.sql.*;
   
  public class JdbcQuery {
      private Connection con = null;
      public int recordnb = 0;
      private Statement stmt = null;
      private ResultSet result = null;

      private String driver = "org.postgresql.Driver";
      private String url = "jdbc:postgresql://COMPUTERNAME:5432/DATABASE";
      private String username = "michels";
      private String password = "";

  boolean ConnectPostgresql(String Valquery) { 
   try   // Attempt to load the JDBC driver
       {     
       Class.forName(driver);
       } 
      catch( Exception e )  // error
       {
    JOptionPane.showMessageDialog(null, "Failed to load the following JDBC driver : " + e.getMessage(), "Error ! ", JOptionPane.PLAIN_MESSAGE);
    return false;
        } 
      try
       {
    con = DriverManager.getConnection ( url, username, password);
    stmt = con.createStatement();
       }
      catch ( Exception e)
       {
     JOptionPane.showMessageDialog(null, "Problems connecting to " + url + ":" + e.getMessage(), "Error ! ", JOptionPane.PLAIN_MESSAGE);
    return false;
     }
        if( con != null)
         {
           try { con.close(); }
           catch( Exception e2 ) {}
        } 
      try
       {
    result = stmt.executeQuery(Valquery);           IT's HERE THAT I GET THE PROBLEM !!!!!!!
       } 
      catch (Exception e)  
    {
     JOptionPane.showMessageDialog(null, "Problems with resultset : " + e.getMessage(), "Error ! ", JOptionPane.PLAIN_MESSAGE);
    return false;
    }
   return true;
    }

  int GetRecordnb() 
   {
       try
       {
    result.last();
    recordnb = result.getRow();
    result.beforeFirst();

       } // end try
      catch ( Exception e)
       {
          System.err.println( "problems in GetRecordnb managing resultset" );
          System.err.println( e.getMessage() );
        if( con != null)
         {
           try { con.close(); }
           catch( Exception e2 ) {}
        } 
       } // end catch 
    return recordnb;
   }

  // alternative (donne le même résultat que la manière précédente)

  // System.out.println(result.getFetchSize()); 

  ResultSet GetRecordData() 
   {
   return result;
   }

  void CloseConnection()
   {
    try { stmt.close(); }
          catch( Exception e ) {}
          try { con.close(); }
          catch( Exception e ) {}
         } 

  } // end class 


  If I try the same query from the Cygwin psql it works !!

  Does somebody have an idea ?

  Thanks

  michel.verplancke@wanadoo.be
    

view thread (19+ messages)  latest in thread

Message-ID: <001901c2c3da$dc8cff70$a8f41881@MINIME>
Permalink:  ../001901c2c3da$dc8cff70$a8f41881@MINIME/
Also on:    postgresql.org/message-id/001901c2c3da$dc8cff70$a8f41881@MINIME

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: dmykola@ahpcc.unm.edu, michel.verplancke@wanadoo.be
  Subject: Re: jdbc connection
  In-Reply-To: <001901c2c3da$dc8cff70$a8f41881@MINIME>
  References: <000801c2bfed$76d489f0$010210ac@principalb> <001901c2c3da$dc8cff70$a8f41881@MINIME>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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