How to connect to PostgreSQL with JDBC driver – Java
Written on January 15, 2009 at 9:25 am by
mkyong
Here is a simple example to demonstrate how do connect to PostgreSQL database with JDBC driver in Java.
Go get a PostgreSQL JDBC driver first , else we can not do anything PostgreSQL JDBC Driver Download Here
Java JDBC connection always behave like following
Class.forName("org.postgresql.Driver"); Connection connection = null; connection = DriverManager.getConnection( "jdbc:postgresql://hostname:port/dbname","username", "password"); connection.close();
Here is the full example
import java.sql.DriverManager; import java.sql.Connection; import java.sql.SQLException; public class JDBCExample { public static void main(String[] argv) { System.out.println("-------- PostgreSQL JDBC Connection Testing ------------"); try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException e) { System.out.println("Where is your PostgreSQL JDBC Driver? Include in your library path!"); e.printStackTrace(); return; } System.out.println("PostgreSQL JDBC Driver Registered!"); Connection connection = null; try { connection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/testdb","mkyong", "123456"); } catch (SQLException e) { System.out.println("Connection Failed! Check output console"); e.printStackTrace(); return; } if (connection != null) System.out.println("You made it, take control your database now!"); else System.out.println("Failed to make connection!"); } }
How to run it?
Assume JDBCExample is store in c:\test folder, together with mysql JDBC driver
C:\test>java -cp c:\test\postgresql-8.3-603.jdbc4.jar;c:\test JDBCExample -------- MySQL JDBC Connection Testing ------------ PostgreSQL JDBC Driver Registered! You made it, take control your database now! C:\test>
This article was posted in Java category.
All Java Tutorials
- Java Core Technology - Java RegEx, Java XML, Java I/O, Java Misc
- J2EE Frameworks - Hibernate, Spring 2.5, Spring MVC, Struts 1.x, Struts 2.x
- Build Tools - Maven, Archiva
- Unit Test - jUnit, TestNG
- Client Scripts - jQuery
Newbie here,
where do i put the postgresql.jre file?
You can put it anywhere u want, and compile with -cp option for the classpath location. e.g
thanks boss :beer:
Thank’s for the article
Is there a way to connect to Postgres using javascript ?
interesting topic, i’ve seen some examples in MsAccess. May i know why you want to connect database in client side? it’s just doesn’t 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.
Nice work, thanx for the article.
} 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
it just print any SQL exception error messages…
hi i got the code in your side… how to run the code……and any other class path is there please send it my mail id…..pvasanth05@gmail.com….class path means any rar file
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!
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)
Why don’t people “READ” error messages these days… ?