如题,代码如下:
    /*
     * DAO具体方法实现
     * 编辑帐户 zhwei 2006-04-13
     * @param identityID 帐户ID,-1时添加,否则修改;name 用户名;indentityType 帐户类型
     * @return int 成功返回帐户的ID,否则返回-1
     * @throws SQLException
     */
    private int EditSecurityIdentity(int identityID,String name,String indentityType) throws SQLException{
        int returnID=-1;
        Connection cn=null;
        try{
            cn=dm.getConnection();//dm是DBConnectionManager
            boolean bln=false;
            String strSQL="";
            if(identityID==-1){
                strSQL="insert into SecurityIdentity (Name,IdentityType)";
                strSQL=strSQL + " values ('" + name + "','" + indentityType + "')";
            }else{
                strSQL="update SecurityIdentity set Name='" + name + "',";
                strSQL=strSQL + "IdentityType='" + indentityType + "'";
                strSQL=strSQL + " where ID=" + identityID;
             }
             System.out.println("EditSecurityIdentity SQL:" + strSQL);
            //执行编辑
            Statement stmt=cn.createStatement();
            int exeRow=stmt.executeUpdate(strSQL);
            System.out.println("EditSecurityIdentity Add ExeRowCount=" + exeRow);
//            cn.commit();//使用此句是提示:在事务管理过程中不能Commit
            System.out.println(bln);
            if(identityID==-1){
                System.out.println("--EditSecurityIdentity Success--");
                strSQL="select ID from SecurityIdentity where ";
                strSQL=strSQL + "Name='" + name + "' and IdentityType='" + indentityType + "'";
                ResultSet rs = stmt.executeQuery(strSQL);
                if(rs.next())
                    returnID=rs.getInt("ID");//编辑成功,返回帐户ID
                rs.close();
                rs=null;
            }else
                returnID=identityID;
            stmt.close();
            stmt=null;
       }catch(SQLException ex){
           System.out.println("业务操作异常,"+ex.getMessage());
           throw new SQLException(ex.getMessage());
       }catch(DBConnectException ex){
           System.out.println("数据库连接异常,"+ex.getMessage());
           throw new SQLException(ex.getMessage());
       }catch(Exception ex){
           System.out.println("怪异异常,"+ex.getMessage());
           throw new SQLException("怪异异常,"+ex.getMessage());
       }finally{
           cn=null;
       }
       System.out.println("EditSecurityIdentity returnID=" + returnID);
       return returnID;
    }
SQL语句正确,exeRow=1,returnID=96,但数据表中就是没有记录。请问这是什么原因?应该如何解决?谢谢!

解决方案 »

  1.   

    把rs.close();stmt.close();cn.close();都关掉试一下.然后再刷新数据库
      

  2.   

    你是否是自动提交conn.autocommit设置为false,所以只是在数据库缓冲区update了,这种情况如果没有commit,并不会写到数据库表中去
      

  3.   

    wsk_228(qing_feng):当执行cn.setAutoCommit(true);时一样提示:You cannot set autocommit during a managed transaction!
      

  4.   

    不是要你在代码中写呀,要设数据库里的autocommit;
      

  5.   

    是事务的原因,在进行了Insert、Update、Delete后不可以Select。各位针对这个问题有什么解决方法吗?
      

  6.   

    执行事务的sql之后,再执行commit,最后执行select.
    一般来说只有写操作的sql才有必要做为一个原子操作写事务.select一般很少写在事务里面.