日期:2014-05-20  浏览次数:20692 次

如何从数据库中取出最后一行的主键?
我的代码:
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’

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