To connect you java program with postgres driver, add the postgres 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 {
        Class.forName("org.postgresql.Driver");
            Connection connection = null;

             connection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/sample","postgres", "postgres");
       if (connection != null)
          {    
  
                  Statement s=connection.createStatement();
                  String query="select ad_alert_id from ad_alert";
                  ResultSet rs=s.executeQuery(query);
                  while(rs.next())
                  {
                      System.out.print(" "+rs.getString(1));
                  }
                

          }
          else
              System.out.println("Connection Failed!");
      }
          catch(Exception e){
                              e.printStackTrace();
          }
   
    }
}
0

Add a comment

Popular Posts
Popular Posts
Blog Archive
Labels
Total Pageviews
Total Pageviews
148867
Subscribe
Subscribe
Labels
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.