如何解决“从字符串转换为uniqueidentifier时转换失败”?

时间:2017-09-27 05:18:48

标签: java sql-server stored-procedures logic

我正在创建需要调用SQL过程的应用程序。

我得到错误的参数是last,即第15个参数,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时转换失败。]”​​

我应该在代码中进行哪些更改才能成功运行它?

1 个答案:

答案 0 :(得分:1)

因此,根据您的评论,我将其添加为答案

1)ps.setString(15, productsListBO.getContactID());"

2)检查您的程序SP_InsertUpdateAdvisoryBoxProductDetails的第15个参数是@ProductSituation NVARCHAR(10)

3)这真的是一个标识符字段,varchar是否与contactID的长度相匹配?改变10说100并测试。

4)我还认为你在参数中设置值的顺序(基于方法名称)和程序参数的顺序是不同的。仔细检查并纠正它