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

最近在用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个回答

这个问题已解决,这行PreparedStatement ps = conn.prepareStatement(sql); 修改为PreparedStatement ps = conn.prepareStatement(sql,new String[]{"id"});
加上需要获取的主键名即可,大家也可以参考java api 文档中的其他方法~~~哦哦~~

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问