package struts2demo;
import java.sql.*;
public class WelcomeUserAction {
// you can get it from
private String username;
private String password;
//get and set method for struts2 fill in
//Username is the label name
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;
}
//"all struts logic here"
public String execute() {
//
try {
Class.forName("com.mysql.jdbc.Driver");
//System.out.println("Success loading Mysql Driver!");
}
catch (Exception e) {
//System.out.println("Error loading Mysql Driver!");
e.printStackTrace();
}
//
try {
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/zrss", "root", "1234");
String sql = "select * from user where name = ?";
PreparedStatement ps = connect.prepareStatement(sql);
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
if (rs.getString("password").equals(password)) {
return "SUCCESS";
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return "ERROR";
}
}
C->forward to SUCCESS(welcome_user.jsp) or ERROR(error.jsp)