Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql General > Re: large query...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 5 Topic 15441 of 16301
Post > Topic >>

Re: large query by offset and limt

by craig@[EMAIL PROTECTED] (Craig Ringer) May 4, 2008 at 08:56 AM

Ge Cong wrote:
> Thank you very much. Could you show me how to do it in JDBC?

Here's one example. As I haven't been using JDBC directly it's probably 
horrible, but it'll do the job. Any exception will terminate this 
example, but in practice you'd want to catch and handle exceptions 
appropriately.

Sorry about the ugly formatting - mail client line wrapping and all.

The example uses a dummy "customer" table, scrolling through it in 
chunks of 1000 records and printing the primary key `id' for each record.

----
im****t java.sql.Connection;
im****t java.sql.DriverManager;
im****t java.sql.ResultSet;
im****t java.sql.SQLException;
im****t java.sql.Statement;

public class Main {

     private static final int BATCH_SIZE = 1000;

     public static void main(String[] args)
             throws ClassNotFoundException, SQLException {

         // Load the JDBC driver
         Class.forName("org.postgresql.Driver");

         // Initialize a read only connection
         Connection c = DriverManager.getConnection(
             "jdbc:postgresql:DBNAME", "USERNAME", "PASSWORD");
         c.setReadOnly(true);
         c.setAutoCommit(false);

         // Declare an open cursor attached to a query for the
         // desired information
         Statement s = c.createStatement();
         s.execute("DECLARE customer_curs CURSOR FOR"
                   + " SELECT id FROM customer");

         // and fetch BATCH_SIZE records from the cursor until fewer
         // than the requested number of records are returned (ie
         // until we've run out of results).
         int nresults = 0;
         do {
             s.execute("FETCH " + BATCH_SIZE + " FROM customer_curs");
             ResultSet rs = s.getResultSet();
             while (rs.next()) {
                 nresults++;
                 // Do something with the current record at `rs'
                 System.out.println("CustomerID: " + rs.getString(1));
             }
         } while (nresults == BATCH_SIZE);	

         // Clean up.
         c.close();
     }

}
----

-- 
Sent via pgsql-general mailing list (pgsql-general@[EMAIL PROTECTED]
)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
 




 5 Posts in Topic:
large query by offset and limt
finecur <finecur@[EMAI  2008-05-02 14:01:43 
Re: large query by offset and limt
steve@[EMAIL PROTECTED]   2008-05-03 11:17:00 
Re: large query by offset and limt
craig@[EMAIL PROTECTED]   2008-05-04 02:34:38 
Re: large query by offset and limt
Ge Cong <gecong@[EMAI  2008-05-03 13:53:36 
Re: large query by offset and limt
craig@[EMAIL PROTECTED]   2008-05-04 08:56:54 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Aug 21 22:41:31 CDT 2008.