Comment on Tutorial - Struts 1 vs Struts 2 By jcarreira, adrian deccico



Comment Added by : Prajwal

Comment Added at : 2011-11-08 10:19:58

Comment on Tutorial : Struts 1 vs Struts 2 By jcarreira, adrian deccico
Can any one help me writing the code...
In struts2... i want to validate username and password through database..

import com.opensymphony.xwork2.ActionSupport;
import java.sql.*;


public class Retrieve extends ActionSupport
{

public String execute() throws Exception
{
String url = "jdbc:mysql://localhost:3306/";
String dbName = "login";
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String passWord = "12345";
Connection con=null;
try{
Class.forName(driverName).newInstance();
con=DriverManager.getConnection(url+dbName, userName, passWord);
System.out.println("Connected database successfully...");
}
catch(Exception e){
System.out.println(e.getMessage());
}
try{
String username=getUsername();
String password=getPassword();


String sql = "select * from user";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);

while(rs.next())
{
// System.out.print("Username :" + rs.getString(1)+ " ");
// System.out.println("Password :" + rs.getString(2));

if (this.username.equals("admin")&& this.password.equals("admin"))
{
System.out.print("Username :" + rs.getString(1)+ " ");
System.out.println("Password :" + rs.getString(2));
return "success";
}
else
{

return "error";
}

}
}
catch(SQLException s){
System.out.println(s);
}
return SUCCESS;
}

private String username= null;
private String password= null;

public String getUsername()
{
return username;
}

public void setUsername(String username)
{
this.username = username;
}

public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}


Here in above code i have hardcoded the username and password but i want database to validate the user credentials..

help me ASAP


View Tutorial



Subscribe to Tutorials

Related Tutorials

Program using concept of byte long short and int in java

Update contents of a file within a jar file

Tomcat and httpd configured in port 8080 and 80

Java File

Java String

Count number of vowels, consonants and digits in a String in Java

Reverse a number in Java

Student marks calculation program in Java

Handling Fractions in Java

Calculate gross salary in Java

Calculate average sale of the week in Java

Vector in Java - Sample Program

MultiLevel Inheritance sample in Java

Multiple Inheritance sample in Java

Java program using Method Overriding

Archived Comments

1. thanks for giving suitable example to understand m
View Tutorial          By: akash dange at 2011-12-21 05:40:36

2. Excellent article. Please make the email id of the
View Tutorial          By: Rajita at 2008-03-25 09:31:27

3. Nice example to understand what does the string To
View Tutorial          By: Spha at 2011-07-14 08:00:03

4. hello can u send me the code in java to send and r
View Tutorial          By: somanath at 2011-02-17 01:28:40

5. How to delete file button
View Tutorial          By: Chinzoo at 2013-04-13 13:30:18

6. I set my eclipse as describe above.
I also

View Tutorial          By: Anonymous at 2012-03-25 02:12:52

7. This is an excerpt from Java Complete Reference.Ju
View Tutorial          By: Guest at 2010-03-25 01:36:57

8. we are getting an error saying that " the sta
View Tutorial          By: chaitra at 2014-03-20 10:53:02

9. good one.......thanks
View Tutorial          By: raghubir at 2012-11-10 19:01:45

10. Great job!
Lot of things cleared...

View Tutorial          By: alok at 2009-03-18 10:45:25