有人可以帮我解决如何在当前页面中找到 last_id 的问题吗?我使用查询public void storeInsuranceInformationToDataBase(int patientId) throws SQLException
{
//It gives arrayList of isuranceIds which are already in the data base.
ArrayList<Integer> insuranceIdsListInDatabase = getInsuranceIdsListInDatabase();
//It returns HashMap of insurance ids for given patient id.
HashMap<Integer, InsuranceInfo> insuranceMap = getInsuranceDetails(patientId);
Map<Integer, InsuranceInfo> tempInsuranceMap = new HashMap<>();
Set<Integer> insuranceIdsInHashMap = insuranceMap.keySet();
//Here I am getting ConcurrentModificationException
for (int insuranceIdInHashMap : insuranceIdsInHashMap)
{
//Here I am comparing the both ids..
if (insuranceIdsListInDatabase.contains(insuranceIdInHashMap))
{
String updateInsuranceQuery = "UPDATE jp_InsuranceInfo SET typeOfPolicy='" + insuranceMap.get(insuranceIdInHashMap).getTypeOfPolicy() + "', amount=" + insuranceMap.get(insuranceIdInHashMap).getAmount() + ", duration=" + insuranceMap.get(insuranceIdInHashMap).getDuration()
+ ", companyName='" + insuranceMap.get(insuranceIdInHashMap).getCompanyName() + "' WHERE insuranceId=" + insuranceIdInHashMap + ";";
getDataBase().getStatement().executeUpdate(updateInsuranceQuery);
}
else
{
String insertInsuranceQuery = "INSERT INTO jp_InsuranceInfo VALUES(" + insuranceMap.get(insuranceIdInHashMap).getPatientId() + ",'" + insuranceMap.get(insuranceIdInHashMap).getTypeOfPolicy() + "'," + insuranceMap.get(insuranceIdInHashMap).getAmount() + ","
+ insuranceMap.get(insuranceIdInHashMap).getDuration() + ",'" + insuranceMap.get(insuranceIdInHashMap).getCompanyName() + "')";
getDataBase().getStatement().executeUpdate(insertInsuranceQuery);
ResultSet generatedInsuranceId = getDataBase().getStatement().executeQuery("SELECT SCOPE_IDENTITY()");
int newInsuranceId = 0;
if (generatedInsuranceId.next())
{
newInsuranceId = generatedInsuranceId.getInt(1);
}
//Here it returns an insurance object which contains insurance details
InsuranceInfo insuranceObject = insuranceMap.get(insuranceIdInHashMap);
insuranceObject.setInsuranceId(newInsuranceId);
//Here I am trying to store the newly inserted insurance object into main hashmap
storeInsuranceInfoToHashMap(patientId, insuranceObject); <-- make this function to save those information into the temporary map
//I am removing previous insurance object with temporary insurance id
getInsuranceMap().get(patientId).remove(insuranceIdInHashMap);
// Adding newly generated insurance id to array list
getInsuranceIdsListInDatabase().add(newInsuranceId);
}
}
insuranceMap.putAll(tempInsuranceMap);
}
我想要页面第50条记录的ID,并希望在下面的查询中使用它:
db.users.find().limit(50)
谢谢!
答案 0 :(得分:0)
我猜你的意图是用MongoDB做分页。我们有更好的方法来做到这一点。例如:
//Page 1
db.users.find().skip(0).limit (10)
//Page 2
db.users.find().skip(10).limit(10)
//Page 3
db.users.find().skip(20).limit(10)
根据您显示的页面更改跳过值(即(N-1)* 10)。