Web服务方法 - 不从try catch块中的if else语句返回数据

时间:2012-11-08 11:52:24

标签: java web-services

我有一个比较模板的Web服务方法,但是它不会在try catch块中找到的if else语句中执行代码,而是返回最后一个返回语句,其中显示“error”。知道自己做错了什么吗?它应该返回“手指被验证”或“手指未经验证”。

  @WebMethod(operationName = "verify")
public String verify(@WebParam(name = "name") String name, @WebParam(name = "ftset") String ftset) {
    Connection con = null;
    String dbTemplate = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/biodb", "root", "1234");
        PreparedStatement st;
        st = con.prepareStatement("select template from info where name = ? ");
        st.setString(1, name);

        ResultSet result = st.executeQuery();

        if (result.next()) { //.next() returns true if there is a next row returned by the query.

            dbTemplate = result.getString("template");

    byte[] byteArray = new byte[1];
    byteArray = hexStringToByteArray(dbTemplate);
    DPFPTemplate template = DPFPGlobal.getTemplateFactory().createTemplate();
    template.deserialize(byteArray);


    byte[] fsArray = new byte[1];
    fsArray = hexStringToByteArray(ftset);
    DPFPFeatureSet features = null;
    features.deserialize(fsArray);

    DPFPVerification matcher = DPFPGlobal.getVerificationFactory().createVerification();
    DPFPVerificationResult fresult = matcher.verify(features, template);

    if (fresult.isVerified()) {

        return "The fingerprint was VERIFIED.";

    } else {
        return "The fingerprint was NOT VERIFIED.";

    }

        }
    } catch (Exception e) {
        System.out.println(e.getMessage());

    } finally {
        if (con != null) {
            try {
                con.close();

            } catch (SQLException e) {
            }
        }
    }

      return "error";
}

1 个答案:

答案 0 :(得分:0)

  

知道自己做错了什么吗?

嗯,你所描述的行为是如果抛出异常会发生什么,由于这个原因:

catch (Exception e) {
    System.out.println(e.getMessage());
} 

如果任何出错了,你会给System.out写一些东西(大概是你没看过,否则你已经看到了发生了什么)并继续回复“错误”

首先,我建议捕获特定的例外 - 并更改您记录异常的方式,以便在诊断中更明显。

另外,如果result.next()返回false,您会收到此行为。由于缺少一致的缩进,您在发布的代码中并不清楚这一点。你应该肯定修复缩进 - 可读性绝对至关重要。

接下来,如果result.next()返回false,请计算出您想要发生的事情。这是一个错误吗?它真的应该返回“未经验证”的情况吗?