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