Meaningless Drivel is fun!*
The moose likes Struts and the fly likes missing return statement { 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 » Frameworks » Struts
Bookmark "missing return statement {" Watch "missing return statement {" New topic
Author

missing return statement {

Prashant K. Singh
Greenhorn

Joined: May 11, 2009
Posts: 18
Hi I have written an action class for struts 2.0 but I am getting a compiler errror which says:

missing return statement
[javac] }
[javac] ^
[javac] 1 error

Actually the thing is that I cant change how the class is written.
I am posting the code. Any suggestions are welcome:


package net.astralpharma;
import com.opensymphony.xwork2.ActionSupport;
import java.sql.*;
import java.util.Date;

public class Login extends ActionSupport {

public String execute()throws Exception
{

String x = getUsername();

String url = "jdbc:mysql://localhost:3306/";
String dbname="astraldb";
String driverName="org.gjt.mm.mysql.Driver";
String userName="root";
String password="root";
Connection con=null;
PreparedStatement stat =null;

try
{
Class.forName(driverName);
System.out.println("Driver Loaded");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/astraldb","root","root" );
System.out.println("Database is connected");
String query="Select password from logindet where username=?";
stat = con.prepareStatement(query);
System.out.println(stat);
stat.setString(1, x);
ResultSet rs = stat.executeQuery();

while(rs.next())
{
String pwd_db = rs.getString("password");
System.out.println("database_passwd" + pwd_db);
String pwd_fe = getPassword();
System.out.println("frontend_passwd" + pwd_fe);
if(pwd_db.equals(pwd_fe))
{
return "SUCCESS";
}
else
{

return "ERROR";
}
}

}
catch(SQLException sqle)
{
System.out.println("Exception : " + sqle);
sqle.printStackTrace();
}
catch(Exception e)
{
System.out.println("Exception : " + e);
e.printStackTrace();
}
}

private String username = null;

public String getUsername() {
return username;
}

public void setUsername(String value) {
username = value;
}


private String password = null;

public String getPassword() {
return password;
}

public void setPassword(String value) {
password = value;
}
}


Prashant Singh
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Prashant, please use code tags when you post a source code



I've corrected the return statement problem in your code. But the logic also looks wrong to me. If you are thinking that it will try to match the password with every password in the database, then I'm afraid that will not happen. What actually will happen is that it will match the password with only the first password in the database. If it matches, then it will return success otherwise it will return error. Although I'm sure that username would be unique in your database, but since you have used a while loop to check the password, so I am just warning you...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'd also add that this code is antithetical to current Java (and S2) coding practices.

By embedding all that logic in the action itself things are more difficult to test, debug, and understand. YMMV, but I'd sure consider breaking it out into a service-makes unit testing both the service and the action *much* easier.
Prashant K. Singh
Greenhorn

Joined: May 11, 2009
Posts: 18
thanks Ankit and thanks David.

You guys were bang on target
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: missing return statement {
 
Similar Threads
I need to set <s:property> value in the action class
how to display database values in a table
need to transfer productID from url to action class in struts 2
How to store values in session using struts 2 and then how to retrieve values in a jsp
Struts 2 -Eclipse