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.
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.
This is my first time working with 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. If something fails, you won't return anything. Try moving the return statement outside the try block or add a 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).
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.
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. 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.
try{
// Your code
} catch (Exception e) {
e.printStackTrace();
}
return false;
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.