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. Nice Article.
View Tutorial          By: Swathi Kumar at 2012-04-15 10:44:14

2. Hi Sir...
sir i am working on Speech to Tex

View Tutorial          By: Sadaquat at 2012-08-19 07:51:28

3.
View Tutorial          By: pmprash at 2009-12-22 01:15:33

4. Your explanation is good and okay but, it will be
View Tutorial          By: Emmanuel at 2009-05-28 02:45:11

5. Hi Shreya, in Linux, the COM ports are identified
View Tutorial          By: Ramlak at 2008-06-23 03:34:11

6. It's simply nice.
View Tutorial          By: ghani at 2011-07-01 07:18:35

7. Thank you for sharing your knowledge. =>
View Tutorial          By: Condel Batac at 2013-02-03 23:17:26

8. Thanks to the author, it was really a good tutoria
View Tutorial          By: Ashok Kumar at 2010-04-29 05:54:47

9. Hello
thanks for nice tutorial. I have one

View Tutorial          By: Chanchal Prajapat at 2015-02-09 06:10:42

10. Plz tell me the classes to import for this code an
View Tutorial          By: Nitin at 2009-11-10 02:54:50