简体   繁体   English

在这种情况下如何返回true或false?

[英]How to return true or false in this scenario?

Here is a my code 这是我的代码

   public boolean changePassword(String password, String id) {

    try {

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(DB_URL, DBuser, DBpassword);


        Statement st = conn.createStatement();
        String updateQuery = "update teacher_registration set password="+password+"where teacher_id="+id;
        ResultSet rs = st.executeQuery(updateQuery);

        if(rs!=null){
            return true;
        }else{
            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

I want to return true if rs is not null else return false. 如果rs不为null,我想返回true,否则返回false。

But i am getting an error at the closing bracket of this method. 但是我在此方法的右括号中遇到错误。 It is asking me to return a true or false again even if I did so in the try catch block earlier. 它要求我再次返回true或false,即使我之前在try catch块中也是如此。

This is my first time working with java. 这是我第一次使用Java。

Please HELP! 请帮忙!

Do something like this 做这样的事情

public boolean changePassword(String password, String id) {
  boolean result = false;

try {

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection(DB_URL, DBuser, DBpassword);


    Statement st = conn.createStatement();
    String updateQuery = "update teacher_registration set password="+password+"where teacher_id="+id;
    ResultSet rs = st.executeQuery(updateQuery);

    if(rs!=null){
        result = true;
    }else{
        result = false;
    }
} catch (Exception e) {
    e.printStackTrace();
}
 return result;
}

Your mistake is you return the value in the try block. 您的错误是您在try块中返回了该值。 If something fails, you won't return anything. 如果失败,您将不会返回任何东西。 Try moving the return statement outside the try block or add a return false; 尝试将return语句移到try块之外或添加return false; after the catch block. 在捕获块之后。

I think update query pieces don't combine together well. 我认为更新查询片段不能很好地结合在一起。

In this case 'where' become a part of your password (at the end). 在这种情况下, “ where”成为您密码的一部分(最后)。

Try this: 尝试这个:

String updateQuery = "update teacher_registration set password="+password+" where teacher_id="+id;

Plus you can move your condition into catch block or do even better as show below. 另外,您可以将条件移到catch块中,甚至可以做得更好,如下所示。

Full version of your code: 完整版本的代码:

public boolean changePassword(String password, String id) {

    try {

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(DB_URL, DBuser, DBpassword);


        Statement st = conn.createStatement();
        String updateQuery = "update teacher_registration set password="+password+" where teacher_id="+id;
        ResultSet rs = st.executeQuery(updateQuery);
        if(rs!=null){
            return true;
        }

    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

}

If there is an exception generated in your try block, your return statements will not be reached. 如果在try块中生成了异常,则不会到达return语句。 After catching the exception, you simply need to return a value. 捕获异常后,您只需要返回一个值。 I assume that you return true if the change is successful, so return false if there is an exception. 我假设如果更改成功,则返回true,因此,如果有异常,则返回false。

try{
    // Your code
} catch (Exception e) {
    e.printStackTrace();
}
return false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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