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. Thank You So Much . .. .. .
Very Nice Expl

View Tutorial          By: salman at 2012-07-05 13:22:11

2. This is a cool tutorial ! :) But I think in this s
View Tutorial          By: ngocanh at 2013-07-11 02:33:06

3. i have to write a calculator program for very ve
View Tutorial          By: maryam at 2012-12-31 10:21:55

4. Hi,

I have tried with your script a

View Tutorial          By: Ivan at 2011-09-08 21:17:36

5. why have you change first name oak to java ?
View Tutorial          By: manohar patil at 2014-08-20 03:44:23

6. please help me to do find a push and pop operation
View Tutorial          By: sweet mae at 2011-01-14 00:01:33

7. I want more example of bufferedReader.
View Tutorial          By: Romulado Bacor at 2011-11-25 01:18:57

8. very nice content
View Tutorial          By: sarat at 2012-08-22 12:17:26

9. Can someone tell me how to run in linux 7.10...
View Tutorial          By: fatwa perdana at 2008-10-15 03:27:49

10. Thanks for the above description, but pls let me k
View Tutorial          By: Amit Goyal at 2009-09-25 05:33:10