Thread: Re: [JDBC] Prepare Statement

Re: [JDBC] Prepare Statement

From
"Jie Liang"
Date:
Kris,
Thank you for your valuable response, I used the code you list
following:
import java.sql.*;

public class ServerSidePreparedStatement
{

    public static void main(String args[]) throws Exception
    {
        Class.forName("org.postgresql.Driver");
        String url = "jdbc:postgresql://localhost:5432/test";
        Connection conn =
DriverManager.getConnection(url,"test","");

        PreparedStatement pstmt = conn.prepareStatement("SELECT
?");

        // cast to the pg extension interface
        org.postgresql.PGStatement pgstmt =
(org.postgresql.PGStatement)pstmt;

        // on the third execution start using server side
statements
        pgstmt.setPrepareThreshold(3);

        for (int i=1; i<=5; i++)
        {
            pstmt.setInt(1,i);
            boolean usingServerPrepare =
pgstmt.isUseServerPrepare();
            ResultSet rs = pstmt.executeQuery();
            rs.next();
            System.out.println("Execution: "+i+", Used
server side: " + usingServerPrepare + ", Result: "+rs.getInt(1));
            rs.close();
        }

        pstmt.close();
        conn.close();
    }
}
Then, the compiler complaint:
ServerSidePreparedStatement.java:20: cannot resolve symbol symbol  :
method setPrepareThreshold  (int)
location: interface org.postgresql.PGStatement
                pgstmt.setPrepareThreshold(3);
I downloaded pg74.213.jdbc2.jar and pg74.213.jdbc2ee.jar at
http://jdbc.postgresql.org/download.html
And had a try, I got same error msg.

I use java 1.3.1, postgresql -7.4.2, FreeBSD 4.7

What I need to do to make it work??

Thanks.



Jie Liang

-----Original Message-----
From: Kris Jurka [mailto:books@ejurka.com]
Sent: Tuesday, June 15, 2004 11:00 AM
To: Jie Liang
Cc: Tom Lane; pgsql-sql@postgresql.org; pgsql-jdbc@postgresql.org
Subject: Re: [JDBC] Prepare Statement




On Mon, 14 Jun 2004, Jie Liang wrote:

> I have a question about performance, in SQL commands: there is a
> prepare/execute command, document says it will improve the performance

> while repeatly execute a statement. In java.sql: there is a
> PreparedStatement object, which can store precompiled SQL statement,
> document says it can improve the performance also. If I use java jdbc
> to connect postgresql database, which one I should use? Can I use
> both?
>

When using JDBC it is best to use the standard
Statement/PreparedStatement
interfaces.  It is possible to directly use PREPARE/EXECUTE, but this
can
be handled by the driver.  Let me give you a run down of the different
driver versions and their capabilities:

Current released version: can enable using PREPARE/EXECUTE behind the
scenes on PreparedStatement by casting the prepared statement to
PGStatement and issuing setUseServerPrepare.

Current cvs version: can enable using PREPARE/EXECUTE by setting an
execution threshold that will turn it on when reached.  This threshold
can
be set at a number of levels, see the following for more information

http://www.ejurka.com/pgsql/docs/cvs/ch09s05.html

Soon to be committed cvs version: can directly use server prepared
statements without using the SQL level PREPARE/EXECUTE.

Kris Jurka

Re: [JDBC] Prepare Statement

From
Kris Jurka
Date:

On Wed, 16 Jun 2004, Jie Liang wrote:

> Kris,
> Thank you for your valuable response, I used the code you list
> following:
>
> [7.5 code example]
>
> Then, the compiler complaint:
> ServerSidePreparedStatement.java:20: cannot resolve symbol symbol  :
> method setPrepareThreshold  (int)
> location: interface org.postgresql.PGStatement
>                 pgstmt.setPrepareThreshold(3); I downloaded
> pg74.213.jdbc2.jar and pg74.213.jdbc2ee.jar at

This example is from the 7.5 documentation and requires a
pgdev.302.jdbcX.jar file.  I mentioned this cvs example because this
functionality is undocumented in the released version.  In the 7.4 version
the enabling of server side statements is only possible via a boolean flag
at the statement level, namely PGStatement.setUseServerPrepare(true);

Kris Jurka