pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Not able to connect to postgresql database
13+ messages / 6 participants
[nested] [flat]

* Not able to connect to postgresql database
@ 2010-06-01 20:19  Manohar Bhattarai <manoharbhattarai@gmail.com>
  0 siblings, 2 replies; 13+ messages in thread

From: Manohar Bhattarai @ 2010-06-01 20:19 UTC (permalink / raw)
  To: pgsql-jdbc

Hi,

 I am a newbie for postgresql. I am trying to connect to postgresql database
using jdbc. I googled about it and got the Driver string and connection
string formats. I tried using some examples i got on the internet. But my
program is not able to connect to postgresql and shows an exception that it
cannot find driver. I am doing it in Ubuntu8.04 Linux with Postgresql 8.3
installed. I am using Eclipse galileo as IDE. I created database using
commands in terminal. I have added the external jar files for postgresql
jdbc connector(postgresql-8.3-603.jdbc3.jar, postgresql-8.3-603.jdbc4.jar,
postgresql-8.4-701.jdbc3.jar and postgresql-8.4-701.jdbc4.jar) in my project
also.

Please help me and let me know what i am doing wrong and what i am missing.
Can you please give an example code if possible?

Thanks
-- 
Regards,
Manohar Bhattarai (मनोहर भट्टराई)
Blogs:
http://manoharbhattarai.wordpress.com/
http://manoharbhattarai.posterous.com/
http://manoharbhattarai.blogspot.com/
Microblogs:
Twitter :- http://twitter.com/manoharmailme
Identi.ca :- http://identi.ca/manoharbhattarai

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

* Re: Not able to connect to postgresql database
@ 2010-06-01 20:27  Kevin Grittner <Kevin.Grittner@wicourts.gov>
  parent: Manohar Bhattarai <manoharbhattarai@gmail.com>
  1 sibling, 2 replies; 13+ messages in thread

From: Kevin Grittner @ 2010-06-01 20:27 UTC (permalink / raw)
  To: Manohar Bhattarai <manoharbhattarai@gmail.com>; pgsql-jdbc

Manohar Bhattarai <manoharbhattarai@gmail.com> wrote:
 
> shows an exception that it cannot find driver.
 
> Please help me and let me know what i am doing wrong and what i am
> missing.
 
The jar is not on your classpath, or you are not spelling the class
name ("org.postgresql.Driver") correctly.  I don't know of any other
reason that the recommended class loading:
 
    Class.forName("org.postgresql.Driver");
 
would throw a ClassNotFoundException.  (If that's not what you're
doing, or that's not the statement on which it fails, or that's not
your exception, you need to give us more clues.)
 
> Can you please give an example code if possible?
 
There are examples here:
 
http://jdbc.postgresql.org/documentation/84/index.html
 
-Kevin



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

* Re: Not able to connect to postgresql database
@ 2010-06-01 20:30  Maciek Sakrejda <msakrejda@truviso.com>
  parent: Manohar Bhattarai <manoharbhattarai@gmail.com>
  1 sibling, 0 replies; 13+ messages in thread

From: Maciek Sakrejda @ 2010-06-01 20:30 UTC (permalink / raw)
  To: Manohar Bhattarai <manoharbhattarai@gmail.com>; +Cc: pgsql-jdbc

Well, first of all, you may want to stick with a single driver jar.
8.4 is backwards compatible with 8.3, so you should probably use 8.4,
and jdbc3 vs. jdbc4 is a Java version issue--jdbc4 for Java 6 and up,
jdbc3g for Java 5. I'm not sure what happens if you try to load
different definitions of the same class four times (not to mention
that I'm not sure how it's even possible, since the 3g and 4 versions
are compile-time incompatible with projects using a different Java
version).

If you're still getting errors, try an explicit

Class.forName("org.postgresql.Driver");

and see if you still get an Exception. If so, it sounds like an
Eclipse configuration issue since the jar isn't showing up in your
classpath.
---
Maciek Sakrejda | System Architect | Truviso

1065 E. Hillsdale Blvd., Suite 230
Foster City, CA 94404
(650) 242-3500 Main
(650) 242-3501 F
www.truviso.com



