pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Unable to compile java file with all Classpaths set
3+ messages / 3 participants
[nested] [flat]

* Unable to compile java file with all Classpaths set
@ 2007-06-22 19:40 Kareem Habib <kareem.habib@gmail.com>
  2007-06-22 19:49 ` Re: Unable to compile java file with all Classpaths set Jeff Hubbach <jeff.hubbach@cha.com>
  2007-06-22 19:53 ` Re: Unable to compile java file with all Classpaths set Heikki Linnakangas <heikki@enterprisedb.com>
  0 siblings, 2 replies; 3+ messages in thread

From: Kareem Habib @ 2007-06-22 19:40 UTC (permalink / raw)
  To: pgsql-jdbc

I'm in a bit of a dilemma. I need to connect to a PostgreSQL database
through JAVA and so naturally I checked out the JDBC drivers. I downloaded
the JDBC 3 driver (both 8.2 and the 8.3dev) as I am using JDK 1.5. I set the
CLASSPATH in the environment variables to C:\postgresql.jar (I renamed the
one i was using to this). I then wrote a test file that will make sure that
I've set up everything correctly. I found this sample on the net so I
decided to use it... I only wanted it to compile, nothing more.

import java.sql.*;

public class maxi{
    public static void main(String[] args){
        Class.forName("org.postgresql.Driver");

        String url = "jdbc:postgresql://server8.cs.uofs.edu/";
        String dbname = "uofsis";
        String usernm = "bi";
        String passwd = "";
        Connection db = DriverManager.getConnection(url+dbname, usernm,
passwd);

        Statement st = db.createStatement();

        String sql = "SELECT name, title " + "FROM faculty f, course c " +
"WHERE f.id = c.instructor";
        ResultSet rs = st.executeQuery(sql);

        st.close();
        db.close();
        }
    }

Every time I compile it I get the following output:

C:\>javac maxi.java
maxi.java:5: unreported exception java.lang.ClassNotFoundException; must be
caug
ht or declared to be thrown
                Class.forName("org.postgresql.Driver");
                             ^
maxi.java:11: unreported exception java.sql.SQLException; must be caught or
decl
ared to be thrown
            Connection db = DriverManager.getConnection(url+dbname, usernm,
pass
wd);
                                                       ^
maxi.java:13: unreported exception java.sql.SQLException; must be caught or
decl
ared to be thrown
            Statement st = db.createStatement();
                                             ^
maxi.java:16: unreported exception java.sql.SQLException; must be caught or
decl
ared to be thrown
            ResultSet rs = st.executeQuery(sql);
                                          ^
maxi.java:18: unreported exception java.sql.SQLException; must be caught or
decl
ared to be thrown
            st.close();
                    ^
maxi.java:19: unreported exception java.sql.SQLException; must be caught or
decl
ared to be thrown
            db.close();
                    ^
6 errors

I've tried versions 1.5 and 1.6 of the JDK on Windows XP, Windows Vista, and
Ubuntu Linux using JDBC 3 and JDBC 4 drivers for 8.0, 8.2, and 8.3dev and
they all output the same errors. I also tried writing it using NetBeans and
importing the jar file into its libraries for the test project I created.
The same errors appeared. What am I missing here?? Perhaps somebody can
help.

Thanks
Kareem

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Unable to compile java file with all Classpaths set
  2007-06-22 19:40 Unable to compile java file with all Classpaths set Kareem Habib <kareem.habib@gmail.com>
@ 2007-06-22 19:49 ` Jeff Hubbach <jeff.hubbach@cha.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Jeff Hubbach @ 2007-06-22 19:49 UTC (permalink / raw)
  To: pgsql-jdbc

Kareem,

The errors are telling you exactly what is wrong. Your ³sample² from the net
is bad. There are method calls in the code sample you posted that throw
checked exceptions. You either need to wrap these in a try/catch block and
explicitly catch them, or specify that your main() method throws them.

If you just want to get it working, change your main() declaration line to:
  
  public static void main(String[] args) throws Exception {

-- Jeff

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Unable to compile java file with all Classpaths set
  2007-06-22 19:40 Unable to compile java file with all Classpaths set Kareem Habib <kareem.habib@gmail.com>
@ 2007-06-22 19:53 ` Heikki Linnakangas <heikki@enterprisedb.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Heikki Linnakangas @ 2007-06-22 19:53 UTC (permalink / raw)
  To: Kareem Habib <kareem.habib@gmail.com>; +Cc: pgsql-jdbc

Kareem Habib wrote:
> Every time I compile it I get the following output:
> 
> C:\>javac maxi.java
> maxi.java:5: unreported exception java.lang.ClassNotFoundException; must be
> caug
> ht or declared to be thrown
>                Class.forName("org.postgresql.Driver");
> ...                            ^

Please read

http://java.sun.com/docs/books/tutorial/essential/exceptions/

-- 
   Heikki Linnakangas
   EnterpriseDB   http://www.enterprisedb.com



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2007-06-22 19:53 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-06-22 19:40 Unable to compile java file with all Classpaths set Kareem Habib <kareem.habib@gmail.com>
2007-06-22 19:49 ` Jeff Hubbach <jeff.hubbach@cha.com>
2007-06-22 19:53 ` Heikki Linnakangas <heikki@enterprisedb.com>

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