PreparedStatement, getLastOID() and java.lang.ClassCastException

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
Thread:
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

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message ludovic orban 2006-11-08 09:39:21 Re: XA end then join fix for WebLogic
Previous Message Heikki Linnakangas 2006-11-07 12:31:53 Re: XA end then join fix for WebLogic