我的代码:
public int getLastMemberID() {
        int ID=0;
        String queryStr = "SELECT * FROM " + tableName;
        try{
       stmt = conn.prepareStatement(queryStr);
        ResultSet rs = stmt.executeQuery();
        while(rs.last()){
        ID=rs.getInt("MEMBERID");
        }
        }catch(SQLException ex){
             JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }
        return ID;
    }
但出现错误
This method should only be called onResultSet object taht are scollable(type TYPE_SCROLL_INSENSITIVE).和
The statement was aborted because it wold have caused a duplicate key value in a unique or primary key constraint or unique index identified by ‘SQL131109134757700’
defined on ‘MEMBERDATA’

解决方案 »

  1.   

    你这样用while肯定会报错的啊。
    不建议采用rs.last()方式,如果坚持要用这种方式,那你就应该修改游标类型。建议采用排序的方式直接取最后一条,各个数据库可能具体语句不一样,
    mysql:采用limit关键字
    oracle:采用rownum关键字
    mssql:采用top关键字
      

  2.   

    rs.last()就直接滚到最后一行了。不要用while
      

  3.   

    select max(主键) from ...
      

  4.   

    SELECT * FROM table order by id desc limit 1