Re: Problems with JDBC and Postgres


Subject: Re: Problems with JDBC and Postgres
From: Peter M. Bagnall (pete@surfaceeffect.com)
Date: Sun Feb 25 2001 - 02:54:11 MST


>Experimenting with my first steps into Java on my Yellowdog 1.2 system,
>everything seems OK,
>I have succesfuly run a number of examples but when I try to access a
>Postgres database based on the examples supplied I cannot get javac to
>compile my code.
>Can anyone help?

It just looks like buggy Java to me. I would expect it to complain
about those three exceptions not being caught. I guess they never
tested this or it was just meant as a code snippet to show the idea,
not as "real" code.

Check out the documentation for the Class class in Java, specifically
the newInstance() method and I think you'll see it is doing what it's
meant to.

Adding these lines should fix it... see below...

I'd be interested to know how this goes, cause I want to install
Postgres and JDBC at some point, so if you have any tips I'd love to
hear them!

Cheers

Pete

>Using Blackdown Java Linux version 1.2.2
>libc 2.1.3
>Postgres 6.5.3
>
>PATH = /usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/include/pgsql/bin
> :/usr/local/jdk1.2.2/bin:/usr/lib/pgsql
>CLASSPATH =.:/usr/lib/pgsql/jdbc6.5-1.2.jar
>(the jar file does exist here and it does contain a postgresql/Driver.class)
>
>Code trying to compile using javac PostgreSQLTest.java
>(Have also tried -classpath /usr/lib/pgsql/jdbc6.5-1.2.jar javac
>PostgreSQLTest.java with the same results)
>
>import java.sql.*;
>
>class PostgreSQLTest {
> public static void main (String[] args) {
> try {
> Driver driver = (Driver)
>Class.forName("postgresql.Driver").newInstance();
> DriverManager.registerDriver(driver);
>
> String url = "jdbc:postgresql:history";
> Connection con = DriverManager.getConnection(url, "postgres", "");
> Statement stm = con.createStatement();
>
> stm.setQueryTimeout(10);
> ResultSet rs = stm.executeQuery("select text from history");
>
> rs.next();
>
> System.out.println(rs.getString(1));
>
> } catch (SQLException e) {
>
> System.out.println("Exception!");
> System.out.println(e.toString());

     } catch (ClassNotFoundException cnfe) {
        System.out.println("ClassNotFoundException");
     } catch (IllegalAccessException iae) {
        System.out.println("IllegalAccessException");
     } catch (InstantiationException ie) {
        System.out.println("InstantiationException");

> }
> }
>}
>
>Get the following errors:
>
>PostgreSQLTest.java:6: Exception java.lang.ClassNotFoundException must be
>caught, or it must be declared
> in the throws clause of this method.
> Driver driver = (Driver)
>Class.forName("postgresql.Driver").newInstance();
>
>PostgreSQLTest.java:6: Exception java.lang.IllegalAccessException must be
>caught, or it must be declared
> in the throws clause of this method.
> Driver driver = (Driver)
>Class.forName("postgresql.Driver").newInstance();
>
>PostgreSQLTest.java:6: Exception java.lang.InstantiationException must be
>caught, or it must be declared
> in the throws clause of this method.
> Driver driver = (Driver)
>Class.forName("postgresql.Driver").newInstance();
>
>3 errors
>
>Jeremy

-- 
Peter M. Bagnall
pete@surfaceeffect.com - http://www.surfaceeffect.com/



This archive was generated by hypermail 2a24 : Sun Feb 25 2001 - 02:58:41 MST