Problems with JDBC and Postgres


Subject: Problems with JDBC and Postgres
From: Jeremy Smith (Jeremy@justasofty.com)
Date: Sat Feb 24 2001 - 13:04:09 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?

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());
   }
 }
}

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



This archive was generated by hypermail 2a24 : Sat Feb 24 2001 - 13:08:24 MST