1.entity层
package com.sunglass.entity;
public class Customer {
private int ctID;
private String ctName;
private String ctPassword;
private String ctEmail;
public String getCtEmail() {
return ctEmail;
}
public void setCtEmail(String ctEmail) {
this.ctEmail = ctEmail;
}
public int getCtID() {
return ctID;
}
public void setCtID(int ctID) {
this.ctID = ctID;
}
public String getCtName() {
return ctName;
}
public void setCtName(String ctName) {
this.ctName = ctName;
}
public String getCtPassword() {
return ctPassword;
}
public void setCtPassword(String ctPassword) {
this.ctPassword = ctPassword;
}
}
2.DAO层
package com.sunglass.DAO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.sunglass.DbUtil.DbUtil;
import com.sunglass.entity.Customer;
public class CustomerDAOImpl implements CustomerDAO {
@Override
public boolean add(Customer customer) {
// TODO Auto-generated method stub
return DbUtil.executeUpdate("insert into customer values(?,?,?,?)",new Object[]{customer.getCtID(),customer.getCtName(),customer.getCtPassword(),customer.getCtEmail()});
}
@Override
public boolean delete(Customer customer) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean update(Customer customer, Customer newcustomer) {
// TODO Auto-generated method stub
return false;
}
@Override
public List<Customer> getAll() {
// TODO Auto-generated method stub
ResultSet rs= DbUtil.executeQuery("select * from customer",new Object[]{});
List<Customer>customerList=new ArrayList<Customer>();
try{
while(rs.next()){
Customer customer=new Customer();
customer.setCtID(rs.getInt(1));
customer.setCtName(rs.getString(2));
customer.setCtPassword(rs.getString(3));
customerList.add(customer);
}
}catch(SQLException e){
e.printStackTrace();
}
return customerList;
}
@Override
public Customer getByEmail(String email) {
// TODO Auto-generated method stub
ResultSet rs= DbUtil.executeQuery("select * from customer where ctEmail=?",new Object[]{email});
Customer customer=null;
try{
while(rs.next()){
customer =new Customer();
customer.setCtID(rs.getInt(1));
customer.setCtName(rs.getString(2));
customer.setCtPassword(rs.getString(3));
customer.setCtEmail(rs.getString(4));
}
}catch(SQLException e){
e.printStackTrace();
}
return customer;
}
@Override
public ResultSet login(Customer customer) {
// TODO Auto-generated method stub
ResultSet rs= DbUtil.executeQuery("select * from customer where ctID=? and ctPassword=?",new Object[]{customer.getCtName(),customer.getCtPassword()});
return rs;
}
}
3.Service层
package com.sunglass.service;
import java.sql.ResultSet;
import java.util.List;
import com.sunglass.DAO.CustomerDAO;
import com.sunglass.entity.Customer;
public class CustomerService {
private CustomerDAO customerDAO;
public boolean add(Customer customer){
return customerDAO.add(customer);
}
public boolean update(Customer customer,Customer newcustomer){
return customerDAO.update(customer,newcustomer);
}
public boolean delete(Customer customer){
return customerDAO.delete(customer);
}
public List<Customer> getALL(){
return customerDAO.getAll();
}
public ResultSet login(Customer customer) {
return customerDAO.login(customer);
}
public Customer getByEmail(String email){
return customerDAO.getByEmail(email);
}
public CustomerDAO getCustomerDAO() {
return customerDAO;
}
public void setCustomerDAO(CustomerDAO customerDAO) {
this.customerDAO = customerDAO;
}
}
4.DbUtil 数据库连接池
package com.sunglass.DbUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DbUtil {
private static ComboPooledDataSource dataSource=new ComboPooledDataSource();
// private static DataSource dataSource=null;
static {
try {
// Context initContext = new InitialContext();
// Context envContext = (Context)
// initContext.lookup("java:/comp/env");
// dataSource = (DataSource) envContext.lookup("jdbc/demo");
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/sunglass");
dataSource.setUser("root");
dataSource.setPassword("19950824");
dataSource.setInitialPoolSize(50);
dataSource.setMaxPoolSize(100);
dataSource.setMaxIdleTime(10000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection(){
Connection conn=null;
try {
conn= DbUtil.dataSource.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static boolean executeUpdate(String sql,Object[] args){
boolean sign=false;
Connection conn=null;
PreparedStatement pst=null;
try{
conn=dataSource.getConnection();
pst=conn.prepareStatement(sql);
if(args!=null&&args.length>0){
for(int i=0;i<args.length;i++){
pst.setObject(i+1, args[i]);
}
}
int rows=pst.executeUpdate();
sign=rows>0?true:false;
}catch(Exception e){
e.printStackTrace();
}
return sign;
}
public static ResultSet executeQuery(String sql,Object[] args){
Connection conn=null;
PreparedStatement pst=null;
ResultSet rs=null;
try{
conn=DbUtil.dataSource.getConnection();
pst=conn.prepareStatement(sql);
if(args!=null&&args.length>0){
for(int i=0;i<args.length;i++){
pst.setObject(i+1, args[i]);
}
}
rs=pst.executeQuery();
}catch(Exception e){
e.printStackTrace();
}
return rs;
}
public static void closeAll(ResultSet rs,Statement st,Connection conn){
try{
if(rs!=null) rs.close();
if(st!=null) st.close();
if(conn!=null) conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
5.Action类
package com.sunglass.action;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sunglass.DAO.CustomerDAOImpl;
import com.sunglass.entity.Customer;
import com.sunglass.service.CustomerService;
public class LoginAction extends ActionSupport implements ServletResponseAware{
/**
*
*/
private static final long serialVersionUID = 1L;
private HttpServletResponse response;
@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response=response;
}
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() throws SQLException{
Customer customer=new Customer();
CustomerDAOImpl customerDAOImpl=new CustomerDAOImpl();
CustomerService customerService=new CustomerService();
customerService.setCustomerDAO(customerDAOImpl);
customer.setCtName(getUsername());
customer.setCtPassword(getPassword());
ResultSet rs=customerService.login(customer);
try{if(rs.next()){
/*Cookie usercookie=new Cookie("user",getUsername());
usercookie.setMaxAge(60*60*24*3);
response.addCookie(usercookie);
Cookie pswdcookie=new Cookie("password",getPassword());
pswdcookie.setMaxAge(60*60*24*3);
response.addCookie(pswdcookie);
ActionContext ctx=ActionContext.getContext();
ctx.getSession().put("username", getUsername());
Integer count=(Integer)ctx.getApplication().get("count");
if(count==null){
count=0;
}
count++;
ctx.getApplication().put("count", count);*/
return SUCCESS;
}else{
return ERROR;
}}
catch(SQLException e){
e.printStackTrace();
}
return ERROR;
}
}
6.Struts配置
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- Struts2的所有Action都需位于package下 -->
<package name="default" namespace="/" extends="struts-default">
<action name="login_login" class="com.sunglass.action.LoginAction" >
<result>/WEB-INF/content/login-success.jsp</result>
<result name="error">/WEB-INF/content/404.jsp</result>
</action>
<action name="register_register" class="com.sunglass.action.RegisterAction" >
<result>/WEB-INF/content/register-success.jsp</result>
<result name="error">/WEB-INF/content/404.jsp</result>
</action>
<action name="login_out" class="com.sunglass.action.LoginOutAction">
<result>/WEB-INF/content/login-out-success.jsp</result>
</action>
<action name="*" >
<result>/WEB-INF/content/{1}.jsp</result>
</action>
</package>
</struts>
这个代码出现了BUG,无论数据库里面有什么数据,一概都会返回error