This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

missing return statement {

 
Prashant K. Singh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
}
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Ankit and thanks David.

You guys were bang on target
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic