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
- Data Science
- React Native
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)