Você está na página 1de 8

6/6/12

Connect to PostgreSQL with JDBC driver


Java Forum Advertise Contact Us RSS feed

Java Core

Web Frameworks

Spring

Hibernate

Android

GAE

Others

Java Forum

Magazine

Search

Connect to PostgreSQL with JDBC driver


Published: January 15, 2009 , Updated: April 4, 2011 , Author: mkyong

Mkyong on Facebook
Like 3,832 people like Mkyong.

Here is an example to show you how to connect to PostgreSQL database with JDBC driver.

1. Download PostgreSQL JDBC Driver


Get a PostgreSQL JDBC driver at this URL : http://jdbc.postgresql.org/download.html

P rateek

Lijin

Kannan

P ietro

Kirill

P ittala

C ng

S uranjana

S ridhar

Dastan

2. Java JDBC connection example


Code snippets to use JDBC to connect a PostgreSQL database
Casfrae"r.otrslDie"; ls.oNm(ogpsgeq.rvr) Cneto cneto =nl; oncin oncin ul cneto =DieMngrgtoncin oncin rvraae.eCneto( "dcpsgeq:/otaepr/bae,uenm" "asod) jb:otrsl/hsnm:otdnm""srae, pswr"; cneto.ls(; oncincoe)

F acebook social plugin

Recent Posts
Google App Engine Tutorial

See a complete example below : File : JDBCExample.java


ipr jv.q.rvraae; mot aaslDieMngr ipr jv.q.oncin mot aaslCneto; ipr jv.q.QEcpin mot aaslSLxeto; pbi casJBEape{ ulc ls DCxml pbi sai vi mi(tig]ag){ ulc ttc od anSrn[ rv Sse.u.rnl(---- PsgeQ " ytmotpitn"---- otrSL +"DCCneto Tsig------"; JB oncin etn ------) ty{ r Casfrae"r.otrslDie"; ls.oNm(ogpsgeq.rvr) }cth(lsNtonEcpine { ac CasoFudxeto ) Sse.u.rnl(Weei yu PsgeQ JB Die?" ytmotpitn"hr s or otrSL DC rvr +"nld i yu lbaypt!) Icue n or irr ah"; epittcTae) .rnSakrc(; rtr; eun } Sse.u.rnl(PsgeQ JB Die Rgsee!) ytmotpitn"otrSL DC rvr eitrd"; Cneto cneto =nl; oncin oncin ul ty{ r cneto =DieMngrgtoncin oncin rvraae.eCneto( "dcpsgeq:/2...:42tsd" "kog, jb:otrsl/1700153/etb, myn" "246) 135"; }cth(QEcpine { ac SLxeto ) Sse.u.rnl(Cneto Fie!Cekotu cnoe) ytmotpitn"oncin ald hc upt osl"; epittcTae) .rnSakrc(; rtr; eun } i (oncin! nl){ f cneto = ul Sse.u.rnl(Yumd i,tk cnrlyu dtbs nw"; ytmotpitn"o ae t ae oto or aaae o!) }es { le Sse.u.rnl(Fie t mk cneto!) ytmotpitn"ald o ae oncin"; } }

Google App Engine + JDO + Spring MVC, CRUD example

Google App Engine + Spring MVC, CRUD example with datastore low level api Cron job on Google App Engine for Java

Download / export Google App Engine Logs, Java App

www.mkyong.com/jdbc/how-do-connect-to-postgresql-with-jdbc-driver-java/

1/8

6/6/12
}

Connect to PostgreSQL with JDBC driver

3. Run it
Assume JDBCExample is store in c:\test folder, together with PostgreSQL JDBC driver, then run it :
C\etjv -pc\etpsgeq-.-0.dc.a;:ts JBEape :ts>aa c :ts\otrsl8363jb4jrc\et DCxml ---- MSLJB Cneto Tsig--------- yQ DC oncin etn -----PsgeQ JB Die Rgsee! otrSL DC rvr eitrd Yumd i,tk cnrlyu dtbs nw o ae t ae oto or aaae o!

Done

You might also like this tutorials :

Java JSON Tutorials

Maven Tutorials

Java RegEx Tutorials

Apache Wicket Tutorials

JSF 2 Tutorials

Subscribe to the Mkyong.com Newsletter


Stay up to date with all of Mkyong.com tips, tutorials and interesting stuffs by subscribing via email. Your email will be kept private and never shared with anyone. Email:
Subscribe

Related Posts
JDBC Tutorial JDBC Transaction example JDBC Statement example Batch Update JDBC PreparedStatement example Batch Update JDBC CallableStatement Stored Procedure OUT parameter example

Popular Posts
Top 5 free Java eBooks Top 8 Java people you should know Top 20 Java websites you must visit Top 10 Java regular expression examples Top 10 open source forums in collection Top 5 open source Q&A systems

28 Comments

June 1, 2012 at 10:28 pm

Djurdjura :
Thanks for this post. Really useful. One item I couldnt find yet anywhere is how would you go about detecting database connection afterwards? I mean, true database connection issues, not SQL errors. Thanks for replying, D.
Reply

www.mkyong.com/jdbc/how-do-connect-to-postgresql-with-jdbc-driver-java/

2/8

6/6/12

Connect to PostgreSQL with JDBC driver

April 3, 2012 at 2:19 pm

sanjeev :
many many thanks to mkyongits working fine
Reply

February 27, 2012 at 12:57 pm

Ambarish :
I can connect to the database when I use the code
jv -pc\etpsgeq-.-0.dc.a;:ts Eape aa c :ts\otrsl9191jb3jrc\et xml1

I modidified the classpath as


. c\etpsgeq-.-0.dc.a;:ts ; :ts\otrsl9191jb3jrc\et

and then I use the command. java Example1 I get the following error.
jv.agCasoFudxeto:ogpsgeq.rvr aaln.lsNtonEcpin r.otrslDie a jv.e.RCasodr1rnUkonSuc) t aantULlsLae$.u(nnw ore a jv.euiyAcsCnrle.orvlgdN t aascrt.cesotolrdPiiee( a jv.e.RCasodrfnCasUkonSu t aantULlsLae.idls(nnw o a jv.agCasodrlaCasUkonSuc t aaln.lsLae.odls(nnw or a snms.anhrAplsLae.odls(n t u.icLuce$pCasodrlaCasU a jv.agCasodrlaCasUkonSuc t aaln.lsLae.odls(nnw or a jv.agCasfrae(aieMto) t aaln.ls.oNm0Ntv ehd a jv.agCasfraeUkonSuc) t aaln.ls.oNm(nnw ore a Eape.anEape.aa1) t xml1mi(xml1jv:0

Reply

February 27, 2012 at 7:28 pm

mkyong :
Make sure p s g e q - . - 0 . d c . a is configured correctly and able to locate in otrsl9191jb3jr your class path. Suppose you are in Windows environment, make sure the PATH variable contains value of c \ e t p s g e q - . - 0 . d c . a . :ts\otrsl9191jb3jr
Reply

February 29, 2012 at 12:08 am

Ambarish :
It still gives the error that driver not found. I dont know where am i doing it wrong
Reply

February 29, 2012 at 12:12 am

Ambarish :
Hey I followed Surinders comment given below and it worked!!!! thanks mkyong for ur reply
Reply

www.mkyong.com/jdbc/how-do-connect-to-postgresql-with-jdbc-driver-java/

3/8

6/6/12
May 27, 2012 at 1:25 pm

Connect to PostgreSQL with JDBC driver

jiameng :
Hi, I have a same problem as yours, and finally I solved it. It does help if you only change the classpath ot the environment variable. Try this: 1.go to your project fold in the eclipse, right click, and then choose run as, click run configurations. 2. go to class path tab, choose add external jar, then choose the postgresql-9.1901.jdbc3.jar you have download. 3. run it again! This works for me, and I hope it will help you. PS: reference
ht:/tcoefo.o/usin/027/cis-aaln-lsntonecpin tp/sakvrlwcmqetos1598elpejv-agcasofudxeto

Finally, thanks mkyong, this is a great start tutorial for gostgis on java!
Reply

August 3, 2011 at 9:52 pm

Alex :
Hi, I got a warning: SQL exception: java.sql.SQLException: No suitable driver found for jdbc:postgresql://127.0.0.1:5432/fundb Please replay, my boss gonna kill me :D
Reply

December 6, 2011 at 12:54 am

Surendra :
set your class path variable as follow goto my computer-> properties->Advanced->Environment Variable Click New Variable Name=CLASSPATH Variable Value=C:\tomcat4\common\lib\servlet-api.jar;C:\tomcat4\common\lib\commonslang-2.4.jar; c:\jdk1.6\bin;C:\Test\postgresql-8.3-603.jdbc4.jar; click Ok and same as do for follow window. click ok click ok. open cmd and type java JDBCExample
Reply

June 3, 2011 at 2:11 pm

haha :
does the codes above work on eclipse?
Reply

June 3, 2011 at 10:57 pm

www.mkyong.com/jdbc/how-do-connect-to-postgresql-with-jdbc-driver-java/

4/8

6/6/12

Connect to PostgreSQL with JDBC driver

mkyong :
Sure it worksEclipse is just an IDE
Reply

April 13, 2011 at 12:19 am

JDBC Tutorials :
[...] Connect to PostgreSQL with JDBC driver Here is an example to show you how to connect to PostgreSQL database with JDBC driver. [...] Reply

January 5, 2011 at 1:39 am

Gaurav :
Which jre version is compatible with these code? jre1.4/1.5/1.6 which one??? I found bad version number in .class file error. Please reply soon. Thank in advance.
Reply

January 5, 2011 at 10:56 pm

mkyong :
any version will do, and your error message is telling you the conflict of different Java JDK at compile time and runtime, see below article http://www.mkyong.com/java/javalangunsupportedclassversionerror-bad-version-numberin-class-file/
Reply

January 4, 2011 at 8:09 am

Gregg Dunkle :
Cannot concur a lot more with this, incredibly attractive article. Thanks A Lot.
Reply

April 13, 2010 at 1:12 am

bullz :
Newbie here, where do i put the postgresql.jre file?
Reply

April 13, 2010 at 4:05 am

mkyong :
You can put it anywhere u want, and compile with -cp option for the classpath location. e.g
jv -pc\etpsgeq-.-0.dc.a;:ts JBEape aa c :ts\otrsl8363jb4jrc\et DCxml

Reply

www.mkyong.com/jdbc/how-do-connect-to-postgresql-with-jdbc-driver-java/

5/8

6/6/12

Connect to PostgreSQL with JDBC driver


April 19, 2010 at 10:20 pm

bullz :
thanks boss :beer:
Reply

February 21, 2010 at 2:35 pm

tundra :
Thanks for the article
Reply

November 5, 2009 at 9:35 am

Ezra :
Is there a way to connect to Postgres using javascript ?
Reply

November 5, 2009 at 1:01 pm

mkyong :
interesting topic, ive seen some examples in MsAccess. May i know why you want to connect database in client side? its just doesnt make sense to me, unless your database is store in client side as well. If you really want to do it, i will suggest you use Ajax to call your code in server side to connect database in server side.
Reply

September 8, 2009 at 6:29 am

Ubaid Raja :
Nice work, thanx for the article.
Reply

August 14, 2009 at 8:12 am

software development company :


} catch (SQLException e) { System.out.println(Connection Failed! Check output console); e.printStackTrace(); return; } I canvot get what this part of the code does ,do you mind explain it in more detail for me to understand
Reply

August 14, 2009 at 1:23 pm

mkyong :
it just print any SQL exception error messages
Reply

www.mkyong.com/jdbc/how-do-connect-to-postgresql-with-jdbc-driver-java/

6/8

6/6/12

Connect to PostgreSQL with JDBC driver

August 5, 2009 at 8:03 am

vasantha kumar :
hi i got the code in your side how to run the codeand any other class path is there please send it my mail id..pvasanth05@gmail.com.class path means any rar file
Reply

May 18, 2009 at 9:55 pm

Rui Teixeira :
Hi! Your code return this line to me: If you reach this line, please email me by telling how you do it? I just chage the address to: jdbc:postgresql://192.168.0.1:6000/comecont,xgest, qwerty and that return the error Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. Please reply to my email. Thanks!
Reply

August 5, 2009 at 7:47 pm

Bobs Your Uncle :


Postgres is telling you that you are trying to connect to database that is not there or is connecting to a database you are not allowed to. Check these things Posgres database is running, i.e. you have started the service and Does the database comecont exist? Does 192.168.0.1 exist? Does 192.168.0.1 accept requests via TCP/IP (the net) on port 6000? Has the firewall has accepted you as a trusted zone? (e.g. the portmaster)
Reply

August 7, 2009 at 12:35 pm

duh :
Why dont people READ error messages these days ?
Reply

Leave a Reply
Name (required)

Mail (will not be published) (required)

Website

[Note] - To post source code in comment, wrap your source code like this :

1. Java - <pre lang="java"> Java codes here </pre> 2. XML - <pre lang="xml"> XML here </pre> 3. HTML - <pre lang="html4strict"> HTML here </pre>

www.mkyong.com/jdbc/how-do-connect-to-postgresql-with-jdbc-driver-java/

7/8

6/6/12

Connect to PostgreSQL with JDBC driver


Questions not related to above article, please post at this JavaNullPointer forum.

Submit Comment

Notify me of followup comments via e-mail

All Available Tutorials


Java Core Technologies : Java I/O, Java RegEx, Java XML, Java JSON, JDBC, Java Misc J2EE Frameworks : Hibernate, JSF 2.0, Spring Core, Spring MVC, Spring Security, Apache Wicket, Struts 1.x, Struts 2.x Web Service : JAX-WS (SOAP), JAX-RS (REST) Build Tools : Maven, Archiva Unit Test Frameworks : jUnit, TestNG Others... : Android, Google App Engine, jQuery, Java MongoDB

Favorites Links
Android Developer Google App Engine using Java DZone - Fresh Links Official Java EE 5 Tutorial Official Java EE 6 Tutorial Spring 2.5.x documentation Spring 3.1.x documentation Hibernate core documentation Java SE 6.0 API documentation Java EE 6.0 API documentation Java Secure Socket Extension (JSSE) Reference Guide JSP home page JSF home page Eclipse IDE for Java developer Struts 1.3 documentation Struts 2.2 documentation Maven home page Maven central repository Search Java.Net Maven repository Ant home page JAX-WS Official Website JAX-RS Official Website (Jersey) MongoDB Official Website

Friends & Links


Java Training JavaScript Training Java Code Geeks Java & Co. Tutorials Martin Fowler PHP Tutorials TenthOfMarch Find Free Icons Web Security Blog

About Mkyong.com
All tutorials and examples are simple, easy to read, and from my personal experience. If you find any mistakes or wrong information in the tutorials, do let me know and correct me, via email or comment :) after all, we all learned through the process. For Java question that is not related to the tutorial, you can post here - Java Q&A forum. 1. Twitter - Follow Me 2. Facebook - Fan Page 3. RSS - Subscribe It Advertise With Us

Copyright 2008-2012 Mkyong Enterprise, all rights reserved. Privacy Policy

www.mkyong.com/jdbc/how-do-connect-to-postgresql-with-jdbc-driver-java/

8/8

Você também pode gostar