return语句在最后阻止好坏主意

时间:2015-01-24 08:33:58

标签: java sql jsp jsonobject

我在这里写了一段代码并想知道,将结果放在finally语句中是个好主意吗? 我已将return语句放在finally块中,因为如果发生异常,那么它也会发送消息。那么,这种正确的方法是将异常消息发送到jsp页面还是以其他方式来实现它。

public JSONObject SetKeyFunctionDAO(JSONObject obj) throws SQLException 
    {
        DatabaseConnection dc = new DatabaseConnection();
        Connection con = dc.openConnection();
        JSONObject res = null;
        int n ;

        String sql = " update data_table set mkey = ? where unique_user = ? and website_name = ? ;";
        try 
        {
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, obj.getString("key"));
            ps.setString(2, obj.getString("uniqueID"));
            ps.setString(3, obj.getString("account"));

            n = ps.executeUpdate();
            res = new JSONObject();
            if(n != 0)
            {
                res.put("status", "success");
            }
            else
            {
                res.put("status", "failed");
            }
        }
        catch (SQLException ex)
        {
           res = new JSONObject();
           res.put("status", "failed");
           res.put("exception", ex);
        } 
        catch (JSONException ex) 
        {
            res = new JSONObject();
            res.put("status", "failed");
            res.put("exception", ex);
        }
        finally
        {
            dc.closeConnection();
            con.close();
            return res;
        }
    }

先谢谢!!

0 个答案:

没有答案