Thread: [Fwd: postgresql JDBC driver question]

[Fwd: postgresql JDBC driver question]

From
Murad Nayal
Date:
I am resubmitting this since my subscription to pgsql-interfaces didn't
seem to go through the first time around. If you submitted a suggestion in
response, please submit it again or forward it directly to me at
mailto:murad@godel.bioc.columbia.edu

thank you very much in advance

Murad Nayal wrote:

> I am baffled:
>
> I wrote a small java program to test postgresql jdbc driver. the applet
> runs fine locally using applet viewer. indicating that the driver is
> recognizing the url and responding to it correctly. however I get the
> message "NO suitable Driver" when the applet is run from a remote
> browser. the Class.forName("postgresql.Driver"); does not produce an
> exception leading me to believe that the Driver is found but it is not
> accepting the url (same one that worked locally)?
>
> code
>
>   String url =
> "jdbc:postgresql://godel.bioc.columbia.edu:1212/stockprices";
>   String usr = "murad";
>   String pwd = "*******";
>
>     try {
>
>       Class.forName("postgresql.Driver");
>       con = DriverManager.getConnection(url, usr, pwd); // <- this
> generates the problem
>       stmt = con.createStatement();
>
>     } catch (Exception e) {
>
> part of the html file:
> <body>
>
> Enter one of the following company symbols:
> SGI, CCI, CMB. A bit of a small database I know.
>
> <Applet code="JDBCtest.class" archive=postgresql.jar width=300
> height=300> </Applet>
>
> Thanks for the help



--
Murad Nayal M.D. Ph.D.
Department of Biochemistry and Molecular Biophysics
College of Physicians and Surgeons of Columbia University
630 West 168th Street. New York, NY 10032
Tel: 212-305-6884       Fax: 212-305-6926




Re: [INTERFACES] [Fwd: postgresql JDBC driver question]

From
Peter T Mount
Date:
On Wed, 28 Oct 1998, Murad Nayal wrote:

> > I am baffled:
> >
> > I wrote a small java program to test postgresql jdbc driver. the applet
> > runs fine locally using applet viewer. indicating that the driver is
> > recognizing the url and responding to it correctly. however I get the
> > message "NO suitable Driver" when the applet is run from a remote
> > browser. the Class.forName("postgresql.Driver"); does not produce an
> > exception leading me to believe that the Driver is found but it is not
> > accepting the url (same one that worked locally)?

Ok, applets can be horrible little things, and sometimes it's not their
fault. Nine times out of ten it's the sandbox (applet security), or a
browser not handling java 1.1 or jdbc (some don't know about jdbc :-( )

Your code looks ok. I placed your URL into an application here, and all I
got was:

Connection failed: java.net.UnknownHostException:  godel.bioc.columbia.edu

which is ok, as I wasn't connected at the time ;-)

> > <Applet code="JDBCtest.class" archive=postgresql.jar width=300
> > height=300> </Applet>

As far as I can see, your problem is here.

First, don't put .class in the code argument. It can break some browsers.
Technically it's the class name that goes here, not the file name.

The main problem is the archive argument. The browser is loading the
postgresql.jar file, looking for the JDBCtest class. It fails, so it then
looks at the directory that the html file is in. It find's JDBCtest, but
as it's not in the jar file, some browsers implementations of the
"Sandbox" prevents the driver to be found.

There are two solutions:

    extract the files from postgresql.jar into your html directory

or

    create a new jar file containing the contents of postgresql.jar
    and your applet.

Remember: it's important to keep the directory structure.

> > Thanks for the help

Hope that helps.

Peter

--
       Peter T Mount peter@retep.org.uk
      Main Homepage: http://www.retep.org.uk
PostgreSQL JDBC Faq: http://www.retep.org.uk/postgres
 Java PDF Generator: http://www.retep.org.uk/pdf


Re: [INTERFACES] [Fwd: postgresql JDBC driver question]

From
Murad Nayal
Date:
Peter T Mount wrote:

> On Wed, 28 Oct 1998, Murad Nayal wrote:
>
> > > I am baffled:
> > >
> > > I wrote a small java program to test postgresql jdbc driver. the applet
> > > runs fine locally using applet viewer. indicating that the driver is
> > > recognizing the url and responding to it correctly. however I get the
> > > message "NO suitable Driver" when the applet is run from a remote
> > > browser. the Class.forName("postgresql.Driver"); does not produce an
> > > exception leading me to believe that the Driver is found but it is not
> > > accepting the url (same one that worked locally)?
>
> Ok, applets can be horrible little things, and sometimes it's not their
> fault. Nine times out of ten it's the sandbox (applet security), or a
> browser not handling java 1.1 or jdbc (some don't know about jdbc :-( )
>
> Your code looks ok. I placed your URL into an application here, and all I
> got was:
>
> Connection failed: java.net.UnknownHostException:  godel.bioc.columbia.edu
>
> which is ok, as I wasn't connected at the time ;-)
>
> > > <Applet code="JDBCtest.class" archive=postgresql.jar width=300
> > > height=300> </Applet>
>
> As far as I can see, your problem is here.
>
> First, don't put .class in the code argument. It can break some browsers.
> Technically it's the class name that goes here, not the file name.
>
> The main problem is the archive argument. The browser is loading the
> postgresql.jar file, looking for the JDBCtest class. It fails, so it then
> looks at the directory that the html file is in. It find's JDBCtest, but
> as it's not in the jar file, some browsers implementations of the
> "Sandbox" prevents the driver to be found.
>
> There are two solutions:
>
>         extract the files from postgresql.jar into your html directory
>
> or
>
>         create a new jar file containing the contents of postgresql.jar
>         and your applet.
>
> Remember: it's important to keep the directory structure.

Thank you for your suggestions. I followed your directions by unzipping the
jar file in the html directory, removed the archive attribute from the
<applet> tag and remove the class extension from the code attribute in the
applet tag. Now netscape produces: user authentication failed exception while
internet explorer produces No suitable driver exception. Mind you, the
connection works just fine if you do it using the appletviewer locally on the
machine. I am including next a copy of the html and the java files with just
the user password obsecured (you have to trust me it does work :-)).

thanks indeed for any suggestions and help


URL:
http://godel.bioc.columbia.edu/~secourse/JDBCtest.html

JDBCtest.html:

<html>
<head>
<title>Java Database Connectivity Test</title>
</head>
<body>

Enter one of the following company symbols:
SGI, CCI, CMB. A bit of a small database I know.

<Applet code="JDBCtest" width=300 height=300> </Applet>
</html>


JDBCtest.java

import java.sql.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;

public class JDBCtest extends Applet implements ActionListener {

  String url = "jdbc:postgresql://godel.bioc.columbia.edu:1212/stockprices";
  String usr = "murad";
  String pwd = "*******";

  Connection con;
  Statement stmt;
  ResultSet res;

  static final String DOIT = "GETPRICE";
  TextField price;
  TextField symbol;
  TextField stat;
  Label thesymbol;
  Label theprice;
  Button doit;

  public void init() {

    try {

      Class.forName("postgresql.Driver");
      System.out.println("After class loading");
      con = DriverManager.getConnection(url, usr, pwd);
      System.out.println("After connection");
      stmt = con.createStatement();

    } catch (Exception e) {
      System.out.println("Just caught an exception");
      System.out.println(e.getMessage());
      e.printStackTrace();
    }


    price     = new TextField(20);
    price.setEditable(false);

    stat      = new TextField(20);
    stat.setEditable(false);

    symbol    = new TextField(20);
    thesymbol = new Label("Stock symbol");
    theprice  = new Label("Stock price");

    doit      = new Button("Get price");
    doit.addActionListener(this);
    doit.setActionCommand(DOIT);

    setLayout(new GridLayout(3,2));
    add(thesymbol);
    add(symbol);
    add(theprice);
    add(price);
    add(doit);
    add(stat);
    validate();
  }

  public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();

    if(command == DOIT) {
      String query;
      float stockprice=0;
      String secsym = symbol.getText();
      stat.setText("Contacting database. please wait");
      try {

        query = "Select price FROM stockinfo WHERE symbol = \'";
        query += secsym + "\';";
        res = stmt.executeQuery(query);
        while(res.next()) {
          stockprice = res.getFloat("price");
        }
        price.setText(String.valueOf(stockprice));
        stat.setText("Updated price");

      } catch (Exception ee) {
        System.out.println("Just caught an exception");
        System.out.println(ee.getMessage());
        ee.printStackTrace();
      }
    }
  }

  public void destroy() {
    try {
      stmt.close();
      con.close();
    } catch (Exception ee) {
      System.out.println("Just caught an exception");
      System.out.println(ee.getMessage());
      ee.printStackTrace();
    }
  }

}


--
Murad Nayal M.D. Ph.D.
Department of Biochemistry and Molecular Biophysics
College of Physicians and Surgeons of Columbia University
630 West 168th Street. New York, NY 10032
Tel: 212-305-6884       Fax: 212-305-6926