u013343382
亲亲小软
2015-12-17 10:10

spring4.0+jdbc+oracle获取自增主键

  • oracle
  • jdbc
  • 数据库

最近在用spring4.0,用spring的jdbctemplate,从oracle数据库中添加数据,然后获取自增主键,可是出现的问题,可以正常的插入数据但是不能获取到主键值,请大家帮忙看看。我用的oracle驱动是ojdbc6.jar,oracle是11g,下面是我的代码

//保存数据的代码

public int saveProduct(){
KeyHolder kh = new GeneratedKeyHolder();
final String sql = "insert into n_show(id,cname) values(seq_niupai.nextval,?)";
jdbc.update(new PreparedStatementCreator() {

        @Override
        public PreparedStatement createPreparedStatement(Connection conn)
                throws SQLException {
            // TODO Auto-generated method stub
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, "111");
            return ps;
        }
    }, kh);
    int key = kh.getKey().intValue();
    return key;
}

运行后能正常的插入数据

下面是错误,有点长,请大家耐心看看,谢谢!!

8:01:10,470 DEBUG org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator:281 - Unable to translate SQLException with Error code '17090', will now try the fallback translator
18:01:10,470 DEBUG org.springframework.jdbc.support.SQLStateSQLExceptionTranslator:95 - Extracted SQL state class '99' from value '99999'
18:01:10,471 DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver:134 - Resolving exception from handler [public java.lang.String com.lanrui.web.shopadmin.ProductSetController.saveorupdatePro(javax.servlet.http.HttpServletRequest,org.springframework.ui.ModelMap)]: org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL []; SQL state [99999]; error code [17090]; 不允许的操作; nested exception is java.sql.SQLException: 不允许的操作
18:01:10,473 DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver:134 - Resolving exception from handler [public java.lang.String com.lanrui.web.shopadmin.ProductSetController.saveorupdatePro(javax.servlet.http.HttpServletRequest,org.springframework.ui.ModelMap)]: org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL []; SQL state [99999]; error code [17090]; 不允许的操作; nested exception is java.sql.SQLException: 不允许的操作
18:01:10,474 DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver:134 - Resolving exception from handler [public java.lang.String com.lanrui.web.shopadmin.ProductSetController.saveorupdatePro(javax.servlet.http.HttpServletRequest,org.springframework.ui.ModelMap)]: org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL []; SQL state [99999]; error code [17090]; 不允许的操作; nested exception is java.sql.SQLException: 不允许的操作
18:01:10,475 DEBUG org.springframework.web.servlet.DispatcherServlet:969 - Could not complete request
org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL []; SQL state [99999]; error code [17090]; 不允许的操作; nested exception is java.sql.SQLException: 不允许的操作
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:84)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:630)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:913)
at com.lanrui.dao.ProductDao.saveProduct(ProductDao.java:107)
at com.lanrui.service.ProductService.saveproduct(ProductService.java:58)
at com.lanrui.web.shopadmin.ProductSetController.saveorupdatePro(ProductSetController.java:110)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答