从java调用过程时,“语句没有返回结果集”

时间:2017-09-14 09:09:00

标签: java sql-server stored-procedures logic procedure

目前我正在从SQL服务器调用一个过程。

这是我的方法:

@Override
    public String savePersonalData(PersonalDataDto personalDataDto) throws SQLException {
        String output = "";
        Connection conn = null;
        Statement sta = null;
        String errorClassAndMethod = getErrorContainingClassAndMethod();
        try {
            Gson gson = new GsonBuilder().create();
            String json = gson.toJson(personalDataDto);
            org.json.JSONObject jsonInput = new org.json.JSONObject(json);

            conn = createConnection(jsonInput);

            sta = conn.createStatement();
            String SPsql = "EXEC SP_UpdateAdvisoryBoxPersonalData ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?";

            PreparedStatement ps = conn.prepareStatement(SPsql);
            // ps.setEscapeProcessing(true);

            System.out.println(personalDataDto);

            ps.setString(1, personalDataDto.getClientId());
            ps.setString(2, personalDataDto.getDivisionId());
            ps.setString(3, personalDataDto.getClientRoleId());
            ps.setString(4, personalDataDto.getUserID());
            ps.setString(5, personalDataDto.getUserLoginDetailId());

            ps.setString(6, personalDataDto.getPersonalData().getAnv_Om_PlaatsGeboorte());
            ps.setString(7, personalDataDto.getPersonalData().getNaw_Om_Postcode());
            ps.setString(8, personalDataDto.getPersonalData().getNaw_Om_Voorletters());
            ps.setString(9, personalDataDto.getPersonalData().getNaw_Om_Naam());
            ps.setDate(10, personalDataDto.getPersonalData().getAnv_Dt_Geboorte());
            ps.setString(11, personalDataDto.getPersonalData().getAnv_Om_Email());
            ps.setString(12, personalDataDto.getPersonalData().getAnv_Om_TelefoonPrive());
            ps.setString(13, personalDataDto.getPersonalData().getAnv_Om_TelefoonWerk());
            ps.setString(14, personalDataDto.getPersonalData().getNaw_Om_Voornamen());
            ps.setString(15, personalDataDto.getPersonalData().getNaw_om_iban());
            ps.setString(16, personalDataDto.getPersonalData().getAnv_Om_LegitimatieAfgifte());
            ps.setInt(17, personalDataDto.getPersonalData().getNaw_Id());
            ps.setString(18, personalDataDto.getPersonalData().getNaw_Nr_Huis());
//          ps.setInt(19, personalDataDto.getPersonalData().getNaw_Id());
            ps.setString(19, personalDataDto.getPersonalData().getNaw_Om_Straat());

            ps.setString(20, personalDataDto.getPersonalData().getAnv_Om_TelefoonMobiel());
            ps.setString(21, personalDataDto.getPersonalData().getAnv_Cd_Nationaliteit());



            ResultSet rs = ps.executeQuery();

            /*boolean gotResults = ps.execute();
            ResultSet rs = null;
            if(!gotResults){
               System.out.println("No results returned");
            } else {
               rs = ps.getResultSet();
               System.out.println(rs.getMetaData());
            }*/

        } catch (Exception e) {
            e.printStackTrace();
            String errorMessageAndClassWithMethod = getErrorContainingClassAndMethod();
            throw new SpringAppRuntimeException(errorMessageAndClassWithMethod + e.toString());
        } finally {
            if (sta != null) {
                conn.close();
            }
            if (conn != null) {
                conn.close();
            }
        }
        return output;
    }

我正在使用两种不同的DTO。 PersonalDataDto就像:

public class PersonalDataDto {

    private String nawId;
    private String serverName;
    private String userName;
    private String password;
    private String databaseName;

    private String clientId;
    private String divisionId;
    private String clientRoleId;
    private String userID;
    private String userLoginDetailId;

    private PersonalDataInfoDto personalData;

// getters and setters for all

}

下一个是PersonalDataInfoDto,它就像:

public class PersonalDataInfoDto {

    private String anv_Om_TelefoonMobiel;

    private int naw_Id;

    private String anv_Om_TelefoonPrive;

    private String anv_Om_LegitimatieAfgifte;

    private int advisoryBoxContactRefID;

    private String naw_Om_Straat;

    private String naw_Om_Postcode;

    private String anv_Om_Email;

    private String naw_Nr_Huis;

    private String naw_Om_Voorletters;

    private String anv_Om_TelefoonWerk;

    private String naw_Om_Voornamen;

    private Date anv_Dt_Geboorte;

    private String naw_om_iban;

    private String anv_Om_PlaatsGeboorte;

    private String naw_Om_Naam;

    private String anv_Cd_Nationaliteit;

// getters and setters for all

}

错误是:

com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set.

我应该在代码/输入中进行哪些更改以从过程中获取结果集? 我是否需要在通过java调用它时在程序中添加一些内容,就像我在方法中所做的那样?

1 个答案:

答案 0 :(得分:1)

注释掉PRINT语句,当您在数据库客户端中执行查询时,将返回一个表。

在存储过程中,有insert和update语句。如果存储过程先前执行任何插入或更新 到最后的选择,JDBC(SQL Server驱动程序)与行计数混淆(语句没有返回结果的异常) 将被抛出。)

因此,您只需在存储过程开始时添加SET NOCOUNT ON。