On Tue, Jun 1, 2010 at 1:19 PM, Manohar Bhattarai
<manoharbhattarai@gmail.com> wrote:
> Hi,
>  I am a newbie for postgresql. I am trying to connect to postgresql database
> using jdbc. I googled about it and got the Driver string and connection
> string formats. I tried using some examples i got on the internet. But my
> program is not able to connect to postgresql and shows an exception that it
> cannot find driver. I am doing it in Ubuntu8.04 Linux with Postgresql 8.3
> installed. I am using Eclipse galileo as IDE. I created database using
> commands in terminal. I have added the external jar files for postgresql
> jdbc connector(postgresql-8.3-603.jdbc3.jar, postgresql-8.3-603.jdbc4.jar,
> postgresql-8.4-701.jdbc3.jar and postgresql-8.4-701.jdbc4.jar) in my project
> also.
> Please help me and let me know what i am doing wrong and what i am missing.
> Can you please give an example code if possible?
> Thanks
> --
> Regards,
> Manohar Bhattarai (मनोहर भट्टराई)
> Blogs:
> http://manoharbhattarai.wordpress.com/
> http://manoharbhattarai.posterous.com/
> http://manoharbhattarai.blogspot.com/
> Microblogs:
> Twitter :- http://twitter.com/manoharmailme
> Identi.ca :- http://identi.ca/manoharbhattarai
>



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

* Re: Not able to connect to postgresql database
@ 2010-06-01 20:46  John R Pierce <pierce@hogranch.com>
  parent: Kevin Grittner <Kevin.Grittner@wicourts.gov>
  1 sibling, 0 replies; 13+ messages in thread

From: John R Pierce @ 2010-06-01 20:46 UTC (permalink / raw)
  To: ; +Cc: pgsql-jdbc

Kevin Grittner wrote:
> Manohar Bhattarai <manoharbhattarai@gmail.com> wrote:
>  
>   
>> shows an exception that it cannot find driver.
>>     
> The jar is not on your classpath, or you are not spelling the class
> name ("org.postgresql.Driver") correctly.  I don't know of any other
> reason that the recommended class loading:
>  
>     Class.forName("org.postgresql.Driver");
>  
> would throw a ClassNotFoundException.  (If that's not what you're
> doing, or that's not the statement on which it fails, or that's not
> your exception, you need to give us more clues.)
>   

a further clarification, you need to specify the full path and name of 
the jdbc jar file in the classpath, its not enough to drop 
postgresql-8.4-701.jdbc4.jar into a dir thats in the classpath as it 
won't recognize the filename.

btw, you only need one of the jdbc drivers, you listed several different 
ones.





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

* Re: Not able to connect to postgresql database
@ 2010-06-01 23:38  Lew <noone@lewscanon.com>
  parent: Kevin Grittner <Kevin.Grittner@wicourts.gov>
  1 sibling, 1 reply; 13+ messages in thread

From: Lew @ 2010-06-01 23:38 UTC (permalink / raw)
  To: pgsql-jdbc

On 06/01/2010 04:27 PM, Kevin Grittner wrote:
Manohar Bhattarai wrote:
>
>> shows an exception that it cannot find driver.
>
>> Please help me and let me know what i am doing wrong and what i am
>> missing.
>
> The jar is not on your classpath, or you are not spelling the class
> name ("org.postgresql.Driver") correctly.  I don't know of any other
> reason that the recommended class loading:
>
>      Class.forName("org.postgresql.Driver");

Just as an aside, nowadays the recommended way from Sun is to load a driver is 
through DataSource descriptors, which has its advantages but is a lot less 
simple at first.  With JPA you configure the data source in the 
"persistence.xml" file for a local application, other XML files like 
"context.xml" for app servers.

The 'Class.forName()' method is reliable but don't fall into the trap of 
calling it more than once per program execution.

> would throw a ClassNotFoundException.  (If that's not what you're
> doing, or that's not the statement on which it fails, or that's not
> your exception, you need to give us more clues.)

All techniques require proper placement of the driver JAR file in your 
classpath.  What that classpath is depends utterly on how you package and 
deploy your app.

There's clear documentation on java.sun.com about JDBC, and about how to 
specify the classpath for the "java ... foo.foo" ("-classpath" option) and 
"java -jar foo.jar" (manifest file "Class-path:" entry) commands.  For the 
various app servers you have to check the rules for the particular app server, 
e.g.,
<http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html;

-- 
Lew



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

* Re: Not able to connect to postgresql database
@ 2010-06-02 13:40  Manohar Bhattarai <manoharbhattarai@gmail.com>
  parent: Lew <noone@lewscanon.com>
  0 siblings, 1 reply; 13+ messages in thread

