Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 23rd, 2003, 01:03 PM
zapa zapa is offline
Mentat of IX
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Bucuresti / Toronto
Posts: 112 zapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 sec
Reputation Power: 6
Send a message via ICQ to zapa
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

Reply With Quote
  #2  
Old April 23rd, 2003, 07:32 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
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?

Reply With Quote
  #3  
Old April 23rd, 2003, 08:13 PM
zapa zapa is offline
Mentat of IX
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Bucuresti / Toronto
Posts: 112 zapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 sec
Reputation Power: 6
Send a message via ICQ to zapa
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 08:25 PM.

Reply With Quote
  #4  
Old April 24th, 2003, 05:51 AM
!zeBlSuDr !zeBlSuDr is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 8 !zeBlSuDr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.



Reply With Quote
  #5  
Old April 24th, 2003, 08:43 AM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
!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/devel...a1/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 08:53 AM.

Reply With Quote
  #6  
Old April 24th, 2003, 09:59 AM
zapa zapa is offline
Mentat of IX
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Bucuresti / Toronto
Posts: 112 zapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 sec
Reputation Power: 6
Send a message via ICQ to zapa
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

Reply With Quote
  #7  
Old April 24th, 2003, 10:13 AM
zapa zapa is offline
Mentat of IX
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Bucuresti / Toronto
Posts: 112 zapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 sec
Reputation Power: 6
Send a message via ICQ to zapa
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > problems with classpath and loading postgresql jdbc driver


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT