FAQ
Hello!

I am trying to use a cursor in Java to fetch multiple rows at once. The code
seems to connect to the database okay, and seems to be fetching records, but
then it fails with the error:

Exception in thread "main" java.lang.OutOfMemoryError


Here is the Java code:


import java.sql.*;

/*
* To compile:
*
* /usr/bin/jikes -source 1.4
-bootclasspath /usr/java/j2sdk1.4.2_02/jre/lib/rt.jar:/usr/java/j2sdk1.4.2_02/jre/lib/jsse.jar:/usr/java/j2sdk1.4.2_02/jre/li
b/jce.jar test_connect.java
*
*
* To run:
*
* java
-cp /usr/share/java/postgresql-8.1-404.jdbc2.jar:/usr/java/j2sdk1.4.2_02/jre/lib/rt.jar:/usr/java/j2sdk1.4.2_02/jre/lib/jsse.jar:/usr/java/j2sdk1.4.
2_02/jre/lib/jce.jar:. test_connect
*/
class test_connect
{
private static Connection connectToDb( String dbName,
String username,
String password ) throws Exception
{
System.out.println( "Connecting to DB " + dbName + ", username " +
username );
String dbDriver = "org.postgresql.Driver";
String url = "jdbc:postgresql://imsstd4/" + dbName;
Class.forName( dbDriver ).newInstance();

System.out.println( "Connecting to " + url + " with user " + username
+ " and pass " + password );

Connection connection = DriverManager.getConnection( url, username,
password );

System.out.println( "Connection successful" );

return connection;
}

private static void executeQuery( Connection connection ) throws
SQLException
{
String sql = "SELECT * FROM pid, pv1 where pid.patient_id_internal_id
= pv1.patient_id_internal_id";

System.out.println( "Executing query: " + sql );
Statement st = connection.createStatement();

System.out.println( "Setting cursor size to 50 rows." );
st.setFetchSize( 50 ); // turn on cursor
ResultSet rs = st.executeQuery( sql );
System.out.println( "Got 50 results. Now walking them..." );
int i = 0;
while ( rs.next() )
{
// Do not print anything here for now
i++;
System.out.println( "Row " + i );
}
System.out.println( "Result set consumed " + i + " rows." );
}

public static void main( String[] args )
{
System.out.println( "Starting up..." );

try
{
Connection connection = connectToDb( "hl7segmentsihe", "hl7",
"segments" );

executeQuery( connection );
}
catch ( Exception e )
{
System.out.println( "Got exception " + e );
e.printStackTrace();
}
}
}


Here is the program output:


Starting up...
Connecting to DB hl7segmentsihe, username hl7
Connecting to jdbc:postgresql://imsstd4/hl7segmentsihe with user hl7 and pass
segments
Connection successful
Executing query: SELECT * FROM pid, pv1 where pid.patient_id_internal_id =
pv1.patient_id_internal_id
Setting cursor size to 50 rows.
Exception in thread "main" java.lang.OutOfMemoryError


What did I do wrong? Any help would be appreciated.


--
Regards,

Richard Kut
Database Administrator
Research & Development
Intelerad Medical Systems Inc.
460 Ste-Catherine West, Suite 210
Montreal, Quebec, Canada H3B 1A7
Tel: 514.931.6222 x7733
Fax: 514.931.4653
rkut@intelerad.com
www.intelerad.com

This email or any attachments may contain confidential or legally
privileged information intended for the sole use of the addressees. Any
use, redistribution, disclosure, or reproduction of this information,
except as intended, is prohibited. If you received this
email in error, please notify the sender and remove all copies of the
message, including any attachments.