From: Manohar Bhattarai @ 2010-06-02 13:40 UTC (permalink / raw)
  To: pgsql-jdbc

Thanks for your replies.

Now i have removed other three jar files and have only one jar i.e.
postgresql-8.4-701.jdbc4.jar
as told above that it is backward compatible with Postgresql 8.3 and using
jdk1.6. I have added the jar in classpath also in the CLASSPATH variable of
/etc/environment file. Still I am getting exception :
java.lang.ClassNotFoundException:
org.postgresql.Driver.

A statement just above the line :
Class.forName("org.postgresql.Driver");
is executed and goes to the catch block.

Now what could be the issue? Still what am i doing wrong or missing?
Please help.

-- 
Regards,
Manohar Bhattarai (मनोहर भट्टराई)
Blogs:
http://manoharbhattarai.wordpress.com/
http://manoharbhattarai.posterous.com/
http://manoharbhattarai.blogspot.com/
Microblogs:
Twitter :- http://twitter.com/manoharmailme
Identi.ca :- http://identi.ca/manoharbhattarai

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

* Re: Not able to connect to postgresql database
@ 2010-06-02 13:49  Kevin Grittner <Kevin.Grittner@wicourts.gov>
  parent: Manohar Bhattarai <manoharbhattarai@gmail.com>
  0 siblings, 1 reply; 13+ messages in thread

From: Kevin Grittner @ 2010-06-02 13:49 UTC (permalink / raw)
  To: Manohar Bhattarai <manoharbhattarai@gmail.com>; pgsql-jdbc

Manohar Bhattarai <manoharbhattarai@gmail.com> wrote:
 
> I have added the jar in classpath also in the CLASSPATH variable
> of /etc/environment file. Still I am getting exception :
> java.lang.ClassNotFoundException: org.postgresql.Driver.
 
You said you were using Eclipse, which I believe sets its own
classpath for each runnable you set up within it.  Look for the
entry under the "Run..." or "Debug..." menu item and set it up
there.
 
-Kevin



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

* Re: Not able to connect to postgresql database
@ 2010-06-02 15:39  dmp <danap@ttc-cmc.net>
  parent: Kevin Grittner <Kevin.Grittner@wicourts.gov>
  0 siblings, 1 reply; 13+ messages in thread

From: dmp @ 2010-06-02 15:39 UTC (permalink / raw)
  To: pgsql-jdbc

>
>
>Manohar Bhattarai <manoharbhattarai@gmail.com> wrote:
> 
>  
>
>>> I have added the jar in classpath also in the CLASSPATH variable
>>> of /etc/environment file. Still I am getting exception :
>>> java.lang.ClassNotFoundException: org.postgresql.Driver.
>>    
>>
> 
>You said you were using Eclipse, which I believe sets its own
>classpath for each runnable you set up within it.  Look for the
>entry under the "Run..." or "Debug..." menu item and set it up
>there.
> 
>-Kevin
>

If you are using Eclipse, and during the initial configuration of the 
project
the PostgreSQL driver was not present in the project directory then it will
needed to be added. To set that:

Highlight the Project: Project | Properties | Java Build Path | 
Libraries | Add JARs.

danap.

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

* Re: Not able to connect to postgresql database
@ 2010-06-02 16:18  Manohar Bhattarai <manoharbhattarai@gmail.com>
  parent: dmp <danap@ttc-cmc.net>
  0 siblings, 1 reply; 13+ messages in thread

From: Manohar Bhattarai @ 2010-06-02 16:18 UTC (permalink / raw)
  To: dmp <danap@ttc-cmc.net>; +Cc: pgsql-jdbc

On Wed, Jun 2, 2010 at 9:09 PM, dmp <danap@ttc-cmc.net> wrote:

>  Manohar Bhattarai <manoharbhattarai@gmail.com> <manoharbhattarai@gmail.com> wrote:
>
>
>
>  > I have added the jar in classpath also in the CLASSPATH variable> of /etc/environment file. Still I am getting exception :> java.lang.ClassNotFoundException: org.postgresql.Driver.
>
>
>
> You said you were using Eclipse, which I believe sets its own
> classpath for each runnable you set up within it.  Look for the
> entry under the "Run..." or "Debug..." menu item and set it up
> there.
>
> -Kevin
>
>
> If you are using Eclipse, and during the initial configuration of the
> project
> the PostgreSQL driver was not present in the project directory then it will
> needed to be added. To set that:
>
> Highlight the Project:   Project  |  Properties  |  Java Build Path  |
> Libraries  |  Add JARs.
>
> danap.
>
I had added the jar files already but it was giving same exception. But now
the Driver is found. :) Thanks all. It was solved by adding the jar file to
CATALINA_HOME/lib/ directory. Then it found the driver.
But now a new exception is :
org.postgresql.util.PSQLException: FATAL: password authentication failed for
user "postgres"

