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条)

报告相同问题?

悬赏问题

  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题