实体类
package com.yccz.entity;
import java.util.Date;
import java.util.List;
/**
* 订单表
* @author Administrator
*
*/
public class Bill {
private int id;
private String billCode;
private String productName;
private String productDesc;
private String productUnit;
private double productCount;
private double totalPrice;
private int isPayment;
private int createdBy;
private Date creationDate;
private int modifyBy;
private Date modifyDate;
private int providerId;
private String providerName;
public String getProviderName() {
return providerName;
}
public void setProviderName(String providerName) {
this.providerName = providerName;
}
//供应商集合
private Provider provider;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBillCode() {
return billCode;
}
public void setBillCode(String billCode) {
this.billCode = billCode;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public String getProductUnit() {
return productUnit;
}
public void setProductUnit(String productUnit) {
this.productUnit = productUnit;
}
public double getProductCount() {
return productCount;
}
public void setProductCount(double productCount) {
this.productCount = productCount;
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
public int getIsPayment() {
return isPayment;
}
public void setIsPayment(int isPayment) {
this.isPayment = isPayment;
}
public int getCreatedBy() {
return createdBy;
}
public void setCreatedBy(int createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public int getModifyBy() {
return modifyBy;
}
public void setModifyBy(int modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
public int getProviderId() {
return providerId;
}
public void setProviderId(int providerId) {
this.providerId = providerId;
}
public Provider getProvider() {
return provider;
}
public void setProvider(Provider provider) {
this.provider = provider;
}
@Override
public String toString() {
return "Bill [id=" + id + ", billCode=" + billCode + ", productName="
+ productName + ", productDesc=" + productDesc
+ ", productUnit=" + productUnit + ", productCount="
+ productCount + ", totalPrice=" + totalPrice + ", isPayment="
+ isPayment + ", createdBy=" + createdBy + ", creationDate="
+ creationDate + ", modifyBy=" + modifyBy + ", modifyDate="
+ modifyDate + ", providerId=" + providerId + ", provider="
+ provider + "]";
}
}
package com.yccz.entity;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 用户表
* @author Administrator
*
*/
public class User {
private int id;
private String userCode;
private String userName;
private String userPassword;
private int gender;
private Date birthday;
private String phone;
private String address;
private int userRole;
private int createdBy;
private Date creationDate;
private int modifyBy;
private Date modifyDate;
private int age;
private String userRoleName;
//由出生日期获得年龄
public int getMyAge(Date birthDay) throws Exception {
Calendar cal = Calendar.getInstance();
if (cal.before(birthDay)) {
throw new IllegalArgumentException(
"The birthDay is before Now.It's unbelievable!");
}
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH);
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
cal.setTime(birthDay);
int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH);
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
int age = yearNow - yearBirth;
if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
if (dayOfMonthNow < dayOfMonthBirth) age--;
}else{
age--;
}
}
return age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUserRoleName() {
return userRoleName;
}
public void setUserRoleName(String userRoleName) {
this.userRoleName = userRoleName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
try {
age = getMyAge(birthday);
} catch (Exception e) {
e.printStackTrace();
}
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getUserRole() {
return userRole;
}
public void setUserRole(int userRole) {
this.userRole = userRole;
}
public int getCreatedBy() {
return createdBy;
}
public void setCreatedBy(int createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public int getModifyBy() {
return modifyBy;
}
public void setModifyBy(int modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
}
package com.yccz.entity;
import java.util.Date;
//供应商
public class Provider {
private int id;
private String proCode;
private String proName;
private String proDesc;
private String proContact;
private String proPhone;
private String proAddress;
private String proFax;
private int createdBy;
private Date creationDate;
private int modifyBy;
private Date modifyDate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getProCode() {
return proCode;
}
public void setProCode(String proCode) {
this.proCode = proCode;
}
public String getProName() {
return proName;
}
public void setProName(String proName) {
this.proName = proName;
}
public String getProDesc() {
return proDesc;
}
public void setProDesc(String proDesc) {
this.proDesc = proDesc;
}
public String getProContact() {
return proContact;
}
public void setProContact(String proContact) {
this.proContact = proContact;
}
public String getProPhone() {
return proPhone;
}
public void setProPhone(String proPhone) {
this.proPhone = proPhone;
}
public String getProAddress() {
return proAddress;
}
public void setProAddress(String proAddress) {
this.proAddress = proAddress;
}
public String getProFax() {
return proFax;
}
public void setProFax(String proFax) {
this.proFax = proFax;
}
public int getCreatedBy() {
return createdBy;
}
public void setCreatedBy(int createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public int getModifyBy() {
return modifyBy;
}
public void setModifyBy(int modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
}
**dao层**
package com.yccz.dao;
import java.util.List;
import com.yccz.entity.Bill;
public interface BillDao {
public List selectAllList(int start,int size);
public int selectAllCount();
public List<Bill> selectByNameList( String productName,String providerId,String isPayment,int start,int size);
public int selectByNameCount( String productName,String providerId,String isPayment);
public int addBill(Bill bill);
public Bill selectById(int id);
public int update(Bill bill);
public int delete(int id);
public String getProviderNameByProId(int proId);
}
package com.yccz.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.yccz.dao.BillDao;
import com.yccz.entity.Bill;
import com.yccz.entity.Provider;
import com.yccz.util.BaseDao;
public class BillDaoImpl extends BaseDao implements BillDao {
@Override
public List<Bill> selectAllList(int start, int size) {
List<Bill> list = new ArrayList<Bill>();
String sql="select b.billCode,b.productName,b.providerId,b.totalPrice,b.isPayment,b.creationDate,p.proName,b.id from smbms_bill b,smbms_provider p where b.providerId = p.id order by b.id limit ?,?";
ResultSet rs = executeQuery(sql, (start-1)*size,size);
try {
while(rs.next()){
Bill bill = new Bill();
bill.setBillCode(rs.getString(1));
bill.setProductName(rs.getString(2));
bill.setProviderId(rs.getInt(3));
bill.setTotalPrice(rs.getDouble(4));
bill.setIsPayment(rs.getInt(5));
bill.setCreationDate(rs.getTimestamp(6));
bill.setId(rs.getInt(8));
Provider pro = new Provider();
pro.setProName(rs.getString(7));
bill.setProvider(pro);
list.add(bill);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll(null, null, rs);
}
return list;
}
@Override
public int selectAllCount() {
int rows = 0;
String sql="select count(b.id) from smbms_bill b,smbms_provider p where b.providerId = p.id order by b.id";
ResultSet rs = executeQuery(sql);
try {
while(rs.next()){
rows = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll(null, null, rs);
}
return rows;
}
@Override
public List<Bill> selectByNameList( String productName,String providerId,String isPayment,int start, int size) {
List<Bill> list = new ArrayList<Bill>();
String sql="select b.billCode,b.productName,b.providerId,b.totalPrice,b.isPayment,b.creationDate,p.proName,b.id from smbms_bill b,smbms_provider p where b.providerId = p.id ";
List<Object> obj = new ArrayList<Object>();
//商品名称productName
//供应商id providerId
//是否付款 isPayment
String str = "";
if(productName!=null&&productName.trim().length()>0){
str+=" and productName like ?";
obj.add("%"+productName+"%");
}
if(providerId!=null&&providerId.trim().length()>0&&Integer.parseInt(providerId)>0){
str+=" and providerId=?";
obj.add(providerId);
}if(isPayment!=null&&isPayment.trim().length()>0&&Integer.parseInt(isPayment)>0){
str+=" and isPayment=?";
obj.add(isPayment);
}
obj.add((start-1)*size);
obj.add(size);
sql=sql+str+" limit ?,?";
ResultSet rs = executeQuery(sql, obj.toArray());
try {
while(rs.next()){
Bill bill = new Bill();
bill.setBillCode(rs.getString(1));
bill.setProductName(rs.getString(2));
bill.setProviderId(rs.getInt(3));
bill.setTotalPrice(rs.getDouble(4));
bill.setIsPayment(rs.getInt(5));
bill.setCreationDate(rs.getTimestamp(6));
bill.setId(rs.getInt(8));
Provider pro = new Provider();
pro.setProName(rs.getString(7));
bill.setProvider(pro);
list.add(bill);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll(null, null, rs);
}
return list;
}
@Override
public int selectByNameCount(String productName,String providerId,String isPayment) {
int rows=0;
String sql="select count(b.id) from smbms_bill b,smbms_provider p where b.providerId = p.id ";
List<Object> obj = new ArrayList<Object>();
//商品名称productName
//供应商id providerId
//是否付款 isPayment
String str = "";
if(productName!=null&&productName.trim().length()>0){
str+=" and productName like ?";
obj.add("%"+productName+"%");
}
if(providerId!=null&&providerId.trim().length()>0&&Integer.parseInt(providerId)>0){
str+=" and providerId=?";
obj.add(providerId);
}if(isPayment!=null&&isPayment.trim().length()>0&&Integer.parseInt(isPayment)>0){
str+=" and isPayment=?";
obj.add(isPayment);
}
sql=sql+str;
ResultSet rs = executeQuery(sql, obj.toArray());
try {
while(rs.next()){
rows = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll(null, null, rs);
}
return rows;
}
@Override
public int addBill(Bill bill) {
String sql = "insert into smbms_bill( billCode,productName,providerId,productCount,totalPrice,isPayment,createdBy,creationDate) values(?,?,?,?,?,?,?,?)";
int rows = executeUpdate(sql, bill.getBillCode(),bill.getProductName(),bill.getProviderId(),bill.getProductCount(),
bill.getTotalPrice(),bill.getIsPayment(),bill.getCreatedBy(),bill.getCreationDate());
return rows;
}
@Override
public Bill selectById(int id) {
Bill bill = new Bill();
String sql="select b.billCode,b.productName,b.providerId,b.totalPrice,b.isPayment,b.creationDate,p.proName,b.id,b.productUnit,b.productcount from smbms_bill b,smbms_provider p where b.providerId = p.id order by b.id and b.id=?";
ResultSet rs = executeQuery(sql, id);
try {
while(rs.next()){
bill.setBillCode(rs.getString(1));
bill.setProductName(rs.getString(2));
bill.setProviderId(rs.getInt(3));
bill.setTotalPrice(rs.getDouble(4));
bill.setIsPayment(rs.getInt(5));
bill.setCreationDate(rs.getTimestamp(6));
bill.setId(rs.getInt(8));
Provider pro = new Provider();
pro.setProName(rs.getString(7));
bill.setProvider(pro);
bill.setProductUnit(rs.getString(9));
bill.setProductCount(rs.getInt(10));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll(null, null, rs);
}
return bill;
}
@Override
public int update(Bill bill) {
String sql = "update smbms_bill set billCode=?,productName=?,providerId=?,productCount=?,totalPrice=?,isPayment=?,modifyBy=?,modifyDate=? where id=?";
int rows = executeUpdate(sql, bill.getBillCode(),bill.getProductName(),bill.getProviderId(),bill.getProductCount(),
bill.getTotalPrice(),bill.getIsPayment(),bill.getModifyBy(),bill.getModifyDate(),bill.getId());
return rows;
}
@Override
public int delete(int id) {
String sql = "delete from smbms_bill where id=?";
int rows = executeUpdate(sql, id);
return rows;
}
//通过供应商id来查供应商名称
@Override
public String getProviderNameByProId(int proId) {
String sql = "select proName from smbms_provider where id = ?";
ResultSet rs = executeQuery(sql, proId);
try {
while(rs.next()){
String proName = rs.getString(1);
return proName;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
service层,只写出了订单的实现层
package com.yccz.service.impl;
import java.util.List;
import com.yccz.dao.BillDao;
import com.yccz.dao.impl.BillDaoImpl;
import com.yccz.entity.Bill;
import com.yccz.service.BillService;
public class BillServiceImpl implements BillService {
BillDao bd = new BillDaoImpl();
@Override
public List selectAllList(int start, int size) {
return bd.selectAllList(start, size);
}
@Override
public int selectAllCount() {
return bd.selectAllCount();
}
@Override
public List<Bill> selectByNameList(String productName, String providerId,
String isPayment, int start, int size) {
return bd.selectByNameList(productName, providerId, isPayment, start, size);
}
@Override
public int selectByNameCount(String productName, String providerId,
String isPayment) {
return bd.selectByNameCount(productName, providerId, isPayment);
}
@Override
public int addBill(Bill bill) {
return bd.addBill(bill);
}
@Override
public Bill selectById(int id) {
return bd.selectById(id);
}
@Override
public int update(Bill bill) {
return bd.update(bill);
}
@Override
public int delete(int id) {
return bd.delete(id);
}
@Override
public String getProviderNameByProId(int proId) {
return new BillDaoImpl().getProviderNameByProId(proId);
}
}
**Servlet,只列出订单的servlet**
package com.yccz.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yccz.entity.Bill;
import com.yccz.entity.Provider;
import com.yccz.entity.User;
import com.yccz.service.BillService;
import com.yccz.service.ProviderService;
import com.yccz.service.impl.BillServiceImpl;
import com.yccz.service.impl.ProviderServiceImpl;
import com.yccz.util.BillPage;
public class BillServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
BillService bs = new BillServiceImpl();
ProviderService ps = new ProviderServiceImpl();
String opr = request.getParameter("opr");
if(opr==null||opr.length()<0){
opr="list";
}
//获取当前页
String currPageNo = request.getParameter("currPageNo");
if(currPageNo==null||currPageNo.length()<0){
currPageNo = "1";
}
if("list".equals(opr)){//查询全部
int totalCount = bs.selectAllCount();
BillPage bp = new BillPage();
bp.setCurrPageNo(Integer.parseInt(currPageNo));
bp.setTotalCount(totalCount);
ProviderService providerService = new ProviderServiceImpl();
List<Provider> prolist = providerService.selectAll();
List<Bill> list = bs.selectAllList(bp.getCurrPageNo(), bp.getPageSize());
request.setAttribute("list", list);
request.setAttribute("prolist", prolist);
request.setAttribute("currPageNo", currPageNo);
request.setAttribute("totalPageCount", bp.getTotalPageCount());
request.getRequestDispatcher("jsp/billlist.jsp").forward(request, response);
}else if("query".equals(opr)){//条件查询
ProviderService providerService = new ProviderServiceImpl();
List<Provider> prolist = providerService.selectAll();
String queryProductName = request.getParameter("queryProductName");
String queryProviderId = request.getParameter("queryProviderId");
String queryIsPayment = request.getParameter("queryIsPayment");
int totalCount = bs.selectByNameCount(queryProductName, queryProviderId, queryIsPayment);
BillPage bp = new BillPage();
bp.setCurrPageNo(Integer.parseInt(currPageNo));
bp.setTotalCount(totalCount);
List<Bill> list = bs.selectByNameList(queryProductName, queryProviderId, queryIsPayment,bp.getCurrPageNo(), bp.getPageSize());
request.setAttribute("prolist", prolist);
request.setAttribute("list", list);
request.setAttribute("currPageNo", currPageNo);
request.setAttribute("totalPageCount", bp.getTotalPageCount());
request.setAttribute("queryProductName", queryProductName);
request.setAttribute("queryProviderId", queryProviderId);
request.setAttribute("queryIsPayment", queryIsPayment);
request.getRequestDispatcher("jsp/billlist.jsp").forward(request, response);
}else if("queryPro".equals(opr)){
List<Provider > list = ps.selectAll();
request.setAttribute("list", list);
request.getRequestDispatcher("jsp/billadd.jsp").forward(request, response);
}else if("add".equals(opr)){
String billcode = request.getParameter("billcode");
String productname = request.getParameter("productname");
String productunit = request.getParameter("productunit");
String productcount = request.getParameter("productcount");
int productCount = 0;
if(productcount!=null&&productcount.length()>0){
productCount = Integer.parseInt(productcount);
}
String totalprice = request.getParameter("totalprice");
double totalPrice=0;
int providerId=0;
int isPayment = 0;
if(totalprice!=null&&totalprice.length()>0){
totalPrice=Double.parseDouble(totalprice);
}
String providerid = request.getParameter("providerid");
if(providerid!=null&&providerid.length()>0){
providerId = Integer.parseInt(providerid);
}
String ispayment = request.getParameter("ispayment");
if(ispayment!=null&&ispayment.length()>0){
isPayment = Integer.parseInt(ispayment);
}
Bill bill = new Bill();
bill.setBillCode(billcode);
bill.setProductName(productname);
bill.setProductUnit(productunit);
bill.setProductCount(productCount);
bill.setTotalPrice(totalPrice);
bill.setProviderId(providerId);
bill.setIsPayment(isPayment);
User user = (User)request.getSession().getAttribute("user");
bill.setCreatedBy(user.getId());
bill.setCreationDate(new Date());
int rows = bs.addBill(bill);
if(rows>0){
out.print("<script>alert('订单添加成功')"
+ ";location.href='jsp/billlist.jsp'"
+ "</script>");
}
else{
out.print("<script>alert('订单添加失败')"
+ ";history.back()"
+ "</script>");
}
}else if("queryById".equals(opr)){
String id=request.getParameter("id");
String method = request.getParameter("method");
Bill bill = bs.selectById(Integer.parseInt(id));
String proName = bs.getProviderNameByProId(bill.getId());
bill.setProviderName(proName);
request.setAttribute("bill", bill);
if(method.equals("update")){
ProviderService providerService = new ProviderServiceImpl();
List<Provider> prolist = providerService.selectAll();
request.setAttribute("prolist", prolist);
request.getRequestDispatcher("jsp/billmodify.jsp").forward(request, response);
}else if(method.equals("query")){
request.getRequestDispatcher("jsp/billview.jsp").forward(request, response);
}
}else if("update".equals(opr)){
String id=request.getParameter("id");
String billcode = request.getParameter("billcode");
String productname = request.getParameter("productname");
String productunit = request.getParameter("productunit");
String productcount = request.getParameter("productcount");
int productCount = 0;
if(productcount!=null&&productcount.length()>0){
productCount = Integer.parseInt(productcount);
}
String totalprice = request.getParameter("totalprice");
double totalPrice=0;
int providerId=0;
int isPayment = 0;
if(totalprice!=null&&totalprice.length()>0){
totalPrice=Double.parseDouble(totalprice);
}
String providerid = request.getParameter("providerid");
if(providerid!=null&&providerid.length()>0){
providerId = Integer.parseInt(providerid);
}
String ispayment = request.getParameter("ispayment");
if(ispayment!=null&&ispayment.length()>0){
isPayment = Integer.parseInt(ispayment);
}
Bill bill = new Bill();
bill.setId(Integer.parseInt(id));
bill.setBillCode(billcode);
bill.setProductName(productname);
bill.setProductUnit(productunit);
bill.setProductCount(productCount);
bill.setTotalPrice(totalPrice);
bill.setProviderId(providerId);
bill.setIsPayment(isPayment);
User user = (User)request.getSession().getAttribute("user");
bill.setModifyBy(user.getId());
bill.setModifyDate(new Date());
int rows = bs.update(bill);
if(rows>0){
out.print("<script>alert('订单修改成功')"
+ ";location.href='BillServlet'"
+ "</script>");
}
else{
out.print("<script>alert('订单修改失败')"
+ ";history.back()"
+ "</script>");
}
}else if("delete".equals(opr)){
String id = request.getParameter("id");
int rows = bs.delete(Integer.parseInt(id.trim()));
System.out.println("受影响行数"+rows);
if(rows>0){
out.print("<script>alert('订单删除成功')"
+ ";location.href='BillServlet'"
+ "</script>");
}
else{
out.print("<script>alert('订单删除失败')"
+ ";history.back()"
+ "</script>");
}
}else{
}
out.flush();
out.close();
}
}
**util包(工具类)**
package com.yccz.util;
import java.io.IOException;
import java.io.InputStream;
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 java.util.Properties;
import javax.annotation.Resource;
/**
*
* @ClassName: BaseDao
* @Description: 数据库工具类
* @author 志辉
* @date 2017年10月11日
*/
public class BaseDao {
private Connection conn = null;
private static Properties props=null;
private static String driver;
private static String url;
private static String user;
private static String password;
static{
InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(“database.properties”);
if(is==null)
throw new RuntimeException(“找不到数据库参数配置文件”);
props = new Properties();
try{
props.load(is);
driver = props.getProperty("driver");
url = props.getProperty("url");
user = props.getProperty("user");
password = props.getProperty("password");
}catch (IOException e) {
throw new RuntimeException("数据库配置参数加载错误!", e);
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public Connection getConnection(){
try {
Class.forName(driver);//加载驱动
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 关闭数据库连接。
*
* @param conn
* 数据库连接
* @param stmt
* Statement对象
* @param rs
* 结果集
*/
public void closeAll(Connection conn, Statement stmt, ResultSet rs) {
// 若结果集对象不为空,则关闭
try {
if (rs != null && !rs.isClosed()){
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}
// 若Statement对象不为空,则关闭
try {
if (stmt != null && !stmt.isClosed())
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
// 若数据库连接对象不为空,则关闭
try {
if (conn != null && !conn.isClosed())
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 增、删、改操作
*
* @param sql
* sql语句
* @param prams
* 参数数组
* @return 执行结果
*/
protected int executeUpdate(String sql, Object... params) {
int result = 0;
conn = this.getConnection();
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll(null, pstmt, null);
}
return result;
}
/**
* 查询操作
*
* @param sql
* sql语句
* @param params
* 参数数组
* @return 查询结果集
*/
protected ResultSet executeQuery(String sql, Object... params) {
conn = this.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
System.out.println(sql);
try {
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
System.out.println(params[i]);
}
rs = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
}
package com.yccz.util;
import java.util.List;
import com.yccz.entity.Bill;
/**
*
* @ClassName: BillPage
* @Description: 分页的工具类
* @author 志辉
* @date 2017年10月11日
*/
public class BillPage {
// 总页数
private int totalPageCount = 0;
// 页面大小,即每页显示记录数
private int pageSize = 5;
// 记录总数
private int totalCount;
// 当前页码
private int currPageNo = 1;
// 每页新闻集合
private List billList;
public int getCurrPageNo() {
return currPageNo;
}
public void setCurrPageNo(int currPageNo) {
if (currPageNo > 0)
this.currPageNo = currPageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
if (pageSize > 0)
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {//总记录60条数据 页面大小 5条
if (totalCount > 0) {
this.totalCount = totalCount;
// 计算总页数
this.totalPageCount = this.totalCount % pageSize == 0 ? (this.totalCount / pageSize)
: (this.totalCount / pageSize + 1);
if(currPageNo>totalPageCount)
this.currPageNo = totalPageCount;
}
}
public int getTotalPageCount() {
return totalPageCount;
}
public void setTotalPageCount(int totalPageCount) {
this.totalPageCount = totalPageCount;
}
public List<Bill> getBillList() {
return billList;
}
public void setBillList(List<Bill> billList) {
this.billList = billList;
}
}
**jsp**
billlist.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="./common/head.jsp"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div class="right">
<div class="location">
<strong>你现在所在的位置是:</strong>
<span>订单管理页面</span>
</div>
<div class="search">
<form method="get" action="BillServlet">
<input name="opr" value="query" class="input-text" type="hidden">
<span>商品名称:</span>
<input name="queryProductName" type="text" value="">
<span>供应商:</span>
<select name="queryProviderId">
<option value="0">--请选择--</option>
<c:forEach items="${prolist }" var="pro">
<option value=${pro.id}>${pro.proName}</option>
</c:forEach>
</select>
<span>是否付款:</span>
<select name="queryIsPayment">
<option value="0">--请选择--</option>
<option value="1">未付款</option>
<option value="2" >已付款</option>
</select>
<input type="hidden" name="pageIndex" value="1"/>
<input value="查 询" type="submit" id="searchbutton">
<a href="BillServlet?opr=queryPro">添加订单</a>
</form>
</div>
<!--账单表格 样式和供应商公用-->
<table class="providerTable" cellpadding="0" cellspacing="0">
<tr class="firstTr">
<th width="10%">订单编码</th>
<th width="20%">商品名称</th>
<th width="10%">供应商</th>
<th width="10%">订单金额</th>
<th width="10%">是否付款</th>
<th width="10%">创建时间</th>
<th width="30%">操作</th>
</tr>
<c:forEach items="${list }" var="bill">
<tr>
<td>
<span>${bill.billCode }</span>
</td>
<td>
<span>${bill.productName }</span>
</td>
<td>
<span>${bill.provider.proName }</span>
</td>
<td>
<span>${bill.totalPrice }</span>
</td>
<!-- 显示是否付款 -->
<td>
<span>
<c:choose>
<c:when test="${bill.isPayment eq 2}">
已付款
</c:when>
<c:otherwise>
未付款
</c:otherwise>
</c:choose>
</span>
</td>
<!-- 创建时间 -->
<td>
<span>
${bill.creationDate }
</span>
</td>
<td>
<span><a class="viewBill" href="BillServlet?opr=queryById&id=${bill.id }&method=query" ><img src="/SupermarketSys/statics/images/read.png" alt="查看" title="查看"/></a></span>
<span><a class="modifyBill" href="BillServlet?opr=queryById&id=${bill.id }&method=update"><img src="/SupermarketSys/statics/images/xiugai.png" alt="修改" title="修改"/></a></span>
<%-- <span><a class="deleteBill" href="javascript:confirm('确认删除?')?BillServlet?opr=delete&id=${bill.id }:history.back()"><img src="/SupermarketSys/statics/images/schu.png" alt="删除" title="删除"/></a></span> --%>
<span><a class="deleteBill" href="BillServlet?opr=delete&id=${bill.id }"><img src="/SupermarketSys/statics/images/schu.png" alt="删除" title="删除"/></a></span>
</td>
</tr>
</c:forEach>
</table>
<p align="center">
当前页数:[${currPageNo }/${totalPageCount }]
<c:choose>
<c:when test="${empty queryProductName and empty queryProviderId and empty queryIsPayment }">
<c:if test="${currPageNo >1}">
<a href="BillServlet?opr=list&currPageNo=1">首页</a>
<a href="BillServlet?opr=list&currPageNo=${currPageNo-1}">上一页</a>
</c:if>
<c:if test="${currPageNo ne totalPageCount }">
<a href="BillServlet?opr=list&currPageNo=${currPageNo+1}">下一页</a>
<a href="BillServlet?opr=list&currPageNo=${totalPageCount}">末页</a>
</c:if>
</c:when>
<c:otherwise>
<c:if test="${currPageNo >1}">
<a href="BillServlet?opr=query&queryProductName=${queryProductName }&queryProviderId=${queryProviderId }&queryIsPayment=${queryIsPayment }&currPageNo=1">首页</a>
<a href="BillServlet?opr=query&queryProductName=${queryProductName }&queryProviderId=${queryProviderId }&queryIsPayment=${queryIsPayment }&currPageNo=${currPageNo-1}">上一页</a>
</c:if>
<c:if test="${currPageNo ne totalPageCount }">
<a href="BillServlet?opr=query&queryProductName=${queryProductName }&queryProviderId=${queryProviderId }&queryIsPayment=${queryIsPayment }&currPageNo=${currPageNo+1}">下一页</a>
<a href="BillServlet?opr=query&queryProductName=${queryProductName }&queryProviderId=${queryProviderId }&queryIsPayment=${queryIsPayment }&currPageNo=${totalPageCount}">末页</a>
</c:if>
</c:otherwise>
</c:choose>
<%-- <c:if test="${currPageNo >1}">
<a href="BillServlet?opr=list&currPageNo=1">首页</a>
<a href="BillServlet?opr=list&currPageNo=${currPageNo-1}">上一页</a>
</c:if>
<c:if test="${currPageNo ne totalPageCount }">
<a href="BillServlet?opr=list&currPageNo=${currPageNo+1}">下一页</a>
<a href="BillServlet?opr=list&currPageNo=${totalPageCount}">末页</a>
</c:if> --%>
</p>
</div>
</section>
<!--点击删除按钮后弹出的页面-->
<div class="zhezhao"></div>
<div class="remove" id="removeBi">
<div class="removerChid">
<h2>提示</h2>
<div class="removeMain">
<p>你确定要删除该订单吗?</p>
<a href="#" id="yes">确定</a>
<a href="#" id="no">取消</a>
</div>
</div>
</div>
<%@include file="./common/foot.jsp" %>
<script type="text/javascript" src="statics/js/billlist.js"></script>
billadd.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="./common/head.jsp"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div class="right">
<div class="location">
<strong>你现在所在的位置是:</strong>
<span>订单管理页面 >> 订单添加页面</span>
</div>\
<script type="text/javascript">
</script>
<div class="providerAdd">
<form id="billForm" name="billForm" method="post" action="BillServlet">
<!--div的class 为error是验证错误,ok是验证成功-->
<input type="hidden" name="opr" value="add">
<div class="">
<label for="billCode" >订单编码:</label>
<input type="text" name="billcode" class="text" id="billCode" value="">
<!-- 放置提示信息 -->
<font color="red"></font>
</div>
<div>
<label for="productName">商品名称:</label>
<input type="text" name="productname" id="productName" value="">
<font color="red"></font>
</div>
<div>
<label for="productUnit">商品单位:</label>
<input type="text" name="productunit" id="productUnit" value="">
<font color="red"></font>
</div>
<div>
<label for="productCount">商品数量:</label>
<input type="text" name="productcount" id="productCount" value="">
<font color="red"></font>
</div>
<div>
<label for="totalPrice">总金额:</label>
<input type="text" name="totalprice" id="totalPrice" value="">
<font color="red"></font>
</div>
<div>
<label >供应商:</label>
<select name="providerid" id="providerId">
<c:forEach items="${list }" var="pro">
<option value="${pro.id }">${pro.proName }</option>
</c:forEach>
</select>
<font color="red"></font>
</div>
<div>
<label >是否付款:</label>
<input type="radio" name="ispayment" value="1" checked="checked">未付款
<input type="radio" name="ispayment" value="2" >已付款
</div>
<div class="providerAddBtn">
<input type="button" name="add" id="add" value="保存">
<input type="button" id="back" name="back" value="返回" >
</div>
</form>
</div>
</div>
</section>
<%@include file="./common/foot.jsp" %>
<script type="text/javascript" src="statics/js/billadd.js"></script>
billmodify.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="./common/head.jsp"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div class="right">
<div class="location">
<strong>你现在所在的位置是:</strong>
<span>订单管理页面 >> 订单添加页面</span>
</div>
<div class="providerAdd">
<form id="billForm" name="billForm" method="post" action="BillServlet">
<input type="hidden" name="opr" value="update">
<input type="hidden" name="id" value="${bill.id }">
<!--div的class 为error是验证错误,ok是验证成功-->
<div class="">
<label for="billCode">订单编码:</label>
<input type="text" name="billcode" id="billCode" value="${bill.billCode }" readonly="readonly">
</div>
<div>
<label for="productName">商品名称:</label>
<input type="text" name="productname" id="productName" value="${bill.productName }">
<font color="red"></font>
</div>
<div>
<label for="productUnit">商品单位:</label>
<input type="text" name="productunit" id="productUnit" value="${bill.productUnit }">
<font color="red"></font>
</div>
<div>
<label for="productCount">商品数量:</label>
<input type="text" name="productcount" id="productCount" value="${bill.productCount }">
<font color="red"></font>
</div>
<div>
<label for="totalPrice">总金额:</label>
<input type="text" name="totalprice" id="totalPrice" value="${bill.totalPrice }">
<font color="red"></font>
</div>
<div>
<label for="providerId">供应商:</label>
<input type="hidden" value="" id="pid" />
<select name="providerid" id="providerId">
<option value="${bill.providerId }">${bill.provider.proName }</option>
<c:forEach items="${prolist }" var="pro">
<option value=${pro.id}>${pro.proName}</option>
</c:forEach>
</select>
<font color="red"></font>
</div>
<div>
<label >是否付款:</label>
<c:if test="${bill.isPayment == 1 }">
<input type="radio" name="ispayment" value="1" checked="checked">未付款
<input type="radio" name="ispayment" value="2" >已付款
</c:if>
<c:if test="${bill.isPayment == 2 }">
<input type="radio" name="ispayment" value="1">未付款
<input type="radio" name="ispayment" value="2" checked="checked">已付款
</c:if>
</div>
<div class="providerAddBtn">
<input type="button" name="save" id="save" value="保存">
<input type="button" id="back" name="back" value="返回" >
</div>
</form>
</div>
</div>
</section>
<%@include file="./common/foot.jsp" %>
<script type="text/javascript" src="statics/js/billmodify.js"></script>
billview.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="./common/head.jsp"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div class="right">
<div class="location">
<strong>你现在所在的位置是:</strong>
<span>订单管理页面 >> 信息查看</span>
</div>
<div class="providerView">
<p><strong>订单编号:</strong><span>${bill.billCode }</span></p>
<p><strong>商品名称:</strong><span>${bill.productName}</span></p>
<p><strong>商品单位:</strong><span>${bill.productUnit }</span></p>
<p><strong>商品数量:</strong><span>${bill.productCount }</span></p>
<p><strong>总金额:</strong><span>${bill.totalPrice }</span></p>
<p><strong>供应商:</strong><span>${bill.providerName }</span></p>
<p><strong>是否付款:</strong>
<span>
<c:if test="${bill.isPayment == 1}">未付款</c:if>
<c:if test="${bill.isPayment == 2}">已付款</c:if>
</span>
</p>
<div class="providerAddBtn">
<input type="button" id="back" name="back" value="返回" >
</div>
</div>
</div>
</section>
<%@include file="./common/foot.jsp" %>
<script type="text/javascript" src="statics/js/billview.js"></script>
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SuperMarketSys</display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.yccz.servlet.LoginServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>BillServlet</servlet-name>
<servlet-class>com.yccz.servlet.BillServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>BillServlet</servlet-name>
<url-pattern>/BillServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/UserServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>