I am giving the line for connection that i am using
connection = DriverManager.getConnection(connectionURL, "postgres",
"mypasswordhere");

But the password is the same that i use to login postgres user in the
terminal.
What could be the problem?

Can you please give me the steps to set up new user and create new database
after a fresh install of postgresql? I have doubts seeing different sites
giving different methods.

Can I follow this link ==> https://help.ubuntu.com/community/PostgreSQL



-- 
Regards,
Manohar Bhattarai (मनोहर भट्टराई)
Blogs:
http://manoharbhattarai.wordpress.com/
http://manoharbhattarai.posterous.com/
http://manoharbhattarai.blogspot.com/
Microblogs:
Twitter :- http://twitter.com/manoharmailme
Identi.ca :- http://identi.ca/manoharbhattarai

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

* Re: Not able to connect to postgresql database
@ 2010-06-02 16:37  Kevin Grittner <Kevin.Grittner@wicourts.gov>
  parent: Manohar Bhattarai <manoharbhattarai@gmail.com>
  0 siblings, 1 reply; 13+ messages in thread

From: Kevin Grittner @ 2010-06-02 16:37 UTC (permalink / raw)
  To: Manohar Bhattarai <manoharbhattarai@gmail.com>; dmp <danap@ttc-cmc.net>; +Cc: pgsql-jdbc

Manohar Bhattarai <manoharbhattarai@gmail.com> wrote:
 
> now the Driver is found. :)
 
Good news.  :-)
 
> But now a new exception is :
> org.postgresql.util.PSQLException: FATAL: password authentication
> failed for user "postgres"
 
> But the password is the same that i use to login postgres user in
> the terminal.
> What could be the problem?
 
The password for the postgres *database* user is not necessarily the
same as for a postgres *OS* user.  You need to find or reset the
database user password.  If you need to reset it, you can do that by
modifying pg_hba.conf to allow trust or ident authentication (at
least temporarily) or by running the postmaster in single-user mode.
 
On the other hand, it is generally not a good idea to run the
application under the database superuser login; superuser logins
should be reserved for administrative purposes which require that
level of authority.  I generally use a non-superuser user for the
database owner and less privileged users for running applications.
 
> Can you please give me the steps to set up new user and create new
> database after a fresh install of postgresql? I have doubts seeing
> different sites giving different methods.
 
There are a lot of ways to do that, and what you should do next
probably depends on what you've done so far.  You're venturing into
territory which has nothing to do with JDBC -- you might want to
post this question on pgsql-admin or pgsql-general with a
description of what you've your OS, done so far, what you're trying
to do, and what problem you're hitting.
 
-Kevin



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

* Re: Not able to connect to postgresql database
@ 2010-06-02 16:41  Kevin Grittner <Kevin.Grittner@wicourts.gov>
  parent: Kevin Grittner <Kevin.Grittner@wicourts.gov>
  0 siblings, 1 reply; 13+ messages in thread

From: Kevin Grittner @ 2010-06-02 16:41 UTC (permalink / raw)
  To: Manohar Bhattarai <manoharbhattarai@gmail.com>; dmp <danap@ttc-cmc.net>; Kevin Grittner <Kevin.Grittner@wicourts.gov>; +Cc: pgsql-jdbc

[correcting copy/paste mangling]
 
I wrote:
 
> description of what you've your OS, done so far, what you're
> trying to do, and what problem you're hitting.
 
description of your OS, what you've done so far, what you're
trying to do, and what problem you're hitting.
 
-Kevin



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

* Re: Not able to connect to postgresql database
@ 2010-06-02 18:06  Manohar Bhattarai <manoharbhattarai@gmail.com>
  parent: Kevin Grittner <Kevin.Grittner@wicourts.gov>
  0 siblings, 1 reply; 13+ messages in thread

From: Manohar Bhattarai @ 2010-06-02 18:06 UTC (permalink / raw)
  To: Kevin Grittner <Kevin.Grittner@wicourts.gov>; +Cc: dmp <danap@ttc-cmc.net>; pgsql-jdbc

