其实你做java开发数据库,最好不要使用Oracle ID自动加一的功能,要尽量做到数据库无关;
例如:
  public synchronized int getNewID() {
    keygen = KeyGenerator.getInstance();
    return keygen.getNextCurrency();
  }
---取得new ID的方法
public class KeyGenerator
{
    private static KeyGenerator keygen = new KeyGenerator();
    private static int Currency_ID = getCurrencyID()+1;
    private KeyGenerator() { }    public static KeyGenerator getInstance()
    {
        return keygen;
    }    public synchronized int getNextCurrency()
    {
        return Currency_ID++;
    }
    private static int getCurrencyID(){
      int Currency_ID=0;
      DBconnect dbconn = new DBconnect();
      try {
        String SqlStr = "select MAX(Currency_ID) from v_Currency";
        ResultSet rs = dbconn.executeQuery(SqlStr);
        while(rs.next())
          Currency_ID=rs.getInt(1);
        rs.close();
      }
      catch (Exception ex) {
        System.err.println("SQLException: " + ex.getMessage());
        ex.printStackTrace();
      }
      finally {
        if (dbconn != null) {
          dbconn.CloseDataBase();
        }
      }
      return Currency_ID;
    }
}
---这是取得New ID的类; 其中数据库的连接,有很多例程,这里就不介绍了

解决方案 »

  1.   

    容易啊!~
    你只要在插入结束以后在select一下不就OK了?
      

  2.   

    jdk1.4中的JDBC statement对象有这个功能
      

  3.   

    jerrygui(jerrygui) 按你方法检索出来的比正确值大一
      

  4.   

    JAVA与模式中好像有讲这个,具体哪章忘了
      

  5.   

    Statement的getGeneratedKeys方法可以实现,不过要JDK1.4,而且数据库驱动也要支持该方法
      

  6.   

    插入后,再select,是不可以得,这样没办法处理并发问题
    给你一个sql server里的方法
    if exists (select 1
                from  sysobjects
               where  id = object_id('CreateLoginLog')
                and   type = 'P')
          drop procedure CreateLoginLog
    gocreate procedure CreateLoginLog 
    @USERCD VARCHAR(10), 
    @USERIP VARCHAR(30), 
    @SESSIONID VARCHAR(100), 
    @STADATE INT, 
    @STATIME INT, 
    @ID INT  OUTPUT
    asbegin transaction
    insert into T_LOGIN 
    (USERCD,USERIP,SESSIONID,STADATE,STATIME)
    values (@USERCD,@USERIP,@SESSIONID,@STADATE,@STATIME);

    select @ID=@@IDENTITY
    commit transaction
    go
      

  7.   

    在Oracle中,自动加一是用到一个对象sequence,只要一调用,sequence就会自动加一,我给出的例子是:取得下一个ID的值;并不是现在的值;
    import java.sql.*;
    import java.util.*;public class Currency {
      private String currency, desc;
      private static KeyGenerator keygen;
      private int autoNumber;
      public Currency() {  }  public Currency(String currency, String desc) {
        this.currency = currency;
        this.desc = desc;
      }
      public String insertCurrency(HashMap InsertData) throws
          ExistCurrencyException {
        String returnStr = "Insert successful!";
        Object strKey;
        DBconnect dbconn = new DBconnect();
        try {
          dbconn.conn.setAutoCommit(false);
          for (int i = 0; i < InsertData.size(); i++) {        strKey = InsertData.keySet().toArray()[i];
            ArrayList newdataList = (ArrayList) InsertData.get(strKey);
            if (newdataList.get(0) != null) {
              if (IsExistID(newdataList.get(0).toString(), "I")) {
                throw new ExistCurrencyException(newdataList.get(0).toString());
              }
              else {
                this.autoNumber = getNewID();
                String SqlStr = 
                    "INSERT INTO Tab_Currency(currency_id,currency_CODE,currency_desc) values(" +
                    autoNumber + ",'" + newdataList.get(0).toString().toUpperCase() +
                    "','" + newdataList.get(1).toString() + "')";
                System.out.println(SqlStr);
                dbconn.executeInsert(SqlStr);
              }
            }
          }
          dbconn.conn.commit();
          dbconn.CloseDataBase();
          return returnStr;
        }
        catch (Exception ex) {
          try {
            dbconn.conn.rollback();
          }
          catch (SQLException e) {
            System.out.println("execute query error:" + e.getMessage());
            e.printStackTrace();
          }
          System.out.println(ex.toString());
          ex.printStackTrace();
          returnStr= "Something error!!!";
        }
        finally {
          if (dbconn != null) {
            dbconn.CloseDataBase();
          }
          return returnStr;
        }
      }
      public int getAutoNumber(){
          return this.autoNumber;
      }
       public synchronized int getNewID() {
        keygen = KeyGenerator.getInstance();
        return keygen.getNextCurrency();
      }}public class KeyGenerator
    {
        private static KeyGenerator keygen = new KeyGenerator();
        private static int Currency_ID = getCurrencyID()+1;
        private KeyGenerator() { }    public static KeyGenerator getInstance()
        {
            return keygen;
        }    public synchronized int getNextCurrency()
        {
            return Currency_ID++;
        }
        private static int getCurrencyID(){
          int Currency_ID=0;
          DBconnect dbconn = new DBconnect();
          try {
            String SqlStr = "select MAX(Currency_ID) from v_Currency";
            ResultSet rs = dbconn.executeQuery(SqlStr);
            while(rs.next())
              Currency_ID=rs.getInt(1);
            rs.close();
          }
          catch (Exception ex) {
            System.err.println("SQLException: " + ex.getMessage());
            ex.printStackTrace();
          }
          finally {
            if (dbconn != null) {
              dbconn.CloseDataBase();
            }
          }
          return Currency_ID;
        }
    }
    只要再写一个调用的程序,就可以了; 
      

  8.   

    使用SELECT @@IDENTITY就行了。前题是要跟INSERT语句用的是同一个Connection.
      

  9.   

    最简单的方法://String tableName 表名, String pk 主键字段名, dhccrmsqlBean con 当前数据库连接
      public static synchronized int getPK(String tableName, String pk, dhccrmsqlBean con) {
        try {
          ResultSet rs = con.executeQuery("select max(" + pk + ") as maxID from " + tableName);      if (rs == null) {
            return 0;
          } else {
             while (rs.next()) {
               if (rs.getString("maxID") != null) {
                  return rs.getInt("maxID") + 1;
               } else {
                 return 0;
               }
             }
          }
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }
        return 0;
      }
      

  10.   

    用oracle自身的序列
    新建一个sequence,然后用一个触发器把新生成的id号加入到数据库里