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



Ask a Question

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. How to download a pdf file from the server and sav
View Tutorial          By: harsha at 2013-08-12 07:52:22

2. the spinner example is very nice...
View Tutorial          By: narendra at 2011-11-23 11:16:47

3. sach a lovely exmpl for bgnrs thanks
View Tutorial          By: aaditya at 2013-01-13 08:05:03

4. nice one to understand the basic steps of jdbc....
View Tutorial          By: trishna at 2011-06-10 00:04:21

5. good explanation
View Tutorial          By: Naresh Kumar at 2010-10-25 00:28:18

6. could u pls tell me whcih class is implemted by it
View Tutorial          By: AMIT at 2009-10-04 00:21:30

7. good work.
if there is a ValidatorForm the

View Tutorial          By: floriano at 2008-10-09 07:23:16

8. welll program runs but fails at the statement wher
View Tutorial          By: Aman Aggarwal at 2009-03-22 22:42:52

9. this tutorial is awesome.. solved my doubt in seco
View Tutorial          By: Natasha at 2015-07-29 09:03:42

10. @dodo : Overloading is only for parameter types an
View Tutorial          By: sundar at 2010-04-19 02:44:23