Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Can  anyone help me with this error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can  anyone help me with this error" Watch "Can  anyone help me with this error" New topic
Author

Can anyone help me with this error

Amritpal Singh Moomie
Ranch Hand

Joined: Sep 24, 2010
Posts: 32
Hi guys i am getting this error with my code

SimpleLogin.java:70: missing return statement
}
^
1 error
Press any key to continue . . .


Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

What gets returned if an SQLException occurs?


Steve
Amritpal Singh Moomie
Ranch Hand

Joined: Sep 24, 2010
Posts: 32
I dont know , i have not run it before
But i dont think its about sql exception
I think some syntax error
i cant figure it out
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

The code has 2 syntax errors. A mis-placed open-curly-bracket ('{') and a missing close parenthesis (')'). I will let you find them.

But the error you get suggests there is valid route through your method in which there is no return statement by the end of the executed code. So again, what happens when a SQLException occurs?

<Edit>
I mean logically... Trace the route through your code and predict what gets returned.
Amritpal Singh Moomie
Ranch Hand

Joined: Sep 24, 2010
Posts: 32
Steve i have email you the full code .
Sorry i am really new to java.
what i am basically trying to do is call CheckValidUser for a jsp page to check login from database
rahul sdfsdf
Greenhorn

Joined: Sep 27, 2010
Posts: 3
after ending the Catch block you have to add return statement and than it will work fine.
Kr Manish
Ranch Hand

Joined: Jul 30, 2010
Posts: 138
Since the function returns a String, compiler has to make sure that it actually does in ALL cases. You have 3 cases. You return "success" in one, "fail" in another. But if sqlexception occurs, you don't return string. Just return "fail" from catch block as well, apart from correcting the syntax errors. Should work fine.


You know what I am saying ?
Amritpal Singh Moomie
Ranch Hand

Joined: Sep 24, 2010
Posts: 32
But issue is that i want only "Success" or "fail" output
Amritpal Singh Moomie
Ranch Hand

Joined: Sep 24, 2010
Posts: 32
Thanks Manish
Let me try
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Amritpal Singh Moomie wrote:Steve i have email you the full code .
Sorry i am really new to java.
what i am basically trying to do is call CheckValidUser for a jsp page to check login from database


Let's keep this on the forum. There is no need to use full code, or send the email.

1) Fix the syntax errors above
2) Walk through your code, and figure out what path(s) may not return a value
3) Determine what value you do want to return in those cases and modify the code accordingly
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

the error says you are missing any return statement anywhere in your code.and i thinnk you got it where you are getting it.


SCJP6.0,My blog Ranchers from Delhi
Amritpal Singh Moomie
Ranch Hand

Joined: Sep 24, 2010
Posts: 32
Thanks for the help guys
i really appreciate

I have made this change to my code acording to Manish
But it is giving me only fail output even if login infomation is right.

Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

I think there may be some mismatch of the words because you are using equals for comparing the two objects..

use equalsIgnoreCase for username
and equals for password..
it will not cause unnessary mistakes in your code..

Like password should only be case sensitive not username
Amritpal Singh Moomie
Ranch Hand

Joined: Sep 24, 2010
Posts: 32
Thanks Shanky that was helpful

But i am still stuck on that thing that it is still giving "fail" as default value
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Amritpal Singh Moomie wrote:Thanks Shanky that was helpful

But i am still stuck on that thing that it is still giving "fail" as default value


You have a logic error in your IF(...) conditions. Presumably you have more than one user. What happens if the user who is trying to login is not the first user returned from the database?

Here is what I suggest:
Take a pen and paper and write out exactly the steps that occur. When you know why the logic is bad, see if you can fix it. One of the things you need to consider is: What are the exact conditions that create a failed login? I can think of 2 (plus an error condition). Make sure your method captures all conditions. Write now it only captures one, and only compares on the first user.
Amritpal Singh Moomie
Ranch Hand

Joined: Sep 24, 2010
Posts: 32
Thanks Steve
Can you please tell me is that my code line 34 i.e
return"fail";
setting the value to fail
Even if we have only one record in the database and I enter the same username and password.
It will return fail ?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

No, only the first return statement will execute. That one at the end would not override a 'success' if return "success"; line was reached. That will return "fail" only if the method completes execution without reaching a different return statement. That can happen
- If there is an exception in the try {} block that gets caught in one of the catch {} blocks.
- If there are no results in the ResultSet (if rs.next() returns false the first time it is called)

Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Why not you check your code by debugging..

because what i think is your query is not executing properly and thats why line 30 is executing always..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can anyone help me with this error
 
Similar Threads
Java won't send SQL query
How to retrieve the data from the database and display it on a jsp using struts
Result set
SQL Search
Help with SQL syntax.