简体   繁体   中英

Web Service Method- not returning data from the if else statement found in try catch block

I have a web service method to compare templates, however it does not perform the code in the if else statement found in the try catch block instead it returns the last return statment which says "error". Any idea what am doing wrong? It was supposed to return "finger was verified" or "finger was NOT verified".

  @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";
}

Any idea what am doing wrong?

Well, the behaviour you've described is what will happen if an exception is thrown, due to this:

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

If anything goes wrong, you write something to System.out (which presumably you're not looking at, otherwise you'd have seen what's happened) and continue by returning "error".

To start with, I'd recommend catching specific exceptions - and change how you're logging the exception so that it's more obvious in your diagnostics.

Additionally , you'll get this behaviour if result.next() returns false . This isn't clear from the code you've posted, due to the lack of consistent indentation. You should definitely fix the indentation - readability is absolutely crucial.

Next, work out what you want to happen if result.next() returns false . Is that an error? Should it actually just return the "not verified" case?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM