#1
  1. No Profile Picture
    Mentat of IX
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Bucuresti / Toronto
    Posts
    112
    Rep Power
    12

    Arrow problems with classpath and loading postgresql jdbc driver


    Hi everyone ,

    I am trying to access a postgresql database with some java code , and i've ran into a little problem setting eveything up .

    I got the postgresql.jar file and added it's location to the classpath , however , once I try to run the program I get this : java.lang.ClassNotFoundException: postgresql.jar
    at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:140)
    at SimpleConnection.main(SimpleConnection.java:23)

    any ideeas on what could be wrong ?

    This is the firts time i've set the classpath , so was there anything in the default one i shold add to the curent one ?


    thanks in advance
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Are you running it in a web server or an application? App servers don't use the classpath environment variable. You must put library jar files in WEB-INF/lib.

    Also, it looks as if it is trying to find a class called postgresql.jar. I suspect your code may be wrong. Can you post it?
  4. #3
  5. No Profile Picture
    Mentat of IX
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Bucuresti / Toronto
    Posts
    112
    Rep Power
    12
    This is my first time trying to get it right , so please bare my unevolved questions . Here is my simple code trying to connect for my first time :
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    public class SimpleConnection {
    static public void main(String args[]) {
    Connection connection = null;

    try { // load the driver
    Class.forName("postgresql.jar").newInstance( );
    }
    catch( Exception e ) {
    e.printStackTrace();
    return;
    }
    try {
    connection = DriverManager.getConnection("localhost.testdb" , "user" , "secret" );
    System.out.println("Connection successful!");
    }
    catch( SQLException e ) {
    e.printStackTrace( );
    }
    finally {
    if( connection != null ) {
    try { connection.close( ); }
    catch( SQLException e ) {
    e.printStackTrace( );
    }
    }
    }
    }
    }



    yes , i'm sure thre are some mistakes form me there too , but that's what I could understand from my documentation so far ... this is not a web application , just something i'm trying to run i nthe command prompt to test some things out
    Last edited by zapa; April 23rd, 2003 at 09:25 PM.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    8
    Rep Power
    0
    try { // load the driver
    Class.forName("org.postgresql.Driver").newInstance( );
    }

    That might be it?

    If so, running the code via command line will use cp but if from
    app servers classloader will look in use the application WEB-INF/lib first and then not there it will then check app server wide repository which is <server root>/lib, I think been awhile.

    and check out http://squirrel-sql.sourceforge.net/ which will use
    the env variable. Real nice rich client.

    maybe someone can kindly point out the best data access layer for java I suspect jboss has the best strategy?

    Corrections welcome.


  8. #5
  9. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    !zeBlSuDr (how do you pronounce that?) hit it on the head, more or less. In your Class.forName() you must put the fully qualified (package included) name of the driver class, not the jar file. The way you are running it (from main) you WILL need the jar file in your classpath. This can be done by setting the environment variable or passing it in when you run the program.
    Here is a page which shows one of the ways to set the classpath environment variable in windows. http://developer.java.sun.com/develo...1/windows.html

    You can also do this at run time with the -cp switch of the 'java' command. Type in 'java' at a command prompt and you should get a list of switches and explanation. If you are not familiar with this, just ignore it and do it the other way.

    I am assuming that "org.postgresql.Driver" is the correct classname for the driver as I have not checked. Try it and see.

    JDBC tutorial
    Last edited by Nemi; April 24th, 2003 at 09:53 AM.
  10. #6
  11. No Profile Picture
    Mentat of IX
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Bucuresti / Toronto
    Posts
    112
    Rep Power
    12
    thanks for the tip

    yes , that was the right sintax .

    right now however , I am getting errors because of my URL sintax , i think ...

    once again , i havn't been able to find any clear examples on how I should do this . I've read the docs at sun about connecting to a database and I tried this in my code :

    try {
    connection = DriverManager.getConnection("jdbcostgresql:database" , "user" , "pass" );

    where i understood that postgresql has to be the protocol database is the db name i'm trying to access , user is the user and pass it's it's passwd

    upon compiling the code in my previous post with these minour changes the driver seems to be accepted but i get connection refused .

    EDIT : the classpath variable was fine all along , i just had the wrong driver name
  12. #7
  13. No Profile Picture
    Mentat of IX
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Bucuresti / Toronto
    Posts
    112
    Rep Power
    12

    success :)


    I'd like to thank both of you for the help .
    My problem here was understanding a few concepts which were not properly explained in the documentation i've went through before .

    So after visiting the tutorial Nemi mentioned , the fact that the URL usage should be described in the drivers documentation was brought to my attention

    I then googled for the postgresql documentation and determined that i had to setup my postmaster to listen on TCP/IP ports , and after a restart of postgresql :

    jtest# java SimpleConnection
    Connection successful!

IMN logo majestic logo threadwatch logo seochat tools logo