LeeAtomic 2017-03-01 02:52 采纳率: 75%
浏览 1209
已采纳

使用springjdbc中的update方法获取Oracle数据库中自增的主键报错

源码如下:
/**
* 获取插入时的主键值
*/
public int getKey(Order o) {
final String sql = "insert into tb_order (user_id,order_money,address, postalcode,phone,mobile,email,sendType,payType, postscript) values (?,?,?,?,?,?,?,?,?,?)";
final int user_id = o.getUserId();
int i = 0;
final double order_money = o.getOrderMoney();
final String address = o.getAddress();
final String postalcode = o.getPostalcode();
final String phone = o.getPhone();
final String mobile = o.getMobile();
final String email = o.getEmail();
final String sendType = o.getSendType();
final String payType = o.getPayType();
final String postscript = o.getPostScript();
KeyHolder keyHolder = new GeneratedKeyHolder();
template.update(new PreparedStatementCreator() {

        @Override
        public PreparedStatement createPreparedStatement(Connection arg0)
                throws SQLException {
            PreparedStatement ps = DBUtils.getDs().getConnection()
                    .prepareStatement(sql);
            ps.setInt(1, user_id);
            ps.setDouble(2, order_money);
            ps.setString(3, address);
            ps.setString(4, postalcode);
            ps.setString(5, phone);
            ps.setString(6, mobile);
            ps.setString(7, email);
            ps.setString(8, sendType);
            ps.setString(9, payType);
            ps.setString(10, postscript);
            return ps;
        }
    }, keyHolder);
    i = keyHolder.getKey().intValue();
    return i;
}
public static void main(String[] args) {
    Order o = new Order(7, 25, "hk", "432222", "1111", "1214334", "3453435",
            "1", "1", "无");
    IOrderDaoImpl dao = new IOrderDaoImpl();
    System.out.println(dao.getKey(o));
}
错误日志:
三月 01, 2017 10:45:47 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

信息: Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
三月 01, 2017 10:45:47 上午 org.springframework.jdbc.support.SQLErrorCodesFactory
信息: SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
Exception in thread "main" org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL []; SQL state [null]; error code [17090]; 不允许的操作; nested exception is java.sql.SQLException: 不允许的操作
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:83)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:607)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:824)
at dao.impl.IOrderDaoImpl.getKey(IOrderDaoImpl.java:58)
at dao.impl.IOrderDaoImpl.main(IOrderDaoImpl.java:85)
Caused by: java.sql.SQLException: 不允许的操作
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
at oracle.jdbc.driver.OracleStatement.getGeneratedKeys(OracleStatement.java:4559)
at org.apache.commons.dbcp.DelegatingStatement.getGeneratedKeys(DelegatingStatement.java:318)
at org.springframework.jdbc.core.JdbcTemplate$3.doInPreparedStatement(JdbcTemplate.java:829)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:591)
... 3 more
程序走到template.update方法时报错

  • 写回答

3条回答

  • 半城风月半城雪 2017-03-01 03:26
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试