Обсуждение: PreparedStatement, getLastOID() and java.lang.ClassCastException

Поиск
Список
Период
Сортировка

PreparedStatement, getLastOID() and java.lang.ClassCastException

От
tomasz brymora
Дата:
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

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

От
Dave Cramer
Дата:
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