gpt4 book ai didi

java - com.microsoft.sqlserver.jdbc.SQLServerException : Conversion failed when converting the nvarchar value '123.0' to data type int

转载 作者:行者123 更新时间:2023-12-02 05:59:53 24 4
gpt4 key购买 nike

下面的代码读取 xls 文件并将其插入到我的测试端。它还检查数据库 xls 文件中的值是否存在,并与页面标题进行比较。

测试失败并给出以下错误

com.microsoft.sqlserver.jdbc.SQLServerException: Conversion failed when converting the nvarchar value '123.0' to data type int.

我的数据库中的forceID是int类型,在这段代码中是字符串,但我在下面的行中进行了转换

int q= (int)Double.parseDouble(forceID);

请帮忙!!!我正在使用 SQL Server 2008 R2、Selenium WebDriver 和 Java

public void loginTest(String forceID,String user,String password)
throws InterruptedException, IOException, SQLException{
// test runmode of current dataset
count++;
if(!runmodes[count].equalsIgnoreCase("Y")) {
skip = true;
throw new SkipException ("Runmode for test set data is set to no"+ count);
}
APP_LOGS.debug("Excuting Login Test");
APP_LOGS.debug(forceID +" -- "+ user +" -- "+ password +" -- ");

openBrowser();
driver.get(CONFIG.getProperty("testSiteName"));
connToDatabase();


// Check the db
try{
pstmt=conn.prepareCall("select * from Login where ForceID=? and Username=? and Password=?");
pstmt.setString(1,forceID);
pstmt.setString(2,user);
pstmt.setString(3,password);
rs=pstmt.executeQuery();
valueFound =rs.next();
}catch(Exception e){
e.printStackTrace();
}

//log in to the app
getObject("forceID").clear();
int q= (int)Double.parseDouble(forceID);
getObject("forceID").sendKeys(String.valueOf(q));
//getObject("forceID").sendKeys(forceID);
getObject("user").clear();
getObject("user").sendKeys(user);
getObject("password").clear();
getObject("password").sendKeys(password);
getObject("logOn").click();
// get the title
String actual_title=driver.getTitle();
System.out.println(actual_title);
System.out.println(valueFound);
if (valueFound) {
Assert.assertEquals(actual_title, "Dashboard");
} else {
Assert.assertEquals(actual_title, "Logon");
}
}

最佳答案

您正在将字符串(java 方法的forceId 参数)与数据库中的整数(数据库中的ForceID 列)进行比较。

您需要在Java代码中将forceID从字符串转换为整数,并将其作为整数传递给准备好的语句。

pstmt.setInt(1, (int)Double.parseDouble(fourceID));

关于java - com.microsoft.sqlserver.jdbc.SQLServerException : Conversion failed when converting the nvarchar value '123.0' to data type int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22760643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com