<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Junky's IT Notebook

    統(tǒng)計(jì)

    留言簿(8)

    積分與排名

    WebSphere Studio

    閱讀排行榜

    評(píng)論排行榜

    利用 JdbcTemplate 自動(dòng)返回 MS SQL SERVER 2005 自增主鍵值

    JDBC3 中可以直接獲取當(dāng)前插入記錄的 ID 值,具體的調(diào)用方式如下:

    Statement stmt = conn.createStatement();
    stmt.executeUpdate(
    "INSERT INTO authors (first_name, last_name) values
     (′George′, ′Orwell′)
    ", Statement.RETURN_GENERATED_KEYS);
    ResultSet rs 
    = stmt.getGeneratedKeys();
    if ( rs.next() ) {
        
    int key = rs.getInt();
    }

    由于實(shí)際與數(shù)據(jù)庫(kù)交互采用的是 JdbcTemplate,因而需要找到它對(duì)這種方式的支持。經(jīng)過(guò)實(shí)際的查看 Spring 的 API 發(fā)現(xiàn)其本身提供相應(yīng)的方法支持,經(jīng)過(guò)多次的實(shí)驗(yàn)后得到如下的實(shí)現(xiàn)方法:

    private void insert(final Profile profile){
        
    final String _save = "insert into Newsletter_Profile (user_id, publication_id, last_update) values (?, ?, getdate())";
        JdbcTemplate template 
    = this.getJdbcTemplate();
        KeyHolder keyHolder 
    = new GeneratedKeyHolder();
        template.update(
    new PreparedStatementCreator() {
            
    public PreparedStatement createPreparedStatement(Connection con)
             
    throws SQLException {
                            
    int i = 0;
                            PreparedStatement ps 
    = con.prepareStatement(_save,
                 Statement.RETURN_GENERATED_KEYS);
                            ps.setInt(
    ++i, profile.getCustomerId().intValue());
                            ps.setInt(
    ++i, profile.getPublication().getId());
                            
    return ps;
                      }

                }
    , keyHolder);
                profile.setId(keyHolder.getKey().intValue());
          }
     

    特別需要注意的地方是Statement.RETURN_GENERATED_KEYS,在使用MS SQL Server 2005 提供的 JDBC Driver 中上面的部分是必須的。之所以這么說(shuō)是因?yàn)?google 出來(lái)的所有資料都是沒(méi)有該部分的,甚至 Spring 自身的 document 中也是沒(méi)有該參數(shù)的。我現(xiàn)在不知道那些代碼是否能夠真正的獲取到 Key,但是現(xiàn)在我 suppose 它們是可以 run 的。

    如果沒(méi)有加入 Statement.RETURN_GENERATED_KEYS  ,在實(shí)際進(jìn)行數(shù)據(jù)庫(kù)操作時(shí)會(huì)出現(xiàn)如下的異常:
    PreparedStatementCallback; uncategorized SQLException for SQL []; SQL state [null]; error code [0]; The statement must be executed before any results can be obtained.; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: The statement must be executed before any results can be obtained.
    caused by : com.microsoft.sqlserver.jdbc.SQLServerException: The statement must be executed before any results can be obtained.
    org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL []; SQL state [null]; error code [0]; The statement must be executed before any results can be obtained.; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: The statement must be executed before any results can be obtained.
    Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The statement must be executed before any results can be obtained.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getGeneratedKeys(Unknown Source)
    at weblogic.jdbc.wrapper.PreparedStatement_com_microsoft_sqlserver_jdbc_SQLServerPreparedStatement.getGeneratedKeys(Unknown Source)
    at org.springframework.jdbc.core.JdbcTemplate$3.doInPreparedStatement(JdbcTemplate.java:772)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:527)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:767)
    at com.fdc.reports20.dao.NewsletterDAO.insert(NewsletterDAO.java:179)
    at com.fdc.reports20.dao.NewsletterDAO.save(NewsletterDAO.java:153)
    at com.fdc.reports20.dao.NewsletterDAO.update(NewsletterDAO.java:138)
    at com.fdc.reports20.business.service.user.AlertServiceImpl.updateNewsletter(AlertServiceImpl.java:146)
    at com.fdc.reports20.business.service.user.AlertServiceImpl$$FastClassByCGLIB$$52b80fbc.invoke()
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
    at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:674)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:154)
    at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:52)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
    at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:53)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:615)
    at com.fdc.reports20.business.service.user.AlertServiceImpl$$EnhancerByCGLIB$$a12ee5d8.updateNewsletter()
    at com.fdc.reports20.web.delegate.AlertBD.updateNewsletter(AlertBD.java:78)
    at com.fdc.reports20.web.jpf.um.workbench.WorkBenchController.editPublicationEmails(WorkBenchController.java:149)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    posted on 2007-07-11 13:54 junky 閱讀(3926) 評(píng)論(0)  編輯  收藏 所屬分類: SQL Server

    主站蜘蛛池模板: 国产精品va无码免费麻豆| 亚洲AV色无码乱码在线观看| 免费大学生国产在线观看p| 亚洲一区二区三区免费观看| 国产综合免费精品久久久| 激情小说亚洲图片| 国产亚洲精品成人AA片| 日本久久久久亚洲中字幕| 亚洲午夜爱爱香蕉片| 国产精品极品美女免费观看| 桃子视频在线观看高清免费完整| 中国亚洲女人69内射少妇| 免费看污成人午夜网站| 伊人久久免费视频| 另类免费视频一区二区在线观看| 亚洲91av视频| 国产亚洲福利精品一区| 337p日本欧洲亚洲大胆裸体艺术| 久9久9精品免费观看| 很黄很污的网站免费| 91免费国产视频| 一区二区三区视频免费| 国产精品亚洲AV三区| 亚洲欧美日韩国产成人| 亚洲熟妇无码一区二区三区 | 好爽又高潮了毛片免费下载| 蜜臀98精品国产免费观看| 久久黄色免费网站| 国精产品一区一区三区免费视频| 亚洲欧洲日韩综合| 亚洲最大的视频网站| 亚洲成aⅴ人片在线观| 亚洲最新黄色网址| 久久精品国产亚洲av麻豆图片| 免费一看一级毛片人| 免费一级毛片正在播放| 国产乱子伦片免费观看中字| 波多野结衣免费视频观看| 无码不卡亚洲成?人片| 亚洲精品国精品久久99热| 亚洲中文字幕无码专区|