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

使用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 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。
  • ¥30 求给定范围的全体素数p的(p-2)/p的连乘积值
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip