Re: PreparedStatement, getLastOID() and java.lang.ClassCastException

Lists: pgsql-jdbc
From: tomasz brymora <tomekpilot(at)yahoo(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: PreparedStatement, getLastOID() and java.lang.ClassCastException
Date: 2006-11-07 17:19:39
Message-ID: 20061107171939.43807.qmail@web56606.mail.re3.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

Greetings Everyone!

I've been beating my head against a wall trying to retrieve an OID form a table, and I finally got a java.lang.ClassCastException at runtime.
The line of code this exception complains about:
long insertedOID = ( (PGStatement) ps ).getLastOID();

... ps is the PreparedStatement I'm using. The entire class follows:

package Contest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.postgresql.PGStatement;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.PreparedStatementCreator;

public class SavePoem {
public SavePoem ( DataSource dataSource ) {
this.dataSource = dataSource;
}

private DataSource dataSource;
private Connection connection;
private JdbcTemplate jdbcTemplate;

public int savePoem( final Poem poem ) throws Exception {
return Integer.parseInt ( (String) jdbcTemplate.execute (
new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement ( Connection connection ) throws SQLException {
PreparedStatement ps = connection.prepareStatement ( "INSERT INTO poems_2006 ( poet_name, email, phone, company, poem_title, poem_body ) VALUES ( ?, ?, ?, ?, ?, ? )" );
ps.setString ( 1, poem.getPoetName () );
ps.setString ( 2, poem.getEmail () );
ps.setString ( 3, poem.getPhone () );
ps.setString ( 4, poem.getCompany () );
ps.setString ( 5, poem.getPoemTitle () );
ps.setString ( 6, poem.getPoemBody () );
return ps;
}// PreparedStatement
}, // new PreparedStatementCreator

new PreparedStatementCallback () {

public Object doInPreparedStatement ( PreparedStatement ps ) throws SQLException , DataAccessException {
long insertedOID = ( (PGStatement) ps ).getLastOID();
return Integer.valueOf (String.valueOf ( insertedOID ));
}// doInPreparedStatement
}// PreparedStatementCallback
)
);
}// savePoem
public void setJdbcTemplate ( JdbcTemplate jdbcTemplate ) {
this.jdbcTemplate = jdbcTemplate;
}

}// end Poem


From: Dave Cramer <pg(at)fastcrypt(dot)com>
To: tomasz brymora <tomekpilot(at)yahoo(dot)com>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: PreparedStatement, getLastOID() and java.lang.ClassCastException
Date: 2006-11-08 12:31:59
Message-ID: F8FDB979-B872-40E3-8EA3-51D4B4821F72@fastcrypt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

Hi,

First of all you should be aware that oid's are not guaranteed to be
unique, secondly they are not in tables by default in future versions
of postgres

Secondly my guess is spring is wrapping your statement inside a
spring object.

Dave

On 7-Nov-06, at 12:19 PM, tomasz brymora wrote:

> Greetings Everyone!
>
> I've been beating my head against a wall trying to retrieve an OID
> form a table, and I finally got a java.lang.ClassCastException at
> runtime.
> The line of code this exception complains about:
> long insertedOID = ( (PGStatement) ps ).getLastOID();
>
> ... ps is the PreparedStatement I'm using. The entire class follows:
>
>
> package Contest;
> import java.sql.Connection;
> import java.sql.PreparedStatement;
> import java.sql.SQLException;
> import javax.sql.DataSource;
> import org.postgresql.PGStatement;
> import org.springframework.dao.DataAccessException;
> import org.springframework.jdbc.core.JdbcTemplate;
> import org.springframework.jdbc.core.PreparedStatementCallback;
> import org.springframework.jdbc.core.PreparedStatementCreator;
>
> public class SavePoem {
> public SavePoem ( DataSource dataSource ) {
> this.dataSource = dataSource;
> }
>
> private DataSource dataSource;
> private Connection connection;
> private JdbcTemplate jdbcTemplate;
>
> public int savePoem( final Poem poem ) throws Exception {
> return Integer.parseInt ( (String) jdbcTemplate.execute (
> new PreparedStatementCreator() {
> public PreparedStatement
> createPreparedStatement ( Connection connection ) throws
> SQLException {
> PreparedStatement ps =
> connection.prepareStatement ( "INSERT INTO poems_2006 ( poet_name,
> email, phone, company, poem_title, poem_body ) VALUES
> ( ?, ?, ?, ?, ?, ? )" );
> ps.setString ( 1, poem.getPoetName () );
> ps.setString ( 2, poem.getEmail () );
> ps.setString ( 3, poem.getPhone () );
> ps.setString ( 4, poem.getCompany () );
> ps.setString ( 5, poem.getPoemTitle () );
> ps.setString ( 6, poem.getPoemBody () );
> return ps;
> }// PreparedStatement
> }, // new PreparedStatementCreator
>
> new PreparedStatementCallback () {
>
> public Object doInPreparedStatement
> ( PreparedStatement ps ) throws SQLException , DataAccessException {
> long insertedOID = ( (PGStatement)
> ps ).getLastOID();
> return Integer.valueOf (String.valueOf
> ( insertedOID ));
> }// doInPreparedStatement
> }// PreparedStatementCallback
> )
> );
> }// savePoem
> public void setJdbcTemplate ( JdbcTemplate jdbcTemplate ) {
> this.jdbcTemplate = jdbcTemplate;
> }
>
> }// end Poem