2014-03-31 117 views
0

以下代碼讀取xls文件並將其插入到我的測試端。它還檢查數據庫是否存在xls文件中的值,並與頁面標題進行比較。com.microsoft.sqlserver.jdbc.SQLServerException:將nvarchar值「123.0」轉換爲數據類型int時轉換失敗

測試失敗並我給以下錯誤

com.microsoft.sqlserver.jdbc.SQLServerException:轉換nvarchar的值「123.0」爲數據類型int時轉換失敗。

我的數據庫中forceID爲int類型,並在此代碼是字符串,但我轉換下面一行

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

請幫助!我使用SQL Server 2008 R2,硒的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"); 
    } 
} 
+0

是不是你試圖插入一個字符串到保存整數字段的問題?這個:'pstmt.setString(1,forceID);'可能應該使用'setInt(1,Integer.parseInt(ForceID))'或者類似的東西。 – jpw

+0

只是澄清你拋出的異常在哪裏?我認爲它是在rs = pstmt.executeQuery();但現在我意識到你並不具體。 –

+0

謝謝jpw和David Waters –

回答

1

您比較字符串(你的Java方法的forceId參數)數據庫(在數據庫中ForceID列)的整數。

您需要將您的java代碼中的forceID從一個字符串轉換爲一個整數,並將其作爲整數傳遞給您的準備狀態。

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

感謝David Waters。 –

相關問題