企业进销存管理系统(二)

五、创建项目

(1)启动Eclipse,在Eclipse工作台中选择“文件”/“新建”/“Java项目”命令,如图所示。
企业进销存管理系统(二)_第1张图片
(2)弹出“新建Java项目”对话框,在”项目名“文本框中输入新建项目的名称;在“位置”栏中选择项目的创建位置,可以选择默认位置(即当前工作空间),也可以单击“浏览”按钮指定具体的位置,这里采用默认位置;然后再JRE栏中选择该项目所使用的JRE(Java SE Runtime Environment,Java运行环境);接下来再在“项目布局”栏中选中“为源文件和类文件创建单独的文件夹”单选按钮;最后还可以为该项目指定工作集,这里不指定任何工作集,单击“完成”按钮,如图所示。
企业进销存管理系统(二)_第2张图片

六、系统文件夹组织结构

由于本系统是由团队进行开发的,所以在编写代码前需要制定好项目的系统文件夹组织结构,如不同的Java包存放不同的窗体、公共类、数据模型、工具类或者图片资源等,这样不但可以保证开发团队开发的一致性,也可规范系统的整体架构。创建完系统中可能用到的文件夹或Java包之后,在开发时,只需将所创建的类文件或资源文件保存到相应的文件夹中即可。

七、公共类设计

公共类也是代码重用的一种形式,它将各个功能模块经常调用的方法提取到共用的Java类中。例如,访问数据库的Dao类容纳了所有访问数据库的方法,并同时管理着数据库的连接和关闭。这样不但实现了项目代码的重用,还提高了程序的性能和代码的可读性。

1.Item公共类

Item公共类是对数据表最常用的id和name属性的封装,用于Swing列表、表格、下拉列表框等组件的赋值。该类重写了toString()方法,在该方法中只输出name属性,所以Item类在Swing组件显示文本时只包含名称信息,不会连带着id属性。但是,在获取组件的内容时,获取的是Item类的对象,从该对象中可以很容易地获取id属性,然后通过该属性到数据库中获取唯一的数据。Item公共类的程序代码如下:

package com.lzw;

public class Item {// 数据表公共类

	private String id;// 编号属性
	private String name;// 名称信息

	public Item() {// 缺省构造函数
	}

	public Item(String id, String name) {// 完整构造函数
		this.id = id;
		this.name = name;
	}

	// 使用Getters and Setters方法将数据表公共类的私有属性封装起来
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	// 重写toString()方法,只输出名称信息
	public String toString() {
		return getName();
	}
}

代码中相应的getXXX()方法和setXXX()方法,是访问不同属性的方法,而相应的属性则被声明为private属性,这样就实现了属性的封装。

2.数据模型公共类

com.lzw.dao.model包中存放的是数据模型公共类,它们对应着数据库中不同的数据表,这些模型将被访问数据库的Dao类和程序中各个模块甚至各个组件所使用。和Item公共类的使用方法类似,数据模型也是对数据表中所有字段(属性)的封装,但是数据模型是纯粹的模型类,它不但需要重写父类的toString()方法,还有重写hashCode()方法和equals()方法(这两个方法分别用于生成模型对象的哈希代码和判断模型对象是否相同)。模型类主要用于存储数据,并通过相应的getXXX()方法和setXXX()方法实现不同属性的访问原则。商品数据表对应的模型类代码如下:

package com.lzw.dao.model;

public class TbSpinfo implements java.io.Serializable {// 商品信息(实现序列化接口)

	private String id;// 商品编号
	private String spname;// 商品名称
	private String jc;// 商品简称
	private String cd;// 产地
	private String dw;// 商品计量单位
	private String gg;// 商品规格
	private String bz;// 包装
	private String ph;// 批号
	private String pzwh;// 批准文号
	private String memo;// 备注
	private String gysname;// 供应商名称
	
	// 使用Getters and Setters方法将商品信息类的私有属性封装起来
	public TbSpinfo() {
	}

	public TbSpinfo(String id) {
		this.id = id;
	}

	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getSpname() {
		return this.spname;
	}

	public void setSpname(String spname) {
		this.spname = spname;
	}

	public String getJc() {
		return this.jc;
	}

	public void setJc(String jc) {
		this.jc = jc;
	}

	public String getCd() {
		return this.cd;
	}

	public void setCd(String cd) {
		this.cd = cd;
	}

	public String getDw() {
		return this.dw;
	}

	public void setDw(String dw) {
		this.dw = dw;
	}

	public String getGg() {
		return this.gg;
	}

	public void setGg(String gg) {
		this.gg = gg;
	}

	public String getBz() {
		return this.bz;
	}

	public void setBz(String bz) {
		this.bz = bz;
	}

	public String getPh() {
		return this.ph;
	}

	public void setPh(String ph) {
		this.ph = ph;
	}

	public String getPzwh() {
		return this.pzwh;
	}

	public void setPzwh(String pzwh) {
		this.pzwh = pzwh;
	}

