Connecting to postgres database in java code

To connect your Java program with Postgresql driver, add the Postgresql library jar (postgresql-jdbc3-8.2.jar or any current jar file) file in the libraries. Then the following code can be modified for your particular database and table.

package javaapplication1;
import java.sql.*;
* @author shankar
public class Main {

* @param args the command line arguments
public static void main(String[] args) {
// TODO code application logic here
System.out.println("-------- PostgreSQL JDBC Connection Testing ------------");

try {
Connection connection = null;
connection = DriverManager.getConnection("jdbc:postgresql://","postgres", "postgres");
if (connection != null)

String query="select ad_alert_id from ad_alert";
ResultSet rs=s.executeQuery(query);
System.out.print(" "+rs.getString(1));

System.out.println("Connection Failed!");
catch(Exception e){


About Shankar Balachandran
Technical Manager, Openbravo operations, Kailao Consulting Private Limited

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: