com.microsoft.sqlserver.jdbc.SQLServerException:将nvarchar值'123.0'转换为数据类型int时转换失败

时间:2014-03-31 11:40:18

标签: java sql sql-server jdbc

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

测试失败并发出以下错误

  

com.microsoft.sqlserver.jdbc.SQLServerException:将nvarchar值'123.0'转换为数据类型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");
  }
}

1 个答案:

答案 0 :(得分:2)

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

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

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