我正在制作一个带有 selenium 的 java servlet,它循环一定次数并插入到数据库中。但循环在 1 INSERT 后停止。
while(i < x)
{
i++;
if(rs.next())
{
String st = rs.getString("st");
String ACCOUNT = rs.getString("User");
String link_URL = rs.getString("Link");
String objectKey = rs.getString("Key");
String DESC = "File description.";
String INF_DESC = " this is an unsual from";
WebElement acc = wait.until(ExpectedConditions.elementToBeClickable(By.id("web_account_"+i)));
WebElement desc = driver.findElement(By.id("web_original_"+i));
WebElement inf_desc = driver.findElement(By.id("web_se_"+i));
WebElement _url = driver.findElement(By.id("web_url_"+i));
List<WebElement> link_in_ = driver.findElements(By.name("web_target_"+i));
WebElement link_in__1 = driver.findElement(By.id("web_website_direct_"+i));
WebElement link_in__conf = driver.findElement(By.id("web_website_direct_confirm_"+i));
WebElement report_another = driver.findElement(By.id("more_link"));
acc.sendKeys(ACCOUNT);
top("Account: ", ACCOUNT, complete, response, 1);
desc.sendKeys(DESC);
top("Description: ", DESC, complete, response, 1);
inf_desc.sendKeys(INF_DESC);
top("Inf_Des: ", INF_DESC, complete, response, 1);
_url.sendKeys(link_URL);
top("URL: ", link_URL, complete, response, 1);
for(WebElement RadioOption : link_in_)
{
//if(RadioOption.getAttribute("value").equals("website"))
if("website".equals(RadioOption.getAttribute("value")))
RadioOption.click();
top("Radio in URL: ", clicked, complete, response, 1);
}
link_in_1.click();
top("Radio in URL2: ", clicked, complete, response, 1);
link_in_conf.click();
top("Radio URL3 Conf: ", clicked, complete, response, 1);
//store current webs in a temp table
st.executeUpdate("INSERT INTO temp_webs (ObjectKey) VALUES ('" + objectKey + "')");
if(i != x)
{
report_another.click();
writer.println("<tr><td>Report Another:</td><td colspan=\"2\">Click</td><td>Complete</td></tr>");
}
}
else
{
writer.println("There was an error in the MySQL query! ID:26");
}
}
我知道问题是st.executeUpdate("INSERT INTO temp_webs (ObjectKey) VALUES ('" + objectKey + "')");
阻止循环继续。当我删除这行代码时,它工作得很好。
更新 我已经确认查询有效并且一切都是正确的。我的问题是它只插入一条记录,然后退出 while 循环。
更新二
在尝试捕获中使用查询后出现新错误
Operation not allowed after ResultSet closed
,似乎 ResulSet 在完成循环之前关闭。不确定是什么原因造成的。