Skip site navigation (1) Skip section navigation (2)

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 (view raw or flat)
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

pgsql-jdbc by date

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

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group