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. hello!
I am so confused and a total beginne

View Tutorial          By: Jena at 2011-06-11 17:39:39

2. plzzz xplain all d buzzwads.....
View Tutorial          By: sindhu at 2010-12-10 08:49:27

3. good
View Tutorial          By: saravanan at 2007-03-02 23:16:00

4. Its a peshak site
View Tutorial          By: SANIJ at 2010-01-06 22:40:31

5. I used two phrases of the security section for the
View Tutorial          By: pabo at 2010-04-05 05:22:24

6. Really a great article!
I have some questio

View Tutorial          By: ron at 2010-03-30 01:37:12

7. This is a good written article!
It gave me

View Tutorial          By: Anders Branderud at 2010-01-10 03:24:25

8. sir, i need java code to display position and velo
View Tutorial          By: Sandhya Yadav at 2015-08-20 07:16:04

9. java program to maintain the books details like Bo
View Tutorial          By: Prabha at 2013-08-28 12:34:00

10. Thanks for your nice article.I learned a lot
View Tutorial          By: Mahyar at 2012-08-12 12:09:36