Login
Register

Home

Trainings

Fusion Blog

EBS Blog

Authors

CONTACT US

Miscellaneous
  • Register

Oracle Gold Partners, our very popular training packages, training schedule is listed here
Designed by Five Star Rated Oracle Press Authors & Oracle ACE's.

webinar new

Search Courses

Postgres is the most advanced Open Source database available in marketplace today.

If you are not building an extremely scalable enterprise application,

or if you have budgetary constraints, then you could consider using PostGres database.

 

Below, you will find the steps of writing a simple java program, that will connect to postgres database, using JDBC.

You can use this as a stepping stone to freebie build database based web application.



Step 1. First thing you do is to download the JDBC Driver for the postGres SQL Database.

This can be done from link http://jdbc.postgresql.org/download.html

Before you download, you need to know the version of your database.



Step 2. Save the downloaded jar file in any given location.




Step 3. Include this downloaded JDBC Driver location into the CLASSPATH.

This can either be done on command prompt, or use jDeveloper/Eclipse type of tool.

Below screenshot shows how this can be done when developing & testing in jDeveloper.



Step 4.

Edit the pg_hba.conf file in the machine where PostGres Database resides.

Put the IP Address/host from where database will be accessed.

You can also make wildcard entries to all access from all machines, if need be.

vi /san/data/xxdir/xxloc/pg_hba.conf

At the very bottom of this file, I added to two entries similar to below

host    dbnameHere           dsuat   155.999.136.56  255.255.255.255 trust

host    dbnameHere           dsuat   155.999.136.56  255.255.255.0 trust





Step 5

Bounce the postgres database.

This is required for changes to pg_hba.conf to take place.

Bouncing the database can be done by executing two commands, as shown below

pg_ctl stop -m fast -D /san/xxdir/xxloc/dblocationDirectory

postmaster -D /san/xxdir/xxloc/dblocationDirectory &




Step 6

Write a class and test out the results.

package xxspiralutilities;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class xxQueueSizeUtilities {

    public static String getQueueSize() {

        try {

            Class.forName("org.postgresql.Driver");

        } catch (ClassNotFoundException cnfe) {

            System.err.println("Couldn't find driver class:");

            System.out.println("Couldn't find driver class:");

            cnfe.printStackTrace();

        }

        System.out.println("Able to locate the jmake a connection.");

        Connection postGresConn = null;

        try {

            postGresConn =

                    DriverManager.getConnection("jdbc:postgresql://xxmachine.com:5433/nameOfDBHere",

                                                "userNameHere", "passwordHere");

        } catch (SQLException se) {

            System.out.println("Couldn't connect: print out a stack trace and exit.");

            se.printStackTrace();

            System.exit(1);

        }

 

        if (postGresConn != null)

            System.out.println("Successfully connected to Postgres Database");

        else

            System.out.println("We should never get here.");

 

        try {

            Statement stGetCount = postGresConn.createStatement();

            ResultSet rs =

                stGetCount.executeQuery("SELECT SUM(import_count -import_remaining) from  xx_queue_table where finish is null and import_end is null");

            //ResultSet rs = stGetCount.executeQuery("SELECT SUM(import_count -import_remaining) from  xx_queue_table") ;

            rs.next();

            return rs.getString(1);

        } catch (SQLException e) {

            System.out.println("Could not create statement in JDBC");

            e.printStackTrace();

 

        }

        return "SUCCESS";

    }

 

    public static void main(String[] args) {

        String textString = getQueueSize();

        System.out.println("Result is " + textString);

    }

}

 


Anil Passi

Comments   

0 #1 Syed 2007-12-10 08:00
I am capturing more & more relevant information.

T hanks,

Anil
Quote
0 #2 baran 2009-01-24 16:16
thanx dude
Quote

Add comment


Security code
Refresh

Search Trainings

Fully verifiable testimonials

Apps2Fusion - Event List

<<  May 2024  >>
 Mon  Tue  Wed  Thu  Fri  Sat  Sun 
    1  2  3  4  5
  6  7  8  9101112
13141516171819
20212223242526
2728293031  

Enquire For Training

Fusion Training Packages

Get Email Updates


Powered by Google FeedBurner