你可以自己编写一个产生序列的类,把setparm里的参数换为你这个产生序列的类产生的值就可以了

解决方案 »

  1.   

    public AccountPK ejbCreate(java.math.BigDecimal id, String name, Double moneyTotal) throws CreateException, NSException {
            String sql="";
            Connection conn = null;
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            this.moneyTotal = moneyTotal;
            try{
                /**
                 *使用公有类中ContextUtil类的
                 * 公有方法getConnection()
                 * 获得数据库连接
                 */
                System.out.println("AccountBean 中的 ejbcreat() called");
                conn = ConnectionUtil.getConnection();
                //自动生成career表中的id号码
                sql = "select newstar_id.nextval ID from dual";
                pstmt = conn.prepareStatement(sql);
                rs = pstmt.executeQuery();
                rs.next();
                this.id = rs.getBigDecimal("ID");
                System.out.println("Account 表中的id ========" + this.id);
                this.name = name;
                this.moneyTotal = moneyTotal;
                System.out.println(" Name : MoneyTotal" + this.name + ":" + this.moneyTotal.doubleValue());
                sql = "insert into account( ID, NAME, MONEY_TOTAL) values (?,?,?)";
                pstmt = conn.prepareStatement(sql);
                pstmt.setBigDecimal(1, this.id);
                pstmt.setString(2, this.name);
                if (this.moneyTotal != null)
                    pstmt.setDouble(3, this.moneyTotal.doubleValue());
                else
                    pstmt.setDouble(3, 0.0);
                pstmt.executeUpdate();
                /**
                 * 生成主键并且返回主键值
                 */
                return new AccountPK(this.id);
            }
            catch (Exception e) {
                /**
                 * Exception 将公用
                 *使用异常类来替代
                 */
                System.out.println("AccountBean 中的 ejbcreat() 出错");
                e.printStackTrace();
                throw NSExceptionUtil.getNSException("创建一个账号错误。");
            }
            finally {
                try {
                    /**
                     * 关闭数据库的连接,
                     * 为其他Bean释放该连接。
                     */
                    if (pstmt != null)
                        pstmt.close();
                    if (conn != null)
                        conn.close();
                }
                catch (Exception e) {
                    System.out.println("关闭数据库连接出错(accountBean ejbcreate())");
                    e.printStackTrace();
                }
            }
        }
      

  2.   

    我只知道,CMP中不需要在ejbcreate()中传递pk的参数,但是BMP中好象需要自己构造method()来(select seq_name.nextval  from dual )来取出seq_number然后传递给ejbcreate().所以要区别对待。