On Wed, Jun 2, 2010 at 10:11 PM, Kevin Grittner <Kevin.Grittner@wicourts.gov
> wrote:

> [correcting copy/paste mangling]
>
> I wrote:
>
> > description of what you've your OS, done so far, what you're
> > trying to do, and what problem you're hitting.
>
> description of your OS, what you've done so far, what you're
> trying to do, and what problem you're hitting.
>
> -Kevin
>

Thanks for your quick reply. I will be asking this problem as specified by
you. Thank you all.

-- 
Regards,
Manohar Bhattarai (मनोहर भट्टराई)
Blogs:
http://manoharbhattarai.wordpress.com/
http://manoharbhattarai.posterous.com/
http://manoharbhattarai.blogspot.com/
Microblogs:
Twitter :- http://twitter.com/manoharmailme
Identi.ca :- http://identi.ca/manoharbhattarai

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

* Re: Not able to connect to postgresql database
@ 2010-06-05 11:31  Manohar Bhattarai <manoharbhattarai@gmail.com>
  parent: Manohar Bhattarai <manoharbhattarai@gmail.com>
  0 siblings, 0 replies; 13+ messages in thread

From: Manohar Bhattarai @ 2010-06-05 11:31 UTC (permalink / raw)
  To: Kevin Grittner <Kevin.Grittner@wicourts.gov>; +Cc: dmp <danap@ttc-cmc.net>; pgsql-jdbc

On Wed, Jun 2, 2010 at 11:36 PM, Manohar Bhattarai <
manoharbhattarai@gmail.com> wrote:

>
>
> On Wed, Jun 2, 2010 at 10:11 PM, Kevin Grittner <
> Kevin.Grittner@wicourts.gov> wrote:
>
>> [correcting copy/paste mangling]
>>
>> I wrote:
>>
>> > description of what you've your OS, done so far, what you're
>> > trying to do, and what problem you're hitting.
>>
>> description of your OS, what you've done so far, what you're
>> trying to do, and what problem you're hitting.
>>
>> -Kevin
>>
>
> Thanks for your quick reply. I will be asking this problem as specified by
> you. Thank you all.
>
>
> --
> Regards,
> Manohar Bhattarai (मनोहर भट्टराई)
> Blogs:
> http://manoharbhattarai.wordpress.com/
> http://manoharbhattarai.posterous.com/
> http://manoharbhattarai.blogspot.com/
> Microblogs:
> Twitter :- http://twitter.com/manoharmailme
> Identi.ca :- http://identi.ca/manoharbhattarai
>


Problem solved. All exception gone. :) Thanks all... for your quick help.
-- 
Regards,
Manohar Bhattarai (मनोहर भट्टराई)
Blogs:
http://manoharbhattarai.wordpress.com/
http://manoharbhattarai.posterous.com/
http://manoharbhattarai.blogspot.com/
Microblogs:
Twitter :- http://twitter.com/manoharmailme
Identi.ca :- http://identi.ca/manoharbhattarai

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


end of thread, other threads:[~2010-06-05 11:31 UTC | newest]

Thread overview: 13+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-06-01 20:19 Not able to connect to postgresql database Manohar Bhattarai <manoharbhattarai@gmail.com>
2010-06-01 20:27 ` Kevin Grittner <Kevin.Grittner@wicourts.gov>
2010-06-01 20:46   ` John R Pierce <pierce@hogranch.com>
2010-06-01 23:38   ` Lew <noone@lewscanon.com>
2010-06-02 13:40     ` Manohar Bhattarai <manoharbhattarai@gmail.com>
2010-06-02 13:49       ` Kevin Grittner <Kevin.Grittner@wicourts.gov>
2010-06-02 15:39         ` dmp <danap@ttc-cmc.net>
2010-06-02 16:18           ` Manohar Bhattarai <manoharbhattarai@gmail.com>
2010-06-02 16:37             ` Kevin Grittner <Kevin.Grittner@wicourts.gov>
2010-06-02 16:41               ` Kevin Grittner <Kevin.Grittner@wicourts.gov>
2010-06-02 18:06                 ` Manohar Bhattarai <manoharbhattarai@gmail.com>
2010-06-05 11:31                   ` Manohar Bhattarai <manoharbhattarai@gmail.com>
2010-06-01 20:30 ` Maciek Sakrejda <msakrejda@truviso.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