Connect to PostgreSQL with JDBC driver
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
2. Java JDBC connection example
Code snippets to use JDBC to connect a PostgreSQL database
Class.forName("org.postgresql.Driver"); Connection connection = null; connection = DriverManager.getConnection( "jdbc:postgresql://hostname:port/dbname","username", "password"); connection.close();
See a complete example below :
File : JDBCExample.java
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!"); } } }
3. Run it
Assume JDBCExample is store in c:\test folder, together with PostgreSQL JDBC driver, then run it :
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!
Done
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
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\commons-lang-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
does the codes above work on eclipse?
Sure it works…Eclipse is just an IDE
[...] Connect to PostgreSQL with JDBC driver Here is an example to show you how to connect to PostgreSQL database with JDBC driver. [...]
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.
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-number-in-class-file/
Cannot concur a lot more with this, incredibly attractive article. Thanks A Lot.
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… ?