src中包括com.mmy.cstm包中含有子包:
1.actions(控制器)
1>AddServlet
2>DelServlet
3>EditServlet
4>SelectAllServlet
5>SelectServlet
2.beans(實體類)
1>Customer
3.dao(數據庫操作)
1>CustomerDao
4.sevice(業務)
1>CustomerService
5.utils(工具類)
1>JdbcUtil
屬性文件:mysql_jdbc.properties
addServlet
package com.mmy.cstm.actions;
import com.mmy.cstm.beans.Customer;
import com.mmy.cstm.service.CustomerService;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.UUID;
public class AddServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//處理請求參數的亂碼
request.setCharacterEncoding("utf-8");
Customer customer = new Customer();
try {
BeanUtils.populate(customer,request.getParameterMap());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
String cid = UUID.randomUUID().toString().replace("-","").substring(0,10);
customer.setCid(cid);
CustomerService customerService = new CustomerService();
customerService.addCustomer(customer);
response.sendRedirect(request.getContextPath()+"/selectAll");
}
}
DelServlet
package com.mmy.cstm.actions;
import com.mmy.cstm.beans.Customer;
import com.mmy.cstm.service.CustomerService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class DelServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String cid = request.getParameter( "cid");
CustomerService customerService = new CustomerService();
customerService.delCustomer(cid);
response.sendRedirect(request.getContextPath()+"/selectAll");
}
}
EditServlet
package com.mmy.cstm.actions;
import com.mmy.cstm.beans.Customer;
import com.mmy.cstm.service.CustomerService;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
public class EditServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
Customer customer = new Customer();
try {
BeanUtils.populate(customer,request.getParameterMap());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
CustomerService customerService = new CustomerService();
customerService.editCustomer(customer);
response.sendRedirect(request.getContextPath()+"/selectAll");
}
}
SelectAllServlet
package com.mmy.cstm.actions;
import com.mmy.cstm.beans.Customer;
import com.mmy.cstm.service.CustomerService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
public class SelectAllServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
CustomerService customerService = new CustomerService();
List<Customer> customerList = null;
customerList = customerService.selectAll();
request.setAttribute("customerList",customerList);
request.getRequestDispatcher("/list.jsp").forward(request,response);
}
}
SelectServlet
package com.mmy.cstm.actions;
import com.mmy.cstm.beans.Customer;
import com.mmy.cstm.service.CustomerService;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
public class SelectServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String cid = request.getParameter("cid");
CustomerService customerService = new CustomerService();
Customer customer = customerService.select(cid);
request.setAttribute("customer",customer);
request.getRequestDispatcher("/edit.jsp").forward(request,response);
}
}
Customer
package com.mmy.cstm.beans;
public class Customer {
private String cid;
private String cname;
private String gender;
private String birthday;
private String cellphone;
private String email;
private String description ;
public Customer() {}
public Customer(String cname, String gender, String birthday, String cellphone, String email, String description) {
this.cname = cname;
this.gender = gender;
this.birthday = birthday;
this.cellphone = cellphone;
this.email = email;
this.description = description;
}
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getCellphone() {
return cellphone;
}
public void setCellphone(String cellphone) {
this.cellphone = cellphone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
CustomerDao
package com.mmy.cstm.dao;
import com.mmy.cstm.beans.Customer;
import com.mmy.cstm.utils.JdbcUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class CustomerDao {
private QueryRunner queryRunner = new QueryRunner(JdbcUtil.getComboPooledDataSource());
public void add(Customer customer){
String sql = "insert into customer values(?,?,?,?,?,?,?)";
Object[] param = {customer.getCid(),customer.getCname(),
customer.getGender(),customer.getBirthday(),
customer.getCellphone(),customer.getEmail(),
customer.getDescription()};
try {
queryRunner.update(sql,param);
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<Customer> selectAll() {
String sql = "select * from customer";
List<Customer> customerList = null;
try {
customerList = queryRunner.query(sql, new BeanListHandler<Customer>(Customer.class));
} catch (SQLException e) {
e.printStackTrace();
}
return customerList;
}
public Customer select(String cid) {
String sql = "select * from customer where cid = ?";
try {
Customer customer = queryRunner.query(sql,new BeanHandler<Customer>(Customer.class),cid);
return customer;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public void delCustomer(String cid) {
String sql = "delete from customer where cid = ?";
try {
queryRunner.update(sql,cid);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void editCustomer(Customer customer) {
String sql = "update customer set cname = ?,gender = ?,birthday = ?,cellphone = ?,email =?," +
"description = ? where cid = ?";
try {
queryRunner.update(sql,customer.getCname(),customer.getGender(),customer.getBirthday(),
customer.getCellphone(),customer.getEmail(),customer.getDescription(),
customer.getCid());
} catch (SQLException e) {
e.printStackTrace();
}
}
}
CustomerService
package com.mmy.cstm.service;
import com.mmy.cstm.beans.Customer;
import com.mmy.cstm.dao.CustomerDao;
import java.util.List;
public class CustomerService {
private CustomerDao customerDao = new CustomerDao();
//>添加客戶
public void addCustomer(Customer customer) {
customerDao.add(customer);
}
public List<Customer> selectAll() {
List<Customer> customerList = customerDao.selectAll();
return customerList;
}
public Customer select(String cid) {
return customerDao.select(cid);
}
public void delCustomer(String cid) {
customerDao.delCustomer(cid);
}
public void editCustomer(Customer customer) {
customerDao.editCustomer(customer);
}
}
JdbcUtil
package com.mmy.cstm.utils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JdbcUtil {
private static ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
static {
Properties properties = new Properties();
InputStream inputStream = JdbcUtil.class.getResourceAsStream("/mysql_jdbc.properties");
try {
properties.load(inputStream);
String driverClass = properties.getProperty("driverClass");
String url = properties.getProperty("url");
String username = properties.getProperty("username");
String password = properties.getProperty("password");
comboPooledDataSource.setDriverClass(driverClass);
comboPooledDataSource.setJdbcUrl(url);
comboPooledDataSource.setUser(username);
comboPooledDataSource.setPassword(password);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
try {
return comboPooledDataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static ComboPooledDataSource getComboPooledDataSource(){
return comboPooledDataSource;
}
public static void close(Connection connection, Statement statement, ResultSet resultSet){
try {
if (connection != null)
connection.close();
if (statement != null)
statement.close();
if (resultSet != null)
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}