	public String getMemo() {
		return this.memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public String getGysname() {
		return this.gysname;
	}

	public void setGysname(String gysname) {
		this.gysname = gysname;
	}

	public String toString() {
		return getSpname();
	}

	@Override
	public int hashCode() {// 重写hashCode方法
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((bz == null) ? 0 : bz.hashCode());
		result = PRIME * result + ((cd == null) ? 0 : cd.hashCode());
		result = PRIME * result + ((dw == null) ? 0 : dw.hashCode());
		result = PRIME * result + ((gg == null) ? 0 : gg.hashCode());
		result = PRIME * result + ((gysname == null) ? 0 : gysname.hashCode());
		result = PRIME * result + ((id == null) ? 0 : id.hashCode());
		result = PRIME * result + ((jc == null) ? 0 : jc.hashCode());
		result = PRIME * result + ((memo == null) ? 0 : memo.hashCode());
		result = PRIME * result + ((ph == null) ? 0 : ph.hashCode());
		result = PRIME * result + ((pzwh == null) ? 0 : pzwh.hashCode());
		result = PRIME * result + ((spname == null) ? 0 : spname.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {// 重写equals方法
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final TbSpinfo other = (TbSpinfo) obj;
		if (bz == null) {
			if (other.bz != null)
				return false;
		} else if (!bz.equals(other.bz))
			return false;
		if (cd == null) {
			if (other.cd != null)
				return false;
		} else if (!cd.equals(other.cd))
			return false;
		if (dw == null) {
			if (other.dw != null)
				return false;
		} else if (!dw.equals(other.dw))
			return false;
		if (gg == null) {
			if (other.gg != null)
				return false;
		} else if (!gg.equals(other.gg))
			return false;
		if (gysname == null) {
			if (other.gysname != null)
				return false;
		} else if (!gysname.equals(other.gysname))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (jc == null) {
			if (other.jc != null)
				return false;
		} else if (!jc.equals(other.jc))
			return false;
		if (memo == null) {
			if (other.memo != null)
				return false;
		} else if (!memo.equals(other.memo))
			return false;
		if (ph == null) {
			if (other.ph != null)
				return false;
		} else if (!ph.equals(other.ph))
			return false;
		if (pzwh == null) {
			if (other.pzwh != null)
				return false;
		} else if (!pzwh.equals(other.pzwh))
			return false;
		if (spname == null) {
			if (other.spname != null)
				return false;
		} else if (!spname.equals(other.spname))
			return false;
		return true;
	}

}

其他模型类的定义与商品模型类的定义方法类似,其属性内容就是数据表中相应的字段。
com.lzw.dao.model包中包含的数据模型类如表所示:

com.lzw.dao.model包中的数据模型类
类名 说明
TbGysinfo 供应商数据表模型类
TbJsr 经手人数据表模型类
TbKhinfo 客户数据表模型类
TbKucun 库存数据表模型类
TbRkthDetail 进货退货详细数据表模型类
TbRkthMain 进货退货主数据表模型类
TbRukuDetail 进货详细信息数据表模型类
TbRukuMain 进货主表模型类
TbSellDetail 销售详细信息数据表模型类
TbSellMain 销售主表模型类
TbSpinfo 商品信息数据表模型类
TbXsthDetail 销售退货详细信息数据表模型类
TbXsthMain 销售退货主表模型类
TbGysinfo类
package com.lzw.dao.model;

public class TbGysinfo implements java.io.Serializable {// 供应商信息(实现序列化接口)

	private String id;// 供应商编号
	private String name;// 供应商名称
	private String jc;// 供应商简称
	private String address;// 供应商地址
	private String bianma;// 邮政编码
	private String tel;// 电话
	private String fax;// 传真
	private String lian;// 联系人
	private String ltel;// 联系电话
	private String yh;// 开户银行
	private String mail;// 电子信箱

	public TbGysinfo() {// 缺省构造函数
	}

	public TbGysinfo(String id) {// 最小构造函数(主键)
		this.id = id;
	}

	public TbGysinfo(String id, String name, String jc, String address, String bianma, String tel, String fax,
			String lian, String ltel, String yh, String mail) {// 完整构造函数
		this.id = id;
		this.name = name;
		this.jc = jc;
		this.address = address;
		this.bianma = bianma;
		this.tel = tel;
		this.fax = fax;
		this.lian = lian;
		this.ltel = ltel;
		this.yh = yh;
		this.mail = mail;
	}

	// 使用Getters and Setters方法将供应商信息类的私有属性封装起来
	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getJc() {
		return this.jc;
	}

	public void setJc(String jc) {
		this.jc = jc;
	}

	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getBianma() {
		return this.bianma;
	}

	public void setBianma(String bianma) {
		this.bianma = bianma;
	}

	public String getTel() {
		return this.tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getFax() {
		return this.fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public String getLian() {
		return this.lian;
	}

	public void setLian(String lian) {
		this.lian = lian;
	}

	public String getLtel() {
		return this.ltel;
	}

	public void setLtel(String ltel) {
		this.ltel = ltel;
	}

	public String getYh() {
		return this.yh;
	}

	public void setYh(String yh) {
		this.yh = yh;
	}

	public String getMail() {
		return this.mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

}

TbJsr类

package com.lzw.dao.model;

public class TbJsr {// 经手人信息

	private String name;// 经手人姓名
	private String sex;// 经手人性别
	private String age;// 经手人年龄
	private String tel;// 经手人电话
	
	// 使用Getters and Setters方法将经手人信息类的私有属性封装起来
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String username) {
		this.sex = username;
	}

	public String getAge() {
		return this.age;
	}

	public void setAge(String pass) {
		this.age = pass;
	}

	public String getTel() {
		return this.tel;
	}

	public void setTel(String quan) {
		this.tel = quan;
	}

}

TbKhinfo类

package com.lzw.dao.model;

public class TbKhinfo implements java.io.Serializable {// 客户信息(实现序列化接口)

	private String id;// 客户编号
	private String khname;// 客户名称
	private String jian;// 客户简称
	private String address;// 客户地址
	private String bianma;// 邮编
	private String tel;// 电话
	private String fax;// 传真
	private String lian;// 联系人
	private String ltel;// 联系电话
	private String mail;// 电子邮箱
	private String xinhang;// 开户银行
	private String hao;// 银行账号

	public TbKhinfo() {// 缺省构造函数
	}

	public TbKhinfo(String id) {// 最小构造函数(主键)
		this.id = id;
	}

	public TbKhinfo(String id, String khname, String jian, String address, String bianma, String tel, String fax,
			String lian, String ltel, String mail, String xinhang, String hao) {// 完整构造函数
		this.id = id;
		this.khname = khname;
		this.jian = jian;
		this.address = address;
		this.bianma = bianma;
		this.tel = tel;
		this.fax = fax;
		this.lian = lian;
		this.ltel = ltel;
		this.mail = mail;
		this.xinhang = xinhang;
		this.hao = hao;
	}
	
	// 使用Getters and Setters方法将客户信息类的私有属性封装起来
	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getKhname() {
		return this.khname;
	}

	public void setKhname(String khname) {
		this.khname = khname;
	}

	public String getJian() {
		return this.jian;
	}

	public void setJian(String jian) {
		this.jian = jian;
	}

	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getBianma() {
		return this.bianma;
	}

	public void setBianma(String bianma) {
		this.bianma = bianma;
	}

	public String getTel() {
		return this.tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getFax() {
		return this.fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public String getLian() {
		return this.lian;
	}

	public void setLian(String lian) {
		this.lian = lian;
	}

	public String getLtel() {
		return this.ltel;
	}

	public void setLtel(String ltel) {
		this.ltel = ltel;
	}

	public String getMail() {
		return this.mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getXinhang() {
		return this.xinhang;
	}

	public void setXinhang(String xinhang) {
		this.xinhang = xinhang;
	}

	public String getHao() {
		return this.hao;
	}

	public void setHao(String hao) {
		this.hao = hao;
	}

}

TbKucun类

package com.lzw.dao.model;

public class TbKucun implements java.io.Serializable {// 库存信息(实现序列化接口)

	private String id;// 商品编号
	private String spname;// 商品名称
	private String jc;// 商品简称
	private String cd;// 产地
	private String gg;// 商品规格
	private String bz;// 包装
	private String dw;// 商品计量单位
	private Double dj;// 单价
	private Integer kcsl;// 库存数量

	public TbKucun() {// 缺省构造函数
	}

	public TbKucun(String id) {// 最小构造函数(主键)
		this.id = id;
	}

	public TbKucun(String id, String spname, String jc, String cd, String gg, String bz, String dw, Double dj,
			Integer kcsl) {// 完整构造函数
		this.id = id;
		this.spname = spname;
		this.jc = jc;
		this.cd = cd;
		this.gg = gg;
		this.bz = bz;
		this.dw = dw;
		this.dj = dj;
		this.kcsl = kcsl;
	}
	
	// 使用Getters and Setters方法将库存信息类的私有属性封装起来
	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getSpname() {
		return this.spname;
	}

	public void setSpname(String spname) {
		this.spname = spname;
	}

	public String getJc() {
		return this.jc;
	}

	public void setJc(String jc) {
		this.jc = jc;
	}

	public String getCd() {
		return this.cd;
	}

	public void setCd(String cd) {
		this.cd = cd;
	}

	public String getGg() {
		return this.gg;
	}

	public void setGg(String gg) {
		this.gg = gg;
	}

	public String getBz() {
		return this.bz;
	}

	public void setBz(String bz) {
		this.bz = bz;
	}

	public String getDw() {
		return this.dw;
	}

	public void setDw(String dw) {
		this.dw = dw;
	}

	public Double getDj() {
		return this.dj;
	}

	public void setDj(Double dj) {
		this.dj = dj;
	}

	public Integer getKcsl() {
		return this.kcsl;
	}

	public void setKcsl(Integer kcsl) {
		this.kcsl = kcsl;
	}

	public String toString() {
		return getSpname();
	}
}

TbRkthDetail类

package com.lzw.dao.model;

public class TbRkthDetail implements java.io.Serializable {// 进货退货详细信息(实现序列化接口)

	private Integer id;// 进货退货编号
	private String tbRkthMain;// 进货退货主表
	private String spid;// 商品编号
	private Double dj;// 单价
	private Integer sl;// 数量

	public TbRkthDetail() {// 缺省构造函数
	}

	public TbRkthDetail(String tbRkthMain, String spid, Double dj, Integer sl) {// 完整构造函数
		this.tbRkthMain = tbRkthMain;
		this.spid = spid;
		this.dj = dj;
		this.sl = sl;
	}

	// 使用Getters and Setters方法将进货退货详细信息类的私有属性封装起来
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTbRkthMain() {
		return this.tbRkthMain;
	}

	public void setTbRkthMain(String tbRkthMain) {
		this.tbRkthMain = tbRkthMain;
	}

	public String getSpid() {
		return this.spid;
	}

	public void setSpid(String spid) {
		this.spid = spid;
	}

	public Double getDj() {
		return this.dj;
	}

	public void setDj(Double dj) {
		this.dj = dj;
	}

	public Integer getSl() {
		return this.sl;
	}

	public void setSl(Integer sl) {
		this.sl = sl;
	}
}

TbRkthMain类

package com.lzw.dao.model;

import java.util.HashSet;
import java.util.Set;

public class TbRkthMain implements java.io.Serializable {// 进货退货主表(实现序列化接口)

	private String rkthId;// 进货退货编号
	private String pzs;// 销售品种数
	private String je;// 总计金额
	private String ysjl;// 验收结论
	private String gysname;// 供应商名称
	private String rtdate;// 进货退货时间
	private String czy;// 操作员
	private String jsr;// 经手人
	private String jsfs;// 结算方式
	private Set tbRkthDetails = new HashSet(0);// 进货退货详细信息

	public TbRkthMain() {// 缺省构造函数
	}

	public TbRkthMain(String rkthId, String pzs, String je, String ysjl, String gysname, String rtdate, String czy,
			String jsr, String jsfs) {// 完整构造函数
		this.rkthId = rkthId;
		this.pzs = pzs;
		this.je = je;
		this.ysjl = ysjl;
		this.gysname = gysname;
		this.rtdate = rtdate;
		this.czy = czy;
		this.jsr = jsr;
		this.jsfs = jsfs;
	}

	// 使用Getters and Setters方法将进货退货主表类的私有属性封装起来
	public String getRkthId() {
		return this.rkthId;
	}

	public void setRkthId(String rkthId) {
		this.rkthId = rkthId;
	}

	public String getPzs() {
		return this.pzs;
	}

	public void setPzs(String pzs) {
		this.pzs = pzs;
	}

	public String getJe() {
		return this.je;
	}

	public void setJe(String je) {
		this.je = je;
	}

	public String getYsjl() {
		return this.ysjl;
	}

	public void setYsjl(String ysjl) {
		this.ysjl = ysjl;
	}

	public String getGysname() {
		return this.gysname;
	}

	public void setGysname(String gysname) {
		this.gysname = gysname;
	}

	public String getRtdate() {
		return this.rtdate;
	}

	public void setRtdate(String rtdate) {
		this.rtdate = rtdate;
	}

	public String getCzy() {
		return this.czy;
	}

	public void setCzy(String czy) {
		this.czy = czy;
	}

	public String getJsr() {
		return this.jsr;
	}

	public void setJsr(String jsr) {
		this.jsr = jsr;
	}

	public String getJsfs() {
		return this.jsfs;
	}

	public void setJsfs(String jsfs) {
		this.jsfs = jsfs;
	}

	public Set getTbRkthDetails() {
		return this.tbRkthDetails;
	}

	public void setTbRkthDetails(Set tbRkthDetails) {
		this.tbRkthDetails = tbRkthDetails;
	}
}

TbRukuDetail类

package com.lzw.dao.model;

public class TbRukuDetail implements java.io.Serializable {// 入库明细(实现序列化接口)

	private String id;// 流水号
	private String tbSpinfo;// 商品信息
	private String tbRukuMain;// 入库主表
	private Double dj;// 单价
	private Integer sl;// 数量

	public TbRukuDetail() {// 缺省构造函数
	}

	public TbRukuDetail(String tbSpinfo, String tbRukuMain, Double dj, Integer sl) {// 完整构造函数
		this.tbSpinfo = tbSpinfo;
		this.tbRukuMain = tbRukuMain;
		this.dj = dj;
		this.sl = sl;
	}

	// 使用Getters and Setters方法将入库明细类的私有属性封装起来
	public String getId() {
		return this.id;
	}

	public void setId(String lsh) {
		this.id = lsh;
	}

	public String getTabSpinfo() {
		return this.tbSpinfo;
	}

	public void setTabSpinfo(String tbSpinfo) {
		this.tbSpinfo = tbSpinfo;
	}

	public String getTabRukuMain() {
		return this.tbRukuMain;
	}

	public void setTabRukuMain(String tbRukuMain) {
		this.tbRukuMain = tbRukuMain;
	}

	public Double getDj() {
		return this.dj;
	}

	public void setDj(Double dj) {
		this.dj = dj;
	}

	public Integer getSl() {
		return this.sl;
	}

	public void setSl(Integer sl) {
		this.sl = sl;
	}

}

TbRukuMain类

package com.lzw.dao.model;

import java.util.HashSet;
import java.util.Set;

public class TbRukuMain implements java.io.Serializable {// 入库主表(实现序列化接口)

	private String rkId;// 入库编号
	private String pzs;// 品种数量
	private String je;// 总计金额
	private String ysjl;// 验收结论
	private String gysname;// 供应商名称
	private String rkdate;// 入库时间
	private String czy;// 操作员
	private String jsr;// 经手人
	private String jsfs;// 结算方式
	private Set<TbRukuDetail> tabRukuDetails = new HashSet<TbRukuDetail>(0);// 入库明细

	public TbRukuMain() {// 缺省构造函数
	}

	public TbRukuMain(String rkId, String pzs, String je, String ysjl, String gysname, String rkdate, String czy,
			String jsr, String jsfs) {// 完整构造函数
		this.rkId = rkId;
		this.pzs = pzs;
		this.je = je;
		this.ysjl = ysjl;
		this.gysname = gysname;
		this.rkdate = rkdate;
		this.czy = czy;
		this.jsr = jsr;
		this.jsfs = jsfs;
	}
	
	// 使用Getters and Setters方法将入库主表类的私有属性封装起来
	public String getRkId() {
		return this.rkId;
	}

	public void setRkId(String rkId) {
		this.rkId = rkId;
	}

	public String getPzs() {
		return this.pzs;
	}

	public void setPzs(String pzs) {
		this.pzs = pzs;
	}

	public String getJe() {
		return this.je;
	}

	public void setJe(String je) {
		this.je = je;
	}

	public String getYsjl() {
		return this.ysjl;
	}

	public void setYsjl(String sf) {
		this.ysjl = sf;
	}

	public String getGysname() {
		return this.gysname;
	}

	public void setGysname(String gysname) {
		this.gysname = gysname;
	}

	public String getRkdate() {
		return this.rkdate;
	}

	public void setRkdate(String rkdate) {
		this.rkdate = rkdate;
	}

	public String getCzy() {
		return this.czy;
	}

	public void setCzy(String czy) {
		this.czy = czy;
	}

	public String getJsr() {
		return this.jsr;
	}

	public void setJsr(String jsr) {
		this.jsr = jsr;
	}

	public String getJsfs() {
		return this.jsfs;
	}

	public void setJsfs(String jsfs) {
		this.jsfs = jsfs;
	}

	public Set<TbRukuDetail> getTabRukuDetails() {
		return this.tabRukuDetails;
	}

	public void setTabRukuDetails(Set<TbRukuDetail> tabRukuDetails) {
		this.tabRukuDetails = tabRukuDetails;
	}
}

TbSellDetail类

package com.lzw.dao.model;

public class TbSellDetail implements java.io.Serializable {// 销售明细(实现序列化接口)

	private Integer id;// 流水号
	private String tbSellMain;// 销售主表
	private String spid;// 商品编号
	private Double dj;// 销售单价
	private Integer sl;// 销售数量

	public TbSellDetail() {// 缺省构造函数
	}

	public TbSellDetail(String tbSellMain, String spid, Double dj, Integer sl) {// 完整构造函数
		this.tbSellMain = tbSellMain;
		this.spid = spid;
		this.dj = dj;
		this.sl = sl;
	}

	// 使用Getters and Setters方法将销售明细类的私有属性封装起来
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTbSellMain() {
		return this.tbSellMain;
	}

	public void setTbSellMain(String tbSellMain) {
		this.tbSellMain = tbSellMain;
	}

	public String getSpid() {
		return this.spid;
	}

	public void setSpid(String spid) {
		this.spid = spid;
	}

	public Double getDj() {
		return this.dj;
	}

	public void setDj(Double dj) {
		this.dj = dj;
	}

	public Integer getSl() {
		return this.sl;
	}

	public void setSl(Integer sl) {
		this.sl = sl;
	}
}

TbSellMain类

package com.lzw.dao.model;

import java.util.HashSet;
import java.util.Set;

public class TbSellMain implements java.io.Serializable {// 销售主表(实现序列化接口)

	private String sellId;// 销售编号
	private String pzs;// 销售品种数
	private String je;// 总计金额
	private String ysjl;// 验收结论
	private String khname;// 客户名称
	private String xsdate;// 销售日期
	private String czy;// 操作员
	private String jsr;// 经手人
	private String jsfs;// 结算方式
	private Set tbSellDetails = new HashSet(0);// 销售明细

	public TbSellMain() {// 缺省构造函数
	}

	public TbSellMain(String sellId, String pzs, String je, String ysjl, String khname, String xsdate, String czy,
			String jsr, String jsfs) {// 完整构造函数
		this.sellId = sellId;
		this.pzs = pzs;
		this.je = je;
		this.ysjl = ysjl;
		this.khname = khname;
		this.xsdate = xsdate;
		this.czy = czy;
		this.jsr = jsr;
		this.jsfs = jsfs;
		this.tbSellDetails = tbSellDetails;
	}

	// 使用Getters and Setters方法将销售主表类的私有属性封装起来
	public String getSellId() {
		return this.sellId;
	}

	public void setSellId(String sellId) {
		this.sellId = sellId;
	}

	public String getPzs() {
		return this.pzs;
	}

	public void setPzs(String pzs) {
		this.pzs = pzs;
	}

	public String getJe() {
		return this.je;
	}

	public void setJe(String je) {
		this.je = je;
	}

	public String getYsjl() {
		return this.ysjl;
	}

	public void setYsjl(String ysjl) {
		this.ysjl = ysjl;
	}

	public String getKhname() {
		return this.khname;
	}

	public void setKhname(String khname) {
		this.khname = khname;
	}

	public String getXsdate() {
		return this.xsdate;
	}

	public void setXsdate(String xsdate) {
		this.xsdate = xsdate;
	}

	public String getCzy() {
		return this.czy;
	}

	public void setCzy(String czy) {
		this.czy = czy;
	}

	public String getJsr() {
		return this.jsr;
	}

	public void setJsr(String jsr) {
		this.jsr = jsr;
	}

	public String getJsfs() {
		return this.jsfs;
	}

	public void setJsfs(String jsfs) {
		this.jsfs = jsfs;
	}

	public Set getTbSellDetails() {
		return this.tbSellDetails;
	}

	public void setTbSellDetails(Set tbSellDetails) {
		this.tbSellDetails = tbSellDetails;
	}
}

TbXsthDetail类

package com.lzw.dao.model;

public class TbXsthDetail implements java.io.Serializable {// 销售退货详细信息(实现序列化接口)

	private Integer id;// 销售退货编号
	private String tbXsthMain;// 销售退货主表
	private String spid;// 商品编号
	private Double dj;// 单价
	private Integer sl;// 数量

	public TbXsthDetail() {// 缺省构造函数
	}

	public TbXsthDetail(String tbXsthMain, String spid, Double dj, Integer sl) {// 完整构造函数
		this.tbXsthMain = tbXsthMain;
		this.spid = spid;
		this.dj = dj;
		this.sl = sl;
	}

	// 使用Getters and Setters方法将销售退货详细信息类的私有属性封装起来
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTbXsthMain() {
		return this.tbXsthMain;
	}

	public void setTbXsthMain(String tbXsthMain) {
		this.tbXsthMain = tbXsthMain;
	}

	public String getSpid() {
		return this.spid;
	}

	public void setSpid(String spid) {
		this.spid = spid;
	}

	public Double getDj() {
		return this.dj;
	}

	public void setDj(Double dj) {
		this.dj = dj;
	}

	public Integer getSl() {
		return this.sl;
	}

	public void setSl(Integer sl) {
		this.sl = sl;
	}
}

TbXsthMain类

package com.lzw.dao.model;

import java.util.HashSet;
import java.util.Set;

public class TbXsthMain implements java.io.Serializable {// 销售退货主表(实现序列化接口)

	private String xsthId;// 销售退货编号
	private String pzs;// 品种数量
	private String je;// 总计金额
	private String ysjl;// 验收结论
	private String khname;// 客户名称
	private String thdate;// 退货日期
	private String czy;// 操作员
	private String jsr;// 经手人
	private String jsfs;// 结算方式
	private Set tbXsthDetails = new HashSet(0);// 销售退货详细信息

	public TbXsthMain() {// 缺省构造函数
	}

	public TbXsthMain(String xsthId, String pzs, String je, String ysjl, String khname, String thdate, String czy,
			String jsr, String jsfs) {// 完整构造函数
		this.xsthId = xsthId;
		this.pzs = pzs;
		this.je = je;
		this.ysjl = ysjl;
		this.khname = khname;
		this.thdate = thdate;
		this.czy = czy;
		this.jsr = jsr;
		this.jsfs = jsfs;
	}

	// 使用Getters and Setters方法将销售退货主表类的私有属性封装起来
	public String getXsthId() {
		return this.xsthId;
	}

	public void setXsthId(String xsthId) {
		this.xsthId = xsthId;
	}

	public String getPzs() {
		return this.pzs;
	}

	public void setPzs(String pzs) {
		this.pzs = pzs;
	}

	public String getJe() {
		return this.je;
	}

	public void setJe(String je) {
		this.je = je;
	}

	public String getYsjl() {
		return this.ysjl;
	}

	public void setYsjl(String ysjl) {
		this.ysjl = ysjl;
	}

	public String getKhname() {
		return this.khname;
	}

	public void setKhname(String khname) {
		this.khname = khname;
	}

	public String getThdate() {
		return this.thdate;
	}

	public void setThdate(String thdate) {
		this.thdate = thdate;
	}

	public String getCzy() {
		return this.czy;
	}

	public void setCzy(String czy) {
		this.czy = czy;
	}

	public String getJsr() {
		return this.jsr;
	}

	public void setJsr(String jsr) {
		this.jsr = jsr;
	}

	public String getJsfs() {
		return this.jsfs;
	}

	public void setJsfs(String jsfs) {
		this.jsfs = jsfs;
	}

	public Set getTbXsthDetails() {
		return this.tbXsthDetails;
	}

	public void setTbXsthDetails(Set tbXsthDetails) {
		this.tbXsthDetails = tbXsthDetails;
	}
}

3.Dao公共类

Dao的全称是Data Access Object,即数据访问对象。本项目中应用该名称作为数据库访问类的名称,在该类中实现了数据库的驱动、连接、关闭和多个操作数据库的方法,这些方法包括不同数据表的操作方法。在介绍具体的数据库访问方法之前,先看一下Dao类的定义:

package com.lzw.dao;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import javax.swing.JOptionPane;

import com.lzw.Item;
import com.lzw.dao.model.TbGysinfo;
import com.lzw.dao.model.TbJsr;
import com.lzw.dao.model.TbKhinfo;
import com.lzw.dao.model.TbKucun;
import com.lzw.dao.model.TbRkthDetail;
import com.lzw.dao.model.TbRkthMain;
import com.lzw.dao.model.TbRukuDetail;
import com.lzw.dao.model.TbRukuMain;
import com.lzw.dao.model.TbSellDetail;
import com.lzw.dao.model.TbSellMain;
import com.lzw.dao.model.TbSpinfo;
import com.lzw.dao.model.TbXsthDetail;
import com.lzw.dao.model.TbXsthMain;

public class Dao {

	protected static String dbClassName = "com.mysql.jdbc.Driver";// MySQL数据库驱动类的名称
	protected static String dbUrl = "jdbc:mysql://127.0.0.1:3306/db_database28";// 访问MySQL数据库的路径
	protected static String dbUser = "root";// 访问MySQL数据库的用户名
	protected static String dbPwd = "123456";// 访问MySQL数据库的密码
	protected static String dbName = "db_database28";// 访问MySQL数据库中的实例(db_database28)
	protected static String second = null;//
	public static Connection conn = null;// MySQL数据库的连接对象

	static {// 静态初始化Dao类
		try {
			if (conn == null) {
				Class.forName(dbClassName).newInstance();// 实例化MySQL数据库的驱动
				conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);// 连接MySQL数据库
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "请将MySQL的JDBC驱动包复制到lib文件夹中。");// 捕获异常后,弹出提示框
			System.exit(-1);// 系统停止运行
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Dao() {
	}

	// 数据库备份
	public static String backup() throws SQLException {
		LinkedList<String> sqls = new LinkedList<String>();// 备份文件中的所有sql
		// 涉及的相关表命数组
		String tables[] = { "tb_gysinfo", "tb_jsr", "tb_khinfo", "tb_kucun",
				"tb_rkth_detail", "tb_rkth_main", "tb_ruku_detail",
				"tb_ruku_main", "tb_sell_detail", "tb_sell_main", "tb_spinfo",
				"tb_userlist", "tb_xsth_detail", "tb_xsth_main" };
		ArrayList<Tables> tableList = new ArrayList<Tables>();// 创建保存所有表对象的集合
		for (int i = 0; i < tables.length; i++) {// 遍历表名称数组
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("desc " + tables[i]);// 查询表结构
			ArrayList<Columns> columns = new ArrayList<Columns>();// 列集合
			while (rs.next()) {
				Columns c = new Columns();// 创建列对象
				c.setName(rs.getString("Field"));// 读取列名
				c.setType(rs.getString("Type"));// 读取列类型
				String isnull = rs.getString("Null");// 读取为空类型
				if ("YES".equals(isnull)) {// 如果列可以为空
					c.setNull(true);// 列可以为空
				}
				String key = rs.getString("Key");// 读取主键类型
				if ("PRI".equals(key)) {// 如果是主键
					c.setKey(true);// 列为主键
					String increment = rs.getString("Extra");// 读取特殊属性
					if ("auto_increment".equals(increment)) {// 表主键是否自增
						c.setIncrement(true);// 主键自增
					}
				}
				columns.add(c);// 列集合添加此列
			}
			Tables table = new Tables(tables[i], columns);// 创建表示此表命和拥有对应列对象的表对象
			tableList.add(table);// 表集合保存此表对象
			rs.close();// 关闭结果集
			stmt.close();// 关闭sql语句接口
		}

		for (int i = 0; i < tableList.size(); i++) {// 遍历表对象集合
			Tables table = tableList.get(i);// 获取表格对象

			String dropsql = "DROP TABLE IF EXISTS " + table.getName() + " ;";// 删除表sql
			sqls.add(dropsql);// 添加删除表sql

			StringBuilder createsql = new StringBuilder();// 创建表sql
			createsql.append("CREATE TABLE " + table.getName() + "( ");// 创建语句句头
			ArrayList<Columns> columns = table.getColumns();// 获取表中所有列对象
			for (int k = 0; k < columns.size(); k++) {// 遍历列集合
				Columns c = columns.get(k);// 获取列对象
				createsql.append(c.getName() + " " + c.getType());// 添加列名和类型声明语句
				if (!c.isNull()) {// 如果列可以为空
					createsql.append(" not null ");// 添加可以为空语句
				}
				if (c.isKey()) {// 如果是主键
					createsql.append(" primary key ");// 添加主键语句
					if (c.isIncrement()) {// 如果是主键自增
						createsql.append(" AUTO_INCREMENT ");// 添加自增语句
					}
				}
				if (k < columns.size() - 1) {// 如果不是最后一列
					createsql.append(",");// 添加逗号
				} else {// 如果是最后一列
					createsql.append(");");// 创建语句结尾
				}
			}
			sqls.add(createsql.toString());// 添加创建表sql

			Statement stmt = conn.createStatement();// 执行sql接口
			ResultSet rs = stmt
					.executeQuery("select * from " + table.getName());
			while (rs.next()) {
				StringBuilder insertsql = new StringBuilder();// 插入值sql
				insertsql.append("INSERT INTO " + table.getName() + " VALUES(");
				for (int j = 0; j < columns.size(); j++) {// 遍历表中所有列
					Columns c = columns.get(j);// 获取列对象
					String type = c.getType();// 获取列字段修饰符
					if (type.startsWith("varchar") || type.startsWith("char")
							|| type.startsWith("datetime")) {// 如果数据类型开头用varchar、char、datetime任意一种修饰
						insertsql.append("'" + rs.getString(c.getName()) + "'");// 获取本列数据,两端加逗号
					} else {
						insertsql.append(rs.getString(c.getName()));// 获取本列数据,两端不加逗号
					}
					if (j < columns.size() - 1) {// 如果不是最后一列
						insertsql.append(",");// 添加逗号
					} else {// 如果是最后一列
						insertsql.append(");");// 添加句尾
					}
				}
				sqls.add(insertsql.toString());// 添加插入数据sql
			}
			rs.close();// 关闭结果集
			stmt.close();// 关闭sql语句接口
		}
		sqls.add("DROP VIEW IF EXISTS v_rukuView;"); // 插入删除视图语句
		// 插入创建爱视图语句
		sqls.add("CREATE VIEW v_rukuView AS SELECT tb_ruku_main.rkID, tb_ruku_detail.spid, tb_spinfo.spname, tb_spinfo.gg, tb_ruku_detail.dj, tb_ruku_detail.sl,tb_ruku_detail.dj * tb_ruku_detail.sl AS je, tb_spinfo.gysname, tb_ruku_main.rkdate, tb_ruku_main.czy, tb_ruku_main.jsr,tb_ruku_main.jsfs FROM tb_ruku_detail INNER JOIN tb_ruku_main ON tb_ruku_detail.rkID = tb_ruku_main.rkID INNER JOIN tb_spinfo ON tb_ruku_detail.spid = tb_spinfo.id;");
		sqls.add("DROP VIEW IF EXISTS v_sellView;");// 插入删除视图语句
		// 插入创建爱视图语句
		sqls.add("CREATE VIEW v_sellView AS SELECT tb_sell_main.sellID, tb_spinfo.spname, tb_sell_detail.spid, tb_spinfo.gg, tb_sell_detail.dj, tb_sell_detail.sl,tb_sell_detail.sl * tb_sell_detail.dj AS je, tb_sell_main.khname, tb_sell_main.xsdate, tb_sell_main.czy, tb_sell_main.jsr,tb_sell_main.jsfs FROM tb_sell_detail INNER JOIN tb_sell_main ON tb_sell_detail.sellID = tb_sell_main.sellID INNER JOIN tb_spinfo ON tb_sell_detail.spid = tb_spinfo.id;");

		java.util.Date date = new java.util.Date();// 通过Date对象获得当前时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");// 设置当前时间的输出格式
		String backupTime = sdf.format(date);// 格式化Date对象
		String filePath = "backup\\" + backupTime + ".sql";// 通过拼接字符串获得备份文件的存放路径

		File sqlFile = new File(filePath);// 创建备份文件对象
		FileOutputStream fos = null;// 文件字节输出流
		OutputStreamWriter osw = null;// 字节流转为字符流
		BufferedWriter rw = null;// 缓冲字符流
		try {
			fos = new FileOutputStream(sqlFile);
			osw = new OutputStreamWriter(fos);
			rw = new BufferedWriter(osw);
			for (String tmp : sqls) {// 遍历所有备份sql
				rw.write(tmp);// 向文件中写入sql
				rw.newLine();// 文件换行
				rw.flush();// 字符流刷新
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 倒序依次关闭所有IO流
			if (rw != null) {
				try {
					rw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (osw != null) {
				try {
					osw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return filePath;
	}

	// 数据库恢复
	public static void restore(String filePath) {
		File sqlFile = new File(filePath);// 创建备份文件对象
		Statement stmt = null;// sql语句直接接口
		FileInputStream fis = null;// 文件输入字节流
		InputStreamReader isr = null;// 字节流转为字符流
		BufferedReader br = null;// 缓存输入字符流
		try {
			fis = new FileInputStream(sqlFile);
			isr = new InputStreamReader(fis);
			br = new BufferedReader(isr);
			String readStr = null;// 缓冲字符串,保存备份文件中一行的内容
			while ((readStr = br.readLine()) != null) {// 逐行读取备份文件中的内容
				if (!"".equals(readStr.trim())) {// 如果读取的内容不为空
					stmt = conn.createStatement();// 创建sql语句直接接口
					int count = stmt.executeUpdate(readStr);// 执行sql语句
					stmt.close();// 关闭接口
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 倒序依次关闭所有IO流
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (isr != null) {
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	// 读取所有客户信息
	public static List getKhInfos() {
		List list = findForList("select id,khname from tb_khinfo");
		return list;
	}

	// 读取所有供应商信息
	public static List getGysInfos() {
		List list = findForList("select id,name from tb_gysinfo");
		return list;
	}

	// 读取客户信息
	public static TbKhinfo getKhInfo(Item item) {
		String where = "khname='" + item.getName() + "'";
		if (item.getId() != null)
			where = "id='" + item.getId() + "'";
		TbKhinfo info = new TbKhinfo();
		ResultSet set = findForResultSet("select * from tb_khinfo where "
				+ where);
		try {
			if (set.next()) {
				info.setId(set.getString("id").trim());
				info.setKhname(set.getString("khname").trim());
				info.setJian(set.getString("jian").trim());
				info.setAddress(set.getString("address").trim());
				info.setBianma(set.getString("bianma").trim());
				info.setFax(set.getString("fax").trim());
				info.setHao(set.getString("hao").trim());
				info.setLian(set.getString("lian").trim());
				info.setLtel(set.getString("ltel").trim());
				info.setMail(set.getString("mail").trim());
				info.setTel(set.getString("tel").trim());
				info.setXinhang(set.getString("xinhang").trim());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return info;
	}

	// 读取指定供应商信息
	public static TbGysinfo getGysInfo(Item item) {
		String where = "name='" + item.getName() + "'";
		if (item.getId() != null)
			where = "id='" + item.getId() + "'";
		TbGysinfo info = new TbGysinfo();
		ResultSet set = findForResultSet("select * from tb_gysinfo where "
				+ where);
		try {
			if (set.next()) {
				info.setId(set.getString("id").trim());
				info.setAddress(set.getString("address").trim());
				info.setBianma(set.getString("bianma").trim());
				info.setFax(set.getString("fax").trim());
				info.setJc(set.getString("jc").trim());
				info.setLian(set.getString("lian").trim());
				info.setLtel(set.getString("ltel").trim());
				info.setMail(set.getString("mail").trim());
				info.setName(set.getString("name").trim());
				info.setTel(set.getString("tel").trim());
				info.setYh(set.getString("yh").trim());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return info;
	}

	// 读取经手人
	public static TbJsr getJsr(String name, String password) {
		TbJsr user = new TbJsr();
		ResultSet rs = findForResultSet("select * from tb_jsr where name='"
				+ name + "'");
		try {
			if (rs.next()) {
				user.setSex(name);
				user.setAge(rs.getString("pass"));
				if (user.getAge().equals(password)) {
					user.setName(rs.getString("name"));
					user.setTel(rs.getString("quan"));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	// 执行指定查询
	public static ResultSet query(String QueryStr) {
		ResultSet set = findForResultSet(QueryStr);
		return set;
	}

	// 执行删除
	public static int delete(String sql) {
		return update(sql);
	}

	// 添加客户信息的方法
	public static boolean addKeHu(TbKhinfo khinfo) {
		if (khinfo == null)
			return false;
		return insert("insert tb_khinfo values('" + khinfo.getId() + "','"
				+ khinfo.getKhname() + "','" + khinfo.getJian() + "','"
				+ khinfo.getAddress() + "','" + khinfo.getBianma() + "','"
				+ khinfo.getTel() + "','" + khinfo.getFax() + "','"
				+ khinfo.getLian() + "','" + khinfo.getLtel() + "','"
				+ khinfo.getMail() + "','" + khinfo.getXinhang() + "','"
				+ khinfo.getHao() + "')");
	}

	// 修改客户信息的方法
	public static int updateKeHu(TbKhinfo khinfo) {
		return update("update tb_khinfo set jian='" + khinfo.getJian()
				+ "',address='" + khinfo.getAddress() + "',bianma='"
				+ khinfo.getBianma() + "',tel='" + khinfo.getTel() + "',fax='"
				+ khinfo.getFax() + "',lian='" + khinfo.getLian() + "',ltel='"
				+ khinfo.getLtel() + "',mail='" + khinfo.getMail()
				+ "',xinhang='" + khinfo.getXinhang() + "',hao='"
				+ khinfo.getHao() + "' where id='" + khinfo.getId() + "'");
	}

	// 修改库存的方法
	public static int updateKucunDj(TbKucun kcInfo) {
		return update("update tb_kucun set dj=" + kcInfo.getDj()
				+ " where id='" + kcInfo.getId() + "'");
	}

	// 修改供应商信息的方法
	public static int updateGys(TbGysinfo gysInfo) {
		return update("update tb_gysinfo set jc='" + gysInfo.getJc()
				+ "',address='" + gysInfo.getAddress() + "',bianma='"
				+ gysInfo.getBianma() + "',tel='" + gysInfo.getTel()
				+ "',fax='" + gysInfo.getFax() + "',lian='" + gysInfo.getLian()
				+ "',ltel='" + gysInfo.getLtel() + "',mail='"
				+ gysInfo.getMail() + "',yh='" + gysInfo.getYh()
				+ "' where id='" + gysInfo.getId() + "'");
	}

	// 添加供应商信息的方法
	public static boolean addGys(TbGysinfo gysInfo) {
		if (gysInfo == null)
			return false;
		return insert("insert tb_gysinfo values('" + gysInfo.getId() + "','"
				+ gysInfo.getName() + "','" + gysInfo.getJc() + "','"
				+ gysInfo.getAddress() + "','" + gysInfo.getBianma() + "','"
				+ gysInfo.getTel() + "','" + gysInfo.getFax() + "','"
				+ gysInfo.getLian() + "','" + gysInfo.getLtel() + "','"
				+ gysInfo.getMail() + "','" + gysInfo.getYh() + "')");
	}

	// 添加商品
	public static boolean addSp(TbSpinfo spInfo) {
		if (spInfo == null)
			return false;
		return insert("insert tb_spinfo values('" + spInfo.getId() + "','"
				+ spInfo.getSpname() + "','" + spInfo.getJc() + "','"
				+ spInfo.getCd() + "','" + spInfo.getDw() + "','"
				+ spInfo.getGg() + "','" + spInfo.getBz() + "','"
				+ spInfo.getPh() + "','" + spInfo.getPzwh() + "','"
				+ spInfo.getMemo() + "','" + spInfo.getGysname() + "')");
	}

	// 更新商品
	public static int updateSp(TbSpinfo spInfo) {
		return update("update tb_spinfo set jc='" + spInfo.getJc() + "',cd='"
				+ spInfo.getCd() + "',dw='" + spInfo.getDw() + "',gg='"
				+ spInfo.getGg() + "',bz='" + spInfo.getBz() + "',ph='"
				+ spInfo.getPh() + "',pzwh='" + spInfo.getPzwh() + "',memo='"
				+ spInfo.getMemo() + "',gysname='" + spInfo.getGysname()
				+ "' where id='" + spInfo.getId() + "'");
	}

	// 读取商品信息
	public static TbSpinfo getSpInfo(Item item) {
		String where = "spname='" + item.getName() + "'";
		if (item.getId() != null)
			where = "id='" + item.getId() + "'";
		ResultSet rs = findForResultSet("select * from tb_spinfo where "
				+ where);
		TbSpinfo spInfo = new TbSpinfo();
		try {
			if (rs.next()) {
				spInfo.setId(rs.getString("id").trim());
				spInfo.setBz(rs.getString("bz").trim());
				spInfo.setCd(rs.getString("cd").trim());
				spInfo.setDw(rs.getString("dw").trim());
				spInfo.setGg(rs.getString("gg").trim());
				spInfo.setGysname(rs.getString("gysname").trim());
				spInfo.setJc(rs.getString("jc").trim());
				spInfo.setMemo(rs.getString("memo").trim());
				spInfo.setPh(rs.getString("ph").trim());
				spInfo.setPzwh(rs.getString("pzwh").trim());
				spInfo.setSpname(rs.getString("spname").trim());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return spInfo;
	}

	// 获取所有商品信息
	public static List getSpInfos() {
		List list = findForList("select * from tb_spinfo");
		return list;
	}

	// 获取库存商品信息
	public static TbKucun getKucun(Item item) {
		String where = "spname='" + item.getName() + "'";
		if (item.getId() != null)
			where = "id='" + item.getId() + "'";
		ResultSet rs = findForResultSet("select * from tb_kucun where " + where);
		TbKucun kucun = new TbKucun();
		try {
			if (rs.next()) {
				kucun.setId(rs.getString("id"));
				kucun.setSpname(rs.getString("spname"));
				kucun.setJc(rs.getString("jc"));
				kucun.setBz(rs.getString("bz"));
				kucun.setCd(rs.getString("cd"));
				kucun.setDj(rs.getDouble("dj"));
				kucun.setDw(rs.getString("dw"));
				kucun.setGg(rs.getString("gg"));
				kucun.setKcsl(rs.getInt("kcsl"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return kucun;
	}

	// 获取入库单的最大ID,即最大入库票号
	public static String getRuKuMainMaxId(Date date) {
		return getMainTypeTableMaxId(date, "tb_ruku_main", "RK", "rkid");
	}

	// 在事务中添加入库信息
	public static boolean insertRukuInfo(TbRukuMain ruMain) {
		try {
			boolean autoCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);
			// 添加入库主表记录
			insert("insert into tb_ruku_main values('" + ruMain.getRkId()
					+ "','" + ruMain.getPzs() + "'," + ruMain.getJe() + ",'"
					+ ruMain.getYsjl() + "','" + ruMain.getGysname() + "','"
					+ ruMain.getRkdate() + "','" + ruMain.getCzy() + "','"
					+ ruMain.getJsr() + "','" + ruMain.getJsfs() + "')");
			Set<TbRukuDetail> rkDetails = ruMain.getTabRukuDetails();
			for (Iterator<TbRukuDetail> iter = rkDetails.iterator(); iter
					.hasNext();) {
				TbRukuDetail details = iter.next();
				// 添加入库详细表记录
				insert("insert into tb_ruku_detail values('" + ruMain.getRkId()
						+ "','" + details.getTabSpinfo() + "',"
						+ details.getDj() + "," + details.getSl() + ")");
				// 添加或修改库存表记录
				Item item = new Item();
				item.setId(details.getTabSpinfo());
				TbSpinfo spInfo = getSpInfo(item);
				if (spInfo.getId() != null && !spInfo.getId().isEmpty()) {
					TbKucun kucun = getKucun(item);
					if (kucun.getId() == null || kucun.getId().isEmpty()) {
						insert("insert into tb_kucun values('" + spInfo.getId()
								+ "','" + spInfo.getSpname() + "','"
								+ spInfo.getJc() + "','" + spInfo.getCd()
								+ "','" + spInfo.getGg() + "','"
								+ spInfo.getBz() + "','" + spInfo.getDw()
								+ "'," + details.getDj() + ","
								+ details.getSl() + ")");
					} else {
						int sl = kucun.getKcsl() + details.getSl();
						update("update tb_kucun set kcsl=" + sl + ",dj="
								+ details.getDj() + " where id='"
								+ kucun.getId() + "'");
					}
				}
			}
			conn.commit();
			conn.setAutoCommit(autoCommit);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public static ResultSet findForResultSet(String sql) {
		if (conn == null)
			return null;
		long time = System.currentTimeMillis();
		ResultSet rs = null;
		try {
			Statement stmt = null;
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			rs = stmt.executeQuery(sql);
			second = ((System.currentTimeMillis() - time) / 1000d) + "";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}

	// 添加数据
	public static boolean insert(String sql) {
		boolean result = false;
		try {
			Statement stmt = conn.createStatement();
			result = stmt.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	// 更新数据
	public static int update(String sql) {
		int result = 0;
		try {
			Statement stmt = conn.createStatement();
			result = stmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static List findForList(String sql) {
		List<List> list = new ArrayList<List>();
		ResultSet rs = findForResultSet(sql);
		try {
			ResultSetMetaData metaData = rs.getMetaData();
			int colCount = metaData.getColumnCount();
			while (rs.next()) {
				List<String> row = new ArrayList<String>();
				for (int i = 1; i <= colCount; i++) {
					String str = rs.getString(i);
					if (str != null && !str.isEmpty())
						str = str.trim();
					row.add(str);
				}
				list.add(row);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	// 获取退货最大ID
	public static String getRkthMainMaxId(Date date) {
		return getMainTypeTableMaxId(date, "tb_rkth_main", "RT", "rkthId");
	}

	// 在事务中添加入库退货信息
	public static boolean insertRkthInfo(TbRkthMain rkthMain) {
		try {
			boolean autoCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);
			// 添加入库退货主表记录
			insert("insert into tb_rkth_main values('" + rkthMain.getRkthId()
					+ "','" + rkthMain.getPzs() + "'," + rkthMain.getJe()
					+ ",'" + rkthMain.getYsjl() + "','" + rkthMain.getGysname()
					+ "','" + rkthMain.getRtdate() + "','" + rkthMain.getCzy()
					+ "','" + rkthMain.getJsr() + "','" + rkthMain.getJsfs()
					+ "')");
			Set<TbRkthDetail> rkDetails = rkthMain.getTbRkthDetails();
			for (Iterator<TbRkthDetail> iter = rkDetails.iterator(); iter
					.hasNext();) {
				TbRkthDetail details = iter.next();
				// 添加入库详细表记录
				insert("insert into tb_rkth_detail values('"
						+ rkthMain.getRkthId() + "','" + details.getSpid()
						+ "'," + details.getDj() + "," + details.getSl() + ")");
				// 添加或修改库存表记录
				Item item = new Item();
				item.setId(details.getSpid());
				TbSpinfo spInfo = getSpInfo(item);
				if (spInfo.getId() != null && !spInfo.getId().isEmpty()) {
					TbKucun kucun = getKucun(item);
					if (kucun.getId() != null && !kucun.getId().isEmpty()) {
						int sl = kucun.getKcsl() - details.getSl();
						update("update tb_kucun set kcsl=" + sl + " where id='"
								+ kucun.getId() + "'");
					}
				}
			}
			conn.commit();
			conn.setAutoCommit(autoCommit);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	// 获取销售主表最大ID
	public static String getSellMainMaxId(Date date) {
		return getMainTypeTableMaxId(date, "tb_sell_main", "XS", "sellID");
	}

	// 在事务中添加销售信息
	public static boolean insertSellInfo(TbSellMain sellMain) {
		try {
			boolean autoCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);
			// 添加销售主表记录
			insert("insert into tb_sell_main values('" + sellMain.getSellId()
					+ "','" + sellMain.getPzs() + "'," + sellMain.getJe()
					+ ",'" + sellMain.getYsjl() + "','" + sellMain.getKhname()
					+ "','" + sellMain.getXsdate() + "','" + sellMain.getCzy()
					+ "','" + sellMain.getJsr() + "','" + sellMain.getJsfs()
					+ "')");
			Set<TbSellDetail> rkDetails = sellMain.getTbSellDetails();
			for (Iterator<TbSellDetail> iter = rkDetails.iterator(); iter
					.hasNext();) {
				TbSellDetail details = iter.next();
				// 添加销售详细表记录
				insert("insert into tb_sell_detail values('"
						+ sellMain.getSellId() + "','" + details.getSpid()
						+ "'," + details.getDj() + "," + details.getSl() + ")");
				// 修改库存表记录
				Item item = new Item();
				item.setId(details.getSpid());
				TbSpinfo spInfo = getSpInfo(item);
				if (spInfo.getId() != null && !spInfo.getId().isEmpty()) {
					TbKucun kucun = getKucun(item);
					if (kucun.getId() != null && !kucun.getId().isEmpty()) {
						int sl = kucun.getKcsl() - details.getSl();
						update("update tb_kucun set kcsl=" + sl + " where id='"
								+ kucun.getId() + "'");
					}
				}
			}
			conn.commit();
			conn.setAutoCommit(autoCommit);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	// 获取更类主表最大ID
	private static String getMainTypeTableMaxId(Date date, String table,
			String idChar, String idName) {
		String dateStr = date.toString().replace("-", "");
		String id = idChar + dateStr;
		String sql = "select max(" + idName + ") from " + table + " where "
				+ idName + " like '" + id + "%'";
		ResultSet set = query(sql);
		String baseId = null;
		try {
			if (set.next())
				baseId = set.getString(1);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		baseId = baseId == null ? "000" : baseId.substring(baseId.length() - 3);
		int idNum = Integer.parseInt(baseId) + 1;
		id += String.format("%03d", idNum);
		return id;
	}

	// 获得最大的销售退货编号
	public static String getXsthMainMaxId(Date date) {
		return getMainTypeTableMaxId(date, "tb_xsth_main", "XT", "xsthID");
	}

	// 获得库存信息
	public static List getKucunInfos() {
		List list = findForList("select id,spname,dj,kcsl from tb_kucun");
		return list;
	}

	// 在事务中添加销售退货信息
	public static boolean insertXsthInfo(TbXsthMain xsthMain) {
		try {
			boolean autoCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);
			// 添加销售退货主表记录
			insert("insert into tb_xsth_main values('" + xsthMain.getXsthId()
					+ "','" + xsthMain.getPzs() + "'," + xsthMain.getJe()
					+ ",'" + xsthMain.getYsjl() + "','" + xsthMain.getKhname()
					+ "','" + xsthMain.getThdate() + "','" + xsthMain.getCzy()
					+ "','" + xsthMain.getJsr() + "','" + xsthMain.getJsfs()
					+ "')");
			Set<TbXsthDetail> xsthDetails = xsthMain.getTbXsthDetails();
			for (Iterator<TbXsthDetail> iter = xsthDetails.iterator(); iter
					.hasNext();) {
				TbXsthDetail details = iter.next();
				// 添加销售退货详细表记录
				insert("insert into tb_xsth_detail values('"
						+ xsthMain.getXsthId() + "','" + details.getSpid()
						+ "'," + details.getDj() + "," + details.getSl() + ")");
				// 修改库存表记录
				Item item = new Item();
				item.setId(details.getSpid());
				TbSpinfo spInfo = getSpInfo(item);
				if (spInfo.getId() != null && !spInfo.getId().isEmpty()) {
					TbKucun kucun = getKucun(item);
					if (kucun.getId() != null && !kucun.getId().isEmpty()) {
						int sl = kucun.getKcsl() + details.getSl();
						update("update tb_kucun set kcsl=" + sl + " where id='"
								+ kucun.getId() + "'");
					}
				}
			}
			conn.commit();
			conn.setAutoCommit(autoCommit);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return true;
	}

	// 添加用户
	public static int addJsr(TbJsr jsr) {
		String sql = "insert tb_jsr values('" + jsr.getName() + "','"
				+ jsr.getSex() + "','" + jsr.getAge() + "','" + jsr.getTel()
				+ "',1)";
		System.out.println(sql);
		return update(sql);
	}

	public static List getJsrs() {
		List list = findForList("select * from tb_jsr where enable=1");
		return list;
	}

	// 修改用户方法
	public static int modifyPassword(String oldPass, String pass) {
		return update("update tb_userlist set pass='" + pass + "' where pass='"
				+ oldPass + "'");
	}

	// 获取用户对象的方法
	public static TbJsr getUser(Item item) {
		String where = "username='" + item.getName() + "'";
		if (item.getId() != null)
			where = "name='" + item.getId() + "'";
		ResultSet rs = findForResultSet("select * from tb_userlist where "
				+ where);
		TbJsr user = new TbJsr();
		try {
			if (rs.next()) {
				user.setName(rs.getString("name").trim());
				user.setSex(rs.getString("username").trim());
				user.setAge(rs.getString("pass").trim());
				user.setTel(rs.getString("quan").trim());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	// 验证登录
	public static boolean checkLogin(String userStr, String passStr)
			throws SQLException {
		ResultSet rs = findForResultSet("select * from tb_userlist where name='"
				+ userStr + "' and pass='" + passStr + "'");
		if (rs == null)
			return false;
		return rs.next();
	}

}

(1)getKhInfo(Item item)方法
该方法用于获取客户信息,方法的返回值是TbKhinfo类的对象,即客户信息的数据模型。方法将接收一个Item类的实例对象,通过该对象获取客户的ID编号,然后从数据库中获取该ID编号的数据信息,并封装到客户信息的数据模型中,这个数据模型最后会作为方法的返回值,返回给方法的调用者。

public static TbKhinfo getKhInfo(Item item) {
		String where = "khname='" + item.getName() + "'";
		if (item.getId() != null)
			where = "id='" + item.getId() + "'";
		TbKhinfo info = new TbKhinfo();
		ResultSet set = findForResultSet("select * from tb_khinfo where "
				+ where);
		try {
			if (set.next()) {
				info.setId(set.getString("id").trim());
				info.setKhname(set.getString("khname").trim());
				info.setJian(set.getString("jian").trim());
				info.setAddress(set.getString("address").trim());
				info.setBianma(set.getString("bianma").trim());
				info.setFax(set.getString("fax").trim());
				info.setHao(set.getString("hao").trim());
				info.setLian(set.getString("lian").trim());
				info.setLtel(set.getString("ltel").trim());
				info.setMail(set.getString("mail").trim());
				info.setTel(set.getString("tel").trim());
				info.setXinhang(set.getString("xinhang").trim());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return info;
	}

注意:Item就是之前所讲到的公共类,它用于封装id和name属性。
(2)getGysInfo(Item item)方法
该方法用于获取供应商信息,方法的返回值是TbGysinfo类的对象,即供应商数据表的模型对象。方法将接收Item类的对象作为参数,从Item对象中获取供应商的编号和名称,然后从数据库中获取该编号的供应商信息,并封装到供应商数据模型对象中,最后该模型对象会作为方法的返回值返回给方法的调用者。

// 读取指定供应商信息
	public static TbGysinfo getGysInfo(Item item) {
		String where = "name='" + item.getName() + "'";
		if (item.getId() != null)
			where = "id='" + item.getId() + "'";
		TbGysinfo info = new TbGysinfo();
		ResultSet set = findForResultSet("select * from tb_gysinfo where "
				+ where);
		try {
			if (set.next()) {
				info.setId(set.getString("id").trim());
				info.setAddress(set.getString("address").trim());
				info.setBianma(set.getString("bianma").trim());
				info.setFax(set.getString("fax").trim());
				info.setJc(set.getString("jc").trim());
				info.setLian(set.getString("lian").trim());
				info.setLtel(set.getString("ltel").trim());
				info.setMail(set.getString("mail").trim());
				info.setName(set.getString("name").trim());
				info.setTel(set.getString("tel").trim());
				info.setYh(set.getString("yh").trim());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return info;
	}

(3)getSpInfo(Item item)方法
该方法用于获取商品信息,方法的返回值是TbSpinfo类的对象,即商品数据表的模型对象。方法的参数是Item公共类的对象item。该对象封装了商品的id和name属性,通过这两个属性可以从数据库中获取该编号的商品信息。该方法将获取到的商品信息封装为商品数据模型,返回给方法的调用者。

// 读取商品信息
	public static TbSpinfo getSpInfo(Item item) {
		String where = "spname='" + item.getName() + "'";
		if (item.getId() != null)
			where = "id='" + item.getId() + "'";
		ResultSet rs = findForResultSet("select * from tb_spinfo where "
				+ where);
		TbSpinfo spInfo = new TbSpinfo();
		try {
			if (rs.next()) {
				spInfo.setId(rs.getString("id").trim());
				spInfo.setBz(rs.getString("bz").trim());
				spInfo.setCd(rs.getString("cd").trim());
				spInfo.setDw(rs.getString("dw").trim());
				spInfo.setGg(rs.getString("gg").trim());
				spInfo.setGysname(rs.getString("gysname").trim());
				spInfo.setJc(rs.getString("jc").trim());
				spInfo.setMemo(rs.getString("memo").trim());
				spInfo.setPh(rs.getString("ph").trim());
				spInfo.setPzwh(rs.getString("pzwh").trim());
				spInfo.setSpname(rs.getString("spname").trim());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return spInfo;
	}

(4)getLogin(String name, String password)方法
该方法用于判断登录用户的用户名与密码是否正确,方法的返回值是boolean类型,接收的参数有name和password,分别是用户名与密码信息。该方法将在一条SQL语句中获取指定用户名和密码的数据。如果用户名和密码正确,则返回true值,否则返回false值。代码如下:

public static boolean getLogin(String name, String password)
			throws SQLException {
		ResultSet rs = findForResultSet("select * from tb_userlist where name='"
				+ name + "' and pass='" + password + "'");//执行SQL查询
		return rs.next();
	}

(5)insertSellInfo(TbSellMain sellMain)方法
该方法用于添加销售信息到数据库中,它将在事务中完成对销售主表、销售明细表和库存表的添加与保存操作。基于事务的安全原则,如果对任何一个数据表的操作失败,将导致整个事务回滚,恢复到之前的数据状态。因此,该方法执行前后可以保证数据库的完整性不被破坏,同时完成对销售信息的添加。在JDBC中使用事务的关键是调用Connection类的setAutoCommit()方法设置自动提交模式为false,完成业务后,再调用commit()方法手动提交事务。代码如下:

// 在事务中添加销售信息
	public static boolean insertSellInfo(TbSellMain sellMain) {
		try {
			boolean autoCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);
			// 添加销售主表记录
			insert("insert into tb_sell_main values('" + sellMain.getSellId()
					+ "','" + sellMain.getPzs() + "'," + sellMain.getJe()
					+ ",'" + sellMain.getYsjl() + "','" + sellMain.getKhname()
					+ "','" + sellMain.getXsdate() + "','" + sellMain.getCzy()
					+ "','" + sellMain.getJsr() + "','" + sellMain.getJsfs()
					+ "')");
			Set<TbSellDetail> rkDetails = sellMain.getTbSellDetails();
			for (Iterator<TbSellDetail> iter = rkDetails.iterator(); iter
					.hasNext();) {
				TbSellDetail details = iter.next();
				// 添加销售详细表记录
				insert("insert into tb_sell_detail values('"
						+ sellMain.getSellId() + "','" + details.getSpid()
						+ "'," + details.getDj() + "," + details.getSl() + ")");
				// 修改库存表记录
				Item item = new Item();
				item.setId(details.getSpid());
				TbSpinfo spInfo = getSpInfo(item);
				if (spInfo.getId() != null && !spInfo.getId().isEmpty()) {
					TbKucun kucun = getKucun(item);
					if (kucun.getId() != null && !kucun.getId().isEmpty()) {
						int sl = kucun.getKcsl() - details.getSl();
						update("update tb_kucun set kcsl=" + sl + " where id='"
								+ kucun.getId() + "'");
					}
				}
			}
			conn.commit();
			conn.setAutoCommit(autoCommit);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

注意:在开始JDBC事务之前最好使用getAutoCommit()方法获取原有的事务提交模式并保存,在执行commit()方法提交事务之后,要使用setAutoCommit()方法恢复原有的提交模式。
(6)backup()方法
该方法用于执行数据库的备份操作,这里主要通过读取MySQL数据库中的表信息和表数据,自动生成可执行的sql脚本(每行均为一个独立sql语句)。代码如下:

// 数据库备份
	public static String backup() throws SQLException {
		LinkedList<String> sqls = new LinkedList<String>();// 备份文件中的所有sql
		// 涉及的相关表命数组
		String tables[] = { "tb_gysinfo", "tb_jsr", "tb_khinfo", "tb_kucun",
				"tb_rkth_detail", "tb_rkth_main", "tb_ruku_detail",
				"tb_ruku_main", "tb_sell_detail", "tb_sell_main", "tb_spinfo",
				"tb_userlist", "tb_xsth_detail", "tb_xsth_main" };
		ArrayList<Tables> tableList = new ArrayList<Tables>();// 创建保存所有表对象的集合
		for (int i = 0; i < tables.length; i++) {// 遍历表名称数组
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("desc " + tables[i]);// 查询表结构
			ArrayList<Columns> columns = new ArrayList<Columns>();// 列集合
			while (rs.next()) {
				Columns c = new Columns();// 创建列对象
				c.setName(rs.getString("Field"));// 读取列名
				c.setType(rs.getString("Type"));// 读取列类型
				String isnull = rs.getString("Null");// 读取为空类型
				if ("YES".equals(isnull)) {// 如果列可以为空
					c.setNull(true);// 列可以为空
				}
				String key = rs.getString("Key");// 读取主键类型
				if ("PRI".equals(key)) {// 如果是主键
					c.setKey(true);// 列为主键
					String increment = rs.getString("Extra");// 读取特殊属性
					if ("auto_increment".equals(increment)) {// 表主键是否自增
						c.setIncrement(true);// 主键自增
					}
				}
				columns.add(c);// 列集合添加此列
			}
			Tables table = new Tables(tables[i], columns);// 创建表示此表命和拥有对应列对象的表对象
			tableList.add(table);// 表集合保存此表对象
			rs.close();// 关闭结果集
			stmt.close();// 关闭sql语句接口
		}

		for (int i = 0; i < tableList.size(); i++) {// 遍历表对象集合
			Tables table = tableList.get(i);// 获取表格对象

			String dropsql = "DROP TABLE IF EXISTS " + table.getName() + " ;";// 删除表sql
			sqls.add(dropsql);// 添加删除表sql

			StringBuilder createsql = new StringBuilder();// 创建表sql
			createsql.append("CREATE TABLE " + table.getName() + "( ");// 创建语句句头
			ArrayList<Columns> columns = table.getColumns();// 获取表中所有列对象
			for (int k = 0; k < columns.size(); k++) {// 遍历列集合
				Columns c = columns.get(k);// 获取列对象
				createsql.append(c.getName() + " " + c.getType());// 添加列名和类型声明语句
				if (!c.isNull()) {// 如果列可以为空
					createsql.append(" not null ");// 添加可以为空语句
				}
				if (c.isKey()) {// 如果是主键
					createsql.append(" primary key ");// 添加主键语句
					if (c.isIncrement()) {// 如果是主键自增
						createsql.append(" AUTO_INCREMENT ");// 添加自增语句
					}
				}
				if (k < columns.size() - 1) {// 如果不是最后一列
					createsql.append(",");// 添加逗号
				} else {// 如果是最后一列
					createsql.append(");");// 创建语句结尾
				}
			}
			sqls.add(createsql.toString());// 添加创建表sql

			Statement stmt = conn.createStatement();// 执行sql接口
			ResultSet rs = stmt
					.executeQuery("select * from " + table.getName());
			while (rs.next()) {
				StringBuilder insertsql = new StringBuilder();// 插入值sql
				insertsql.append("INSERT INTO " + table.getName() + " VALUES(");
				for (int j = 0; j < columns.size(); j++) {// 遍历表中所有列
					Columns c = columns.get(j);// 获取列对象
					String type = c.getType();// 获取列字段修饰符
					if (type.startsWith("varchar") || type.startsWith("char")
							|| type.startsWith("datetime")) {// 如果数据类型开头用varchar、char、datetime任意一种修饰
						insertsql.append("'" + rs.getString(c.getName()) + "'");// 获取本列数据,两端加逗号
					} else {
						insertsql.append(rs.getString(c.getName()));// 获取本列数据,两端不加逗号
					}
					if (j < columns.size() - 1) {// 如果不是最后一列
						insertsql.append(",");// 添加逗号
					} else {// 如果是最后一列
						insertsql.append(");");// 添加句尾
					}
				}
				sqls.add(insertsql.toString());// 添加插入数据sql
			}
			rs.close();// 关闭结果集
			stmt.close();// 关闭sql语句接口
		}
		sqls.add("DROP VIEW IF EXISTS v_rukuView;"); // 插入删除视图语句
		// 插入创建爱视图语句
		sqls.add("CREATE VIEW v_rukuView AS SELECT tb_ruku_main.rkID, tb_ruku_detail.spid, tb_spinfo.spname, tb_spinfo.gg, tb_ruku_detail.dj, tb_ruku_detail.sl,tb_ruku_detail.dj * tb_ruku_detail.sl AS je, tb_spinfo.gysname, tb_ruku_main.rkdate, tb_ruku_main.czy, tb_ruku_main.jsr,tb_ruku_main.jsfs FROM tb_ruku_detail INNER JOIN tb_ruku_main ON tb_ruku_detail.rkID = tb_ruku_main.rkID INNER JOIN tb_spinfo ON tb_ruku_detail.spid = tb_spinfo.id;");
		sqls.add("DROP VIEW IF EXISTS v_sellView;");// 插入删除视图语句
		// 插入创建爱视图语句
		sqls.add("CREATE VIEW v_sellView AS SELECT tb_sell_main.sellID, tb_spinfo.spname, tb_sell_detail.spid, tb_spinfo.gg, tb_sell_detail.dj, tb_sell_detail.sl,tb_sell_detail.sl * tb_sell_detail.dj AS je, tb_sell_main.khname, tb_sell_main.xsdate, tb_sell_main.czy, tb_sell_main.jsr,tb_sell_main.jsfs FROM tb_sell_detail INNER JOIN tb_sell_main ON tb_sell_detail.sellID = tb_sell_main.sellID INNER JOIN tb_spinfo ON tb_sell_detail.spid = tb_spinfo.id;");

		java.util.Date date = new java.util.Date();// 通过Date对象获得当前时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");// 设置当前时间的输出格式
		String backupTime = sdf.format(date);// 格式化Date对象
		String filePath = "backup\\" + backupTime + ".sql";// 通过拼接字符串获得备份文件的存放路径

		File sqlFile = new File(filePath);// 创建备份文件对象
		FileOutputStream fos = null;// 文件字节输出流
		OutputStreamWriter osw = null;// 字节流转为字符流
		BufferedWriter rw = null;// 缓冲字符流
		try {
			fos = new FileOutputStream(sqlFile);
			osw = new OutputStreamWriter(fos);
			rw = new BufferedWriter(osw);
			for (String tmp : sqls) {// 遍历所有备份sql
				rw.write(tmp);// 向文件中写入sql
				rw.newLine();// 文件换行
				rw.flush();// 字符流刷新
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 倒序依次关闭所有IO流
			if (rw != null) {
				try {
					rw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (osw != null) {
				try {
					osw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return filePath;
	}

(7)restore(String filePath)方法
该方法用于执行数据库的恢复操作,这里主要通过逐行执行备份文件中的sql语句实现的,代码如下:

// 数据库恢复
	public static void restore(String filePath) {
		File sqlFile = new File(filePath);// 创建备份文件对象
		Statement stmt = null;// sql语句直接接口
		FileInputStream fis = null;// 文件输入字节流
		InputStreamReader isr = null;// 字节流转为字符流
		BufferedReader br = null;// 缓存输入字符流
		try {
			fis = new FileInputStream(sqlFile);
			isr = new InputStreamReader(fis);
			br = new BufferedReader(isr);
			String readStr = null;// 缓冲字符串,保存备份文件中一行的内容
			while ((readStr = br.readLine()) != null) {// 逐行读取备份文件中的内容
				if (!"".equals(readStr.trim())) {// 如果读取的内容不为空
					stmt = conn.createStatement();// 创建sql语句直接接口
					int count = stmt.executeUpdate(readStr);// 执行sql语句
					stmt.close();// 关闭接口
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 倒序依次关闭所有IO流
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (isr != null) {
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

(8)checkLogin(String userStr, String passStr)方法
该方法用于判断登录用户的用户名与密码是否正确,方法的返回值是boolean类型,接收的参数有useStr和passStr,分别是用户名与密码信息。该方法将在一条SQL语句中获取指定用户名和密码的数据。如果用户名和密码正确,则返回true值,否则返回false值。代码如下:

// 验证登录
	public static boolean checkLogin(String userStr, String passStr)
			throws SQLException {
		ResultSet rs = findForResultSet("select * from tb_userlist where name='"
				+ userStr + "' and pass='" + passStr + "'");
		if (rs == null)
			return false;
		return rs.next();
	}

你可能感兴趣的:(JAVA实例)