PreparedStatement, getLastOID() and java.lang.ClassCastException
От | tomasz brymora |
---|---|
Тема | PreparedStatement, getLastOID() and java.lang.ClassCastException |
Дата | |
Msg-id | 20061107171939.43807.qmail@web56606.mail.re3.yahoo.com обсуждение исходный текст |
Ответы |
Re: PreparedStatement, getLastOID() and java.lang.ClassCastException
|
Список | 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
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
В списке pgsql-jdbc по дате отправления: