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

标签 java sql sql-server jdbc

下面的代码读取 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/

相关文章:

java - 如何删除 sqlite 中的行/行(java,不是 Android java)

c# - 比较客户端排序和 SQL Server 排序之间的特性

sql - 如何包装以下 CTE,以便我可以将其输出与 INSERT INTO 语句一起使用?

sql-server - SQL INNER JOIN vs LEFT JOIN with a WHERE

c# - 休眠 : saving and retrieving a file to/from database

java - 嘿,我有一个关于 Spring 中的依赖注入(inject)和 bean 实例化的问题

Java编程: Something like Watir-Webdriver running in background

java - Hibernate Annotation 中的多重模式匹配

SQL 服务器 : How to update table based on subquery in where clause?

sql-server - 将所有 VARCHAR 列更改为 NVARCHAR