首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

怎么从数据库中取出最后一行的主键

2013-12-05 
如何从数据库中取出最后一行的主键?我的代码:public int getLastMemberID() {int ID0String queryStr

如何从数据库中取出最后一行的主键?
我的代码:

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

热点排行