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. hey dude
im using vb.net 2008 and sql serv

View Tutorial          By: Haseeb ur Rehman at 2010-11-12 02:08:48

2. I'm a beginner, and I was told it is a good practi
View Tutorial          By: Nelson at 2009-08-17 09:13:28

3. hi every body i want full java code to send messag
View Tutorial          By: Yous at 2010-12-26 06:45:30

4. could you send me a code for a music video mobile
View Tutorial          By: Christian at 2009-08-28 23:03:45

5. how to get only the year like 2012
View Tutorial          By: Iguster at 2012-01-27 22:20:32

6. Thanks alot! it works.I have also suffered alot du
View Tutorial          By: jiya at 2008-11-24 10:00:50

7. new Thread(this , "Demo thread") mean
View Tutorial          By: shalini at 2011-12-06 07:29:20

8. i can't understand the deadlock clearly
View Tutorial          By: kalpana at 2012-11-01 11:26:09

9. thanx nice explanation...
View Tutorial          By: vignesh at 2012-07-15 20:12:22

10. not bad
View Tutorial          By: JUST_PREET at 2013-03-06 11:37:15