Thread: Can't connect to posgresql through the jdbc driver
Hi I'm using postgresql 8.3 and the postgresql-8.4-701.jdbc4.jar ( a jar file which is supposed to include the jdbc driver:org.postgresql.Driver) I set the CLASSPATH environment variable to the correct place and ran my JAVA code which first tried to call the jdbc driver: "Class.forName("org.postgresql.Driver");" But to no avail ("java.lang.ClassNotFoundException: org.postgresql.Driver") . At first I thought I set the CLASSPATH wrong (did it thorugh my .bashrc and exported) but after trying a few variations of CLASSPATH I opened the file postgresql-8.4-701.jdbc4.jar with the ZIP tool that came with UBUNTU. I noticed that the jdbc driver postgresql-8.4-701.jdbc4.jar was not in that bundled jar file... I did notice that there was another driver in there called "java.sql.Driver" I put it in my code (just to check the CLASSPATH) : "Class.forName("java.sql.Driver");" compiled and ran . This time it passed that line correctly (it did get stuck on the actual line that operated the JDBC URL,but at least it showed me that my CLASSPATH was right...could anyone tell me why org.postgresql.Driver could not be found in postgresql-8.4-701.jdbc4.jar...? and also if there is an answer to that , how do I make this one disappear : java.lang.ClassNotFoundException: org.postgresql.Driver When I try to call it.... Thanks : Lior K.
Lior K wrote: > Hi > I'm using postgresql 8.3 and the postgresql-8.4-701.jdbc4.jar ( a jar file which is supposed to include the jdbc driver:org.postgresql.Driver) It certainly should! If yours doesn't - where did you download it from? Please post the output of: unzip -l postgresql-8.4-701.jdbc.jar | grep Driver You should see these lines among others: 14725 2009-06-30 22:30 org/postgresql/Driver.class 22 2007-07-18 20:23 META-INF/services/java.sql.Driver > I set the CLASSPATH environment variable to the correct place and ran my JAVA code which first tried to call the jdbc driver: "Class.forName("org.postgresql.Driver");" What's the exact value of CLASSPATH as you set it? Did you set it to the directory containing the .jar ? Or to the .jar its self? You need to put the actual .jar on the CLASSPATH. > But to no avail ("java.lang.ClassNotFoundException: > org.postgresql.Driver") . At first I thought I set the CLASSPATH wrong > (did it thorugh my .bashrc and exported) but after trying a few > variations of CLASSPATH I opened the file postgresql-8.4-701.jdbc4.jar > with the ZIP tool that came with UBUNTU. > I noticed that the jdbc driver postgresql-8.4-701.jdbc4.jar was not in that bundled jar file... Of course not. postgresql-8.4-701.jdbc4.jar is the jar file containing the Driver class and all the other classes required for it to work. It can't contain its self. > I did notice that there was another driver in there called "java.sql.Driver" > I put it in my code (just to check the CLASSPATH) : "Class.forName("java.sql.Driver");" > compiled and ran . That's because you're loading the JDBC driver interface (java.sql.Driver), not an actual driver. "Class.forName" is just a call to the classloader asking it to load a particular class from a file. It's not magic, and it doesn't know what you want, only what you ask for. > This time it passed that line correctly Well, that statement ran without error, but it didn't load the PostgreSQL JDBC driver because that's not what you asked for. > (it did get stuck on the actual line that operated the JDBC URL, but at least it showed me that my CLASSPATH was right Bad assumption. It did nothing of the sort. Class.forName("java.sql.Driver"); will run without error on a stock-standard J2SE install with no added JDBC drivers. I still think your classpath is probably wrong. ( On a side note: Maybe you should look into using an IDE like NetBeans? It makes this sort of thing easier for newbies to Java. The Maven plugin, if you use that, even takes care of updating your dependencies and their dependencies for you automatically. ) -- Craig Ringer
[ Replying to list, since poster replied directly to me. I've trimmed the reply and fixed the broken quoting; please quote correctly in future replies and reply to the list, not directly to me]. Lior K wrote: >> Craig Ringer wrote: >> What's the exact value of CLASSPATH as you set it? > > from within .bashrc : > > CLASSPATH=$CLASSPATH:/opt/PostgreSQL/pgJDBC/ > export CLASSPATH > >> Did you set it to the directory containing the .jar ? Or to the .jar its >> self? You need to put the actual .jar on the CLASSPATH. > > I set it to the jar file as written above No, you didn't. You set it to the folder CONTAINING the jar. Try: CLASSPATH=$CLASSPATH:/opt/PostgreSQL/pgJDBC/postgresql-8.4-701.jdbc.jar > I am using netbeans 6.8 ... The project I chose was a java application . The application is supposed to connect to a databasewhich parameters (DB name , userName,Password ) I pass through netbeans config file option (under the "run" sectionin the menu. I also tried to run it manually with this line: Eh? If you're using NetBeans, why are you messing with the CLASSPATH environment variable? Just define libraries and add them to your netbeans project. See the netbeans introductory tutorial, the built-in help, the "Libraries" folder in the project, and the "Libraries" entry in the Tools menu. For example, I created a library "PostgreSQL JDBC Driver", and added the postgresql-8.4-701.jdbc.jar to it. If I wanted to use that in an Ant project (the default netbeans project type) I'd just right-click on the library list and add "PostgreSQL JDBC Driver" to the list. Now netbeans will add it to the classpath automatically, copy it into the dist folder when I do a project build, bundle it up in my application jar when I do a jar build, and otherwise just take care of it for me. If you add the sources and javadoc to the library too, you get source-level debuggging, JavaDoc autocomplete, etc for the library too. Right now, you're doing things the hard way! You'll get a lot further by reading the documentation in Netbeans and the basic documentation on the JVM, "java" command, etc. ( Alternately, if you use Maven, and add a dependency for: Artifact: postgresql Group: postgresql Version: 8.4-701.jdbc4 and it all automagically just happens ). -- Craig Ringer
Craig Ringer, 03.03.2010 04:20: >> I set it to the jar file as written above > > No, you didn't. You set it to the folder CONTAINING the jar. Try: > > CLASSPATH=$CLASSPATH:/opt/PostgreSQL/pgJDBC/postgresql-8.4-701.jdbc.jar > >> I am using netbeans 6.8 ... The project I chose was a java application . The application is supposed to connect to a databasewhich parameters (DB name , userName,Password ) I pass through netbeans config file option (under the "run" sectionin the menu. I also tried to run it manually with this line: > > Eh? If you're using NetBeans, why are you messing with the CLASSPATH > environment variable? Actually nowadays one should never use a system wide CLASSPATH, because it simply leads to a lot of problems. Either the IDE manages that or the classpath is defined using the -cp parameter when starting the Java program. Or - mostuser-friendly - create an executable jar file that defines all needed libraries. Thomas
Thomas Kellerer <spam_eater <at> gmx.net> writes: > > Craig Ringer, 03.03.2010 04:20: > >> I set it to the jar file as written above > > > > No, you didn't. You set it to the folder CONTAINING the jar. Try: > > > > CLASSPATH=$CLASSPATH:/opt/PostgreSQL/pgJDBC/postgresql-8.4-701.jdbc.jar > > > >> I am using netbeans 6.8 ... The project I chose was a java application . The application is supposed to > connect to a database which parameters (DB name , userName,Password ) I pass through netbeans config file > option (under the "run" section in the menu. I also tried to run it manually with this line: > > > > Eh? If you're using NetBeans, why are you messing with the CLASSPATH > > environment variable? > > Actually nowadays one should never use a system wide CLASSPATH, because it simply leads to a lot of problems. > > Either the IDE manages that or the classpath is defined using the -cp parameter when starting the Java > program. Or - most user-friendly - create an executable jar file that defines all needed libraries. > > Thomas > Using NetBeans 6.9.1 2 Steps 1) Tools -> Libraries. Scroll down on left side of window. Select Postgresql and on the right Click Add Jar or Folder and point it to the jar file. Click OK. 2) Within the project you are working on, in the Projects tab on the left hand sidee of the window, right click the project you want and choose Properties. Click Libraries choice (under Categories.) On right hand side on the Compile tab click the add library button and choose Postgresql. Click add library. Click OK. Now it should find and load Driver. All the Best Kyran