Search Discussions

  • Michael Swierczek at Mar 13, 2006 at 8:40 pm
    Richard,
    I'm not sure about all operating systems, but on Windows 2000 and XP
    you can add a flag -Xmx___M to your java command line to set the maximum
    program heap size. The default Java heap maximum is 64 MB. If your test
    machine has a reasonably good amount of RAM, you could insert -Xmx300M or
    -Xmx500M to your command line and see if you get the same error.
    And you may be misunderstanding what setFetchSize(int) does. It does
    not control the maximum amount of data retrieved, just how many rows are
    retrieved at a given time. Maybe you want to use setMaxRows(int) instead,
    at least for testing. For more information, you can look at
    http://www.jguru.com/faq/view.jsp?EID=462124
    I hope this helps.

    -Mike

    On 3/13/06, Richard Kut wrote:

    Hello!

    I am trying to use a cursor in Java to fetch multiple rows at
    once. The code
    seems to connect to the database okay, and seems to be fetching records,
    but
    then it fails with the error:

    Exception in thread "main" java.lang.OutOfMemoryError


    Here is the Java code:


    import java.sql.*;

    /*
    * To compile:
    *
    * /usr/bin/jikes -source 1.4
    -bootclasspath
    /usr/java/j2sdk1.4.2_02/jre/lib/rt.jar:/usr/java/j2sdk1.4.2_02/jre/lib/jsse.jar:/usr/java/j2sdk1.4.2_02/jre/li

    b/jce.jar test_connect.java
    *
    *
    * To run:
    *
    * java
    -cp /usr/share/java/postgresql-
    8.1-404.jdbc2.jar:/usr/java/j2sdk1.4.2_02/jre/lib/rt.jar:/usr/java/j2sdk1.4.2_02/jre/lib/jsse.jar:/usr/java/j2sdk1.4.
    2_02/jre/lib/jce.jar:. test_connect
    */
    class test_connect
    {
    private static Connection connectToDb( String dbName,
    String username,
    String password ) throws
    Exception
    {
    System.out.println( "Connecting to DB " + dbName + ", username " +
    username );
    String dbDriver = "org.postgresql.Driver";
    String url = "jdbc:postgresql://imsstd4/" + dbName;
    Class.forName( dbDriver ).newInstance();

    System.out.println( "Connecting to " + url + " with user " +
    username
    + " and pass " + password );

    Connection connection = DriverManager.getConnection( url,
    username,
    password );

    System.out.println( "Connection successful" );

    return connection;
    }

    private static void executeQuery( Connection connection ) throws
    SQLException
    {
    String sql = "SELECT * FROM pid, pv1 where
    pid.patient_id_internal_id
    = pv1.patient_id_internal_id";

    System.out.println( "Executing query: " + sql );
    Statement st = connection.createStatement();

    System.out.println( "Setting cursor size to 50 rows." );
    st.setFetchSize( 50 ); // turn on cursor
    ResultSet rs = st.executeQuery ( sql );
    System.out.println( "Got 50 results. Now walking them..." );
    int i = 0;
    while ( rs.next() )
    {
    // Do not print anything here for now
    i++;
    System.out.println( "Row " + i );
    }
    System.out.println( "Result set consumed " + i + " rows." );
    }

    public static void main( String[] args )
    {
    System.out.println( "Starting up..." );

    try
    {
    Connection connection = connectToDb( "hl7segmentsihe", "hl7",
    "segments" );

    executeQuery( connection );
    }
    catch ( Exception e )
    {
    System.out.println( "Got exception " + e );
    e.printStackTrace();
    }
    }
    }


    Here is the program output:


    Starting up...
    Connecting to DB hl7segmentsihe, username hl7
    Connecting to jdbc:postgresql://imsstd4/hl7segmentsihe with user hl7 and
    pass
    segments
    Connection successful
    Executing query: SELECT * FROM pid, pv1 where pid.patient_id_internal_id =
    pv1.patient_id_internal_id
    Setting cursor size to 50 rows.
    Exception in thread "main" java.lang.OutOfMemoryError


    What did I do wrong? Any help would be appreciated.


    --
    Regards,

    Richard Kut
    Database Administrator
    Research & Development
    Intelerad Medical Systems Inc.
    460 Ste-Catherine West, Suite 210
    Montreal, Quebec, Canada H3B 1A7
    Tel: 514.931.6222 x7733
    Fax: 514.931.4653
    rkut@intelerad.com
    www.intelerad.com

    This email or any attachments may contain confidential or legally
    privileged information intended for the sole use of the addressees. Any
    use, redistribution, disclosure, or reproduction of this information,
    except as intended, is prohibited. If you received this
    email in error, please notify the sender and remove all copies of the
    message, including any attachments.

    ---------------------------(end of broadcast)---------------------------
    TIP 1: if posting/reading through Usenet, please send an appropriate
    subscribe-nomail command to majordomo@postgresql.org so that your
    message can get through to the mailing list cleanly
  • S anwar at Mar 16, 2006 at 5:38 pm
    I don't think that your JDBC driver does not honor the fetch-size.
    See: http://jdbc.postgresql.org/todo.html
    On 3/13/06, Richard Kut wrote:

    Hello!

    I am trying to use a cursor in Java to fetch multiple rows at
    once. The code
    seems to connect to the database okay, and seems to be fetching records,
    but
    then it fails with the error:

    Exception in thread "main" java.lang.OutOfMemoryError


    Here is the Java code:


    import java.sql.*;

    /*
    * To compile:
    *
    * /usr/bin/jikes -source 1.4
    -bootclasspath
    /usr/java/j2sdk1.4.2_02/jre/lib/rt.jar:/usr/java/j2sdk1.4.2_02/jre/lib/jsse.jar:/usr/java/j2sdk1.4.2_02/jre/li
    b/jce.jar test_connect.java
    *
    *
    * To run:
    *
    * java
    -cp /usr/share/java/postgresql-
    8.1-404.jdbc2.jar:/usr/java/j2sdk1.4.2_02/jre/lib/rt.jar:/usr/java/j2sdk1.4.2_02/jre/lib/jsse.jar:/usr/java/j2sdk1.4
    .
    2_02/jre/lib/jce.jar:. test_connect
    */
    class test_connect
    {
    private static Connection connectToDb( String dbName,
    String username,
    String password ) throws
    Exception
    {
    System.out.println( "Connecting to DB " + dbName + ", username " +
    username );
    String dbDriver = "org.postgresql.Driver";
    String url = "jdbc:postgresql://imsstd4/" + dbName;
    Class.forName( dbDriver ).newInstance();

    System.out.println( "Connecting to " + url + " with user " +
    username
    + " and pass " + password );

    Connection connection = DriverManager.getConnection( url,
    username,
    password );

    System.out.println( "Connection successful" );

    return connection;
    }

    private static void executeQuery( Connection connection ) throws
    SQLException
    {
    String sql = "SELECT * FROM pid, pv1 where
    pid.patient_id_internal_id
    = pv1.patient_id_internal_id";

    System.out.println( "Executing query: " + sql );
    Statement st = connection.createStatement();

    System.out.println( "Setting cursor size to 50 rows." );
    st.setFetchSize( 50 ); // turn on cursor
    ResultSet rs = st.executeQuery( sql );
    System.out.println( "Got 50 results. Now walking them..." );
    int i = 0;
    while ( rs.next() )
    {
    // Do not print anything here for now
    i++;
    System.out.println( "Row " + i );
    }
    System.out.println( "Result set consumed " + i + " rows." );
    }

    public static void main( String[] args )
    {
    System.out.println( "Starting up..." );

    try
    {
    Connection connection = connectToDb( "hl7segmentsihe", "hl7",
    "segments" );

    executeQuery( connection );
    }
    catch ( Exception e )
    {
    System.out.println( "Got exception " + e );
    e.printStackTrace();
    }
    }
    }


    Here is the program output:


    Starting up...
    Connecting to DB hl7segmentsihe, username hl7
    Connecting to jdbc:postgresql://imsstd4/hl7segmentsihe with user hl7 and
    pass
    segments
    Connection successful
    Executing query: SELECT * FROM pid, pv1 where pid.patient_id_internal_id =
    pv1.patient_id_internal_id
    Setting cursor size to 50 rows.
    Exception in thread "main" java.lang.OutOfMemoryError


    What did I do wrong? Any help would be appreciated.


    --
    Regards,

    Richard Kut
    Database Administrator
    Research & Development
    Intelerad Medical Systems Inc.
    460 Ste-Catherine West, Suite 210
    Montreal, Quebec, Canada H3B 1A7
    Tel: 514.931.6222 x7733
    Fax: 514.931.4653
    rkut@intelerad.com
    www.intelerad.com

    This email or any attachments may contain confidential or legally
    privileged information intended for the sole use of the addressees. Any
    use, redistribution, disclosure, or reproduction of this information,
    except as intended, is prohibited. If you received this
    email in error, please notify the sender and remove all copies of the
    message, including any attachments.

    ---------------------------(end of broadcast)---------------------------
    TIP 1: if posting/reading through Usenet, please send an appropriate
    subscribe-nomail command to majordomo@postgresql.org so that your
    message can get through to the mailing list cleanly

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppgsql-novice @
categoriespostgresql
postedMar 13, '06 at 6:39p
activeMar 16, '06 at 5:38p
posts3
users3
websitepostgresql.org
irc#postgresql

People

Translate

site design / logo © 2022 Grokbase