我需要根據給定 SQL SENTENCE 拋出的行數進行回圈,并在每次迭代中分配列的值
這是我的代碼:
package first;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
public class assign_code{
static Connection conn = null;
static QueryRunner run = new QueryRunner();
static boolean keepConnection = false;
public static void createConn(String urlString, String driverr, String usernameString, String password) throws SQLException, IOException {
if (conn == null || conn.isClosed()) {
String driver = null;
DbUtils.loadDriver(driver);
conn = DriverManager.getConnection(urlString, usernameString, password);
conn.setAutoCommit(false);
}
}
public static List<Map<String, Object>> getResultInMapList(String urlString, String driverr, String usernameString, String password,String sqlQuery, Object... params) throws
SQLException, IOException {
try {
createConn(urlString,driverr,usernameString,password);
if (params == null) {
return run.query(conn, sqlQuery, new MapListHandler());
} else {
return run.query(conn, sqlQuery, new MapListHandler(), params);
}
} catch (SQLException se) {
se.printStackTrace();
return null;
} finally {
closeConn();
}
}
public static void closeConn() throws SQLException {
if (!keepConnection) {
DbUtils.closeQuietly(conn);
}
}
public static void main(String[] args) throws InterruptedException, SQLException, IOException{
System.setProperty("webdriver.chrome.driver","C:\\Users\\Steven\\Desktop\\SELENIUM\\chromedriver.exe");
String urlString="jdbc:sqlserver://GERTER5404.btqw.local:1433;databaseName=Test";
String usernameString="admin";
String password="admin";
String driverr="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String sqlQuery= "select APPROVERID from orders.Approvals where ITEMID = ?";
Map<String,Object>resultSet= getResultInMapList(urlString, driverr, usernameString, password, sqlQuery, "45");
WebDriver driver = new ChromeDriver();
driver.get("https://stage.com/backend");
driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.id("login")).sendKeys("12345678");
driver.findElement(By.id("add_new_option_button")).click();
driver.findElement(By.xpath("//*[@id=\"manage-options-panel\"]/table/tbody/tr[117]/td[3]/input")).sendKeys(resultSet.get("APPROVERID").toString());
driver.findElement(By.xpath("//*[@id=\"save\"]/a")).click();
Thread.sleep(3000);
}
}
假設我的 SQL QUERY在執行此操作后拋出3 行:
SQL查詢:
select APPROVERID from orders.Approvals where ITEMID = 45;
圖片:

我想根據 SQL QUERY 拋出的行數回圈并分配值
在第一個回圈中,我需要在第 1 行和第 4 列的值內設定引數 APPROVERID

在第二個回圈中,我需要在第 2 行和第 4 列的值內設定引數 APPROVERID

在第三個回圈中,我需要在第 3 行和第 4 列的值內設定引數 APPROVERID

我這樣做了:
一個名為“getResultInMapList”的函式:
public static List<Map<String, Object>> getResultInMapList(String urlString, String driverr, String usernameString, String password,String sqlQuery, Object... params) throws
SQLException, IOException {
try {
createConn(urlString,driverr,usernameString,password);
if (params == null) {
return run.query(conn, sqlQuery, new MapListHandler());
} else {
return run.query(conn, sqlQuery, new MapListHandler(), params);
}
} catch (SQLException se) {
se.printStackTrace();
return null;
} finally {
closeConn();
}
}
呼叫方法:
String sqlQuery= "select APPROVERID from orders.WebOrderItem where itemid = ?";
List<Map<String, Object>>resultSet=getResultInMapList(String url, String driver, String usr, String pwd,sqlQuery, "45");
for (Map<String, Object> rows: resultSet)
{ for (Map.Entry<String, Object> row: rows.entrySet())
{ driver.findElement(By.xpath("//*[@id=\"attributeGrid_filter_frontend_label\"]")).ssendKeys(resultSet.get("sku").toString()); }
}
我收到這條訊息:

我究竟做錯了什么?
我回圈好嗎?
編輯#1:
使用這句話后出現此錯誤:
List<Map<String, Object>>resultSet= getResultInMapList(urlString, driverr, usernameString, password, sqlQuery, "45");

List<Map<String,Object>> 型別中的方法 get(int) 不適用于引數 (String)
uj5u.com熱心網友回復:
你已經宣告
List<Map<String, Object>> getResultInMapList(String urlString, String driverr, String usernameString, String password,String sqlQuery, Object... params)`
并且在您正在呼叫的主要方法中
Map<String,Object>resultSet= getResultInMapList(urlString, driverr, usernameString, password, sqlQuery, "45")`
從未宣告過。因此錯誤
uj5u.com熱心網友回復:
您收到該錯誤是因為您的方法“ getResultInMapList ”正在回傳 Map 串列(List<Map<String,Object>>)并且您試圖將這些結果放入單個地圖 Map<String, Object> 而不是地圖串列。
我正在查看您問題末尾的影像,而不是:
Map<String, Object>resultSet=getResultInMapList(String url, String driver, String usr, String pwd,sqlQuery, "45");
你需要寫:
List<Map<String, Object>>resultSet=getResultInMapList(String url, String driver, String usr, String pwd,sqlQuery, "45");
這將解決您的錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/512227.html
