我正在創建應用程序,我需要調用SQL過程。如何解決「從字符串轉換爲uniqueidentifier時轉換失敗」?
我得到錯誤的參數是最後一個,即第十五個參數,contactID ..前五個參數也是唯一標識符,但它們設置正確..只有最後一個,contactID在嘗試設置時拋出異常它作爲程序參數。
這裏是我的Java代碼:
@SuppressWarnings({ "unchecked", "null" })
@Override
public JSONObject saveProductData(ProductsDataToSaveDTO ProductsDataToSaveDTO) throws SQLException {
String output = "";
Connection conn = null;
Statement sta = null;
String errorClassAndMethod = getErrorContainingClassAndMethod();
JSONObject jsonOutput = new JSONObject();
try {
Gson gson = new GsonBuilder().create();
String json = gson.toJson(ProductsDataToSaveDTO);
org.json.JSONObject jsonInput = new org.json.JSONObject(json);
conn = createConnection(jsonInput);
sta = conn.createStatement();
String SPsql = "{call SP_InsertUpdateAdvisoryBoxProductDetails (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}";
PreparedStatement ps = conn.prepareStatement(SPsql);
System.out.println(jsonInput);
org.json.JSONArray productsList = (org.json.JSONArray) jsonInput.get("productsList");
ps.setString(1, ProductsDataToSaveDTO.getClientId());
ps.setString(2, ProductsDataToSaveDTO.getDivisionId());
ps.setString(3, ProductsDataToSaveDTO.getClientRoleId());
ps.setString(4, ProductsDataToSaveDTO.getUserID());
ps.setString(5, ProductsDataToSaveDTO.getUserLoginDetailId());
for (ProductsList productsListBO : ProductsDataToSaveDTO.getProductsList()) {
if (productsListBO.getProductCategory().equalsIgnoreCase("AOV")) {
ps.setString(6, productsListBO.getProductSituation());
String output2 = productsListBO.getProductSituation();
ps.setString(7, productsListBO.getProductCategory());
ps.setString(8, productsListBO.getProductSubcategory());
ps.setString(9, productsListBO.getProviderName());
ps.setString(10, productsListBO.getProductName());
ps.setString(11, productsListBO.getServiceProvider());
ps.setString(12, productsListBO.getPolicyNumber());
ps.setString(13,productsListBO.getProductClosed());
ps.setString(14, productsListBO.getNameFirstInsured());
ps.setString(15, productsListBO.getContactID());
}
ResultSet rs = ps.executeQuery();
while (rs.next()) {
output = rs.getString(1);
jsonOutput.put("status", output);
}
}
} catch (Exception e) {
output = e.toString();
e.printStackTrace();
String errorMessageAndClassWithMethod = getErrorContainingClassAndMethod();
throw new SpringAppRuntimeException(errorMessageAndClassWithMethod + e.toString());
} finally {
if (sta != null) {
conn.close();
}
if (conn != null) {
conn.close();
}
}
return jsonOutput;
}
,當我跑我的服務,我得到的錯誤作爲,發生 「應用程序運行時異常[中ClassName.saveProductData錯誤:com.microsoft.sqlserver .jdbc.SQLServerException:轉換從字符串轉換爲uniqueidentifier時失敗]」
我應該做的代碼什麼樣的改變才能成功運行它。?
對於哪個變量它給出的錯誤。 – Optional
嗨,它設置此參數時拋出異常: 「ps.setString(15,productsListBO.getContactID());」 –