Escolar Documentos
Profissional Documentos
Cultura Documentos
Java Core
Web Frameworks
Spring
Hibernate
Android
GAE
Others
Java Forum
Magazine
Search
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.
P rateek
Lijin
Kannan
P ietro
Kirill
P ittala
C ng
S uranjana
S ridhar
Dastan
Recent Posts
Google App Engine Tutorial
Google App Engine + Spring MVC, CRUD example with datastore low level api Cron job on Google App Engine for Java
www.mkyong.com/jdbc/how-do-connect-to-postgresql-with-jdbc-driver-java/
1/8
6/6/12
}
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
Maven Tutorials
JSF 2 Tutorials
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
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
sanjeev :
many many thanks to mkyongits working fine
Reply
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
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
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
Ambarish :
It still gives the error that driver not found. I dont know where am i doing it wrong
Reply
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
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
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
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
haha :
does the codes above work on eclipse?
Reply
www.mkyong.com/jdbc/how-do-connect-to-postgresql-with-jdbc-driver-java/
4/8
6/6/12
mkyong :
Sure it worksEclipse is just an IDE
Reply
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
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
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
Gregg Dunkle :
Cannot concur a lot more with this, incredibly attractive article. Thanks A Lot.
Reply
bullz :
Newbie here, where do i put the postgresql.jre file?
Reply
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
bullz :
thanks boss :beer:
Reply
tundra :
Thanks for the article
Reply
Ezra :
Is there a way to connect to Postgres using javascript ?
Reply
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
Ubaid Raja :
Nice work, thanx for the article.
Reply
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
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
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
duh :
Why dont people READ error messages these days ?
Reply
Leave a Reply
Name (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
Submit Comment
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
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
www.mkyong.com/jdbc/how-do-connect-to-postgresql-with-jdbc-driver-java/
8/8