package com.jsict.action;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.jws.soap.SOAPBinding.Use;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.jsict.entity.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class UserAction extends ActionSupport implements ModelDriven<User> {
private User user=new User();
public String login() {
boolean is=false;
try {
is = isExist(user.getUsername(), user.getPassword());
} catch (Exception e) {
// TODO: handle exception
System.out.println("error");
}
if (is) {
return "login";
} else {
return "unlogin";
}
}
public String register() {
if (user.getSex().equals("1")) {
user.setSex("男");
} else {
user.setSex("女");
}
Connection connection = null;
Statement stm = null;
PreparedStatement pst = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8", "root", "root");
stm = connection.createStatement();
pst = connection.prepareStatement("INSERT INTO user (username,password,email,tel,address,age,sex) VALUES(?,?,?,?,?,?,?)");
pst.setString(1, user.getUsername());
pst.setString(2, user.getPassword());
pst.setString(3, user.getEmail());
pst.setString(4, user.getTel());
pst.setString(5, user.getAddress());
pst.setString(6, user.getAge());
pst.setString(7, user.getSex());
pst.execute();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stm.close();
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "register";
}
private boolean isExist(String name,String pwd) throws Exception{
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs = null;
//1.加载JDBC驱动
Class.forName("com.mysql.jdbc.Driver");
//2.与数据库建立连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8", "root", "root");
//3.创建Statement或PreparedStatement对象
statement = connection.prepareStatement("select * from user where username=? and password=?");
statement.setString(1, name);
statement.setString(2, pwd);
rs = statement.executeQuery();
user.setUsername(null);
user.setPassword(null);
System.out.println(rs);
if(rs.next()) {
user.setId(rs.getString(1));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
user.setEmail(rs.getString(4));
user.setTel(rs.getString(5));
user.setAddress(rs.getString(6));
user.setAge(rs.getString(7));
user.setSex(rs.getString(8));
System.out.println(user);
}
rs.close();
statement.close();
connection.close();
if(name.equals(user.getUsername()))
{return true;}
return false;
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}