使用getSelectedIndices()时遇到问题。偶尔,并非总是如我在数据库代码中的调试会话中发现的那样,它返回一个包含索引-1的数组。
为什么会这样,我该怎么做才能防止这种情况发生?我想用这种风格的逻辑是各种各样的地方。
我有一个带有SelectionMode.MULTIPLE的Livestock TableView。 它有一个ContextMenu,其中包括(' Set Breed',' Set Sex'' Set Has Calf')
设置其中一个属性的逻辑是相同的: 用户将,(1)选择相关行(使用或+鼠标单击),然后(2)选择相关的MenuItem
然后将调用setLivestockAttribute(),如下所示:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="custom_text">SHIPPING RATES: €<span id="rotate"></span> </div>
答案 0 :(得分:0)
我决定:
代码段确保数据库更新,仅在索引i> = 0时才会更新。
public boolean updateLivestock(ObservableList<LivestockModel> livestockData, ObservableList<Integer> selectedIndices, String attribute, String value)
{
boolean proceed = true;
boolean updated = false;
int rowCount = 0;
int counter = 0;
String sql = "";
LivestockModel lm;
switch (attribute)
{
case LM_Constant.BREED:
sql = "UPDATE livestock SET (breed, last_updated) = (?, DEFAULT) WHERE rfid = ? AND begin_event = ?";
break;
case LM_Constant.SEX:
sql = "UPDATE livestock SET (sex, last_updated) = (?, DEFAULT) WHERE rfid = ? AND begin_event = ?";
break;
case LM_Constant.HAS_CALF:
sql = "UPDATE livestock SET (has_calf, last_updated) = (?, DEFAULT) WHERE rfid = ? AND begin_event = ?";
break;
}
try ( PreparedStatement preparedUpdate = connection.prepareStatement(sql) )
{
for (Integer i : selectedIndices)
{
if ( proceed != true )
break;
if ( i >= 0 )
{
lm = livestockData.get(i);
preparedUpdate.setString(1, value);
preparedUpdate.setString(2, lm.getRFID());
preparedUpdate.setInt(3, lm.getBeginEvent());
rowCount = preparedUpdate.executeUpdate();
if ( rowCount == 1 )
counter++;
}
else
{
counter++;
LM_Utility.showError_Dialog("Update Livestock", "Index Error", "index = " + i.toString());
}
}
if ( counter == selectedIndices.size() )
{
connection.commit();
updated = true;
}
}
catch (SQLException sqle)
{
LM_Utility.showSQL_Exception("updateLivestock()", sqle);
proceed = false;
}
return updated;
}