由于代码太多,我会分为4个部分发这些代码,可以找我要源码
带dao后缀的都是一些方法,连接了SQL语句,主要是增删改查
(dao包里面有一些注释,供大家理解)
先写好DBHelper包,主要用于连接MySQL文件和MySQL数据
java
复制代码
package com.zking.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBHelper {
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getCon() {
try {
return DriverManager.getConnection(
"jdbc:mysql://localhost:3306/user1?serverTimezone=GMT",
"root",
"123456");
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void myClose(Connection con,PreparedStatement ps,ResultSet rs) {
try {
if(con!=null)
con.close();
if(ps!=null)
ps.close();
if(rs!=null)
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
再写好实体类(food(食物类),sale(订单类),Record(订单消费记录),saleltem(订单详情),user(用户),cart(购物车),category(食物类型)
food
java
复制代码
package com.zking.pojo;
public class Food {
private Integer id;
private Integer cid;
private String name;
private Double price;
private String cover;
private String detail;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getCover() {
return cover;
}
public void setCover(String cover) {
this.cover = cover;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
@Override
public String toString() {
return "Food [id=" + id + ", cid=" + cid + ", name=" + name + ", price=" + price + ", cover=" + cover
+ ", detail=" + detail + "]";
}
public Food(Integer id, Integer cid, String name, Double price, String cover, String detail) {
super();
this.id = id;
this.cid = cid;
this.name = name;
this.price = price;
this.cover = cover;
this.detail = detail;
}
public Food() {
super();
// TODO Auto-generated constructor stub
}
}
Record
java
复制代码
package com.zking.pojo;
import java.sql.Date;
public class Record {
private Integer id;
private Integer uid;
private Double amount;
private String kind;
private Date trstime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Double getAmount() {
return amount;
}
public void setAmount(Double amount) {
this.amount = amount;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public Date getTrstime() {
return trstime;
}
public void setTrstime(Date trstime) {
this.trstime = trstime;
}
@Override
public String toString() {
return "Record [id=" + id + ", uid=" + uid + ", amount=" + amount + ", kind=" + kind + ", trstime=" + trstime
+ "]";
}
public Record(Integer id, Integer uid, Double amount, String kind, Date trstime) {
super();
this.id = id;
this.uid = uid;
this.amount = amount;
this.kind = kind;
this.trstime = trstime;
}
public Record() {
super();
// TODO Auto-generated constructor stub
}
}
sale
java
复制代码
package com.zking.pojo;
import java.sql.Date;
public class Sale {
private Integer id;
private String no;
private Integer uid;
private Double total;
private Date createTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Double getTotal() {
return total;
}
public void setTotal(Double total) {
this.total = total;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
return "Sale [id=" + id + ", no=" + no + ", uid=" + uid + ", total=" + total + ", createTime=" + createTime
+ "]";
}
public Sale(Integer id, String no, Integer uid, Double total, Date createTime) {
super();
this.id = id;
this.no = no;
this.uid = uid;
this.total = total;
this.createTime = createTime;
}
public Sale() {
super();
// TODO Auto-generated constructor stub
}
}
saleltem
java
复制代码
package com.zking.pojo;
public class SaleItem {
private Integer id;
private String sid;
private Integer fid;
private Integer num;
private Double total;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public Integer getFid() {
return fid;
}
public void setFid(Integer fid) {
this.fid = fid;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
public Double getTotal() {
return total;
}
public void setTotal(Double total) {
this.total = total;
}
@Override
public String toString() {
return "SaleItem [id=" + id + ", sid=" + sid + ", fid=" + fid + ", num=" + num + ", total=" + total + "]";
}
public SaleItem(Integer id, String sid, Integer fid, Integer num, Double total) {
super();
this.id = id;
this.sid = sid;
this.fid = fid;
this.num = num;
this.total = total;
}
public SaleItem() {
super();
// TODO Auto-generated constructor stub
}
}
user
java
复制代码
package com.zking.pojo;
public class User {
private Integer id;
private String name;
private String pwd;
private Double money;
private String ques;
private String answer;
private String avatar;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
public String getQues() {
return ques;
}
public void setQues(String ques) {
this.ques = ques;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + ", money=" + money + ", ques=" + ques
+ ", answer=" + answer + ", avatar=" + avatar + "]";
}
public User(Integer id, String name, String pwd, Double money, String ques, String answer, String avatar) {
super();
this.id = id;
this.name = name;
this.pwd = pwd;
this.money = money;
this.ques = ques;
this.answer = answer;
this.avatar = avatar;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
}
category
java
复制代码
package com.zking.pojo;
public class Category {
private Integer id;
private String name;
private String en;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEn() {
return en;
}
public void setEn(String en) {
this.en = en;
}
@Override
public String toString() {
return "Category [id=" + id + ", name=" + name + ", en=" + en + "]";
}
public Category(Integer id, String name, String en) {
super();
this.id = id;
this.name = name;
this.en = en;
}
public Category() {
super();
// TODO Auto-generated constructor stub
}
}
cart
java
复制代码
package com.zking.pojo;
public class Cart {
private Integer id;
private Integer uid;
private Integer fid;
private Integer num;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Integer getFid() {
return fid;
}
public void setFid(Integer fid) {
this.fid = fid;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
@Override
public String toString() {
return "Cart [id=" + id + ", uid=" + uid + ", fid=" + fid + ", num=" + num + "]";
}
public Cart(Integer id, Integer uid, Integer fid, Integer num) {
super();
this.id = id;
this.uid = uid;
this.fid = fid;
this.num = num;
}
public Cart() {
super();
// TODO Auto-generated constructor stub
}
}
之后再写这些东西要用到的方法,比如说用户查询,查询食品之类的,我简称dao包
cartdao
java
复制代码
package com.zking.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zking.pojo.Cart;
import com.zking.util.DBHelper;
public class Cartdao {
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
/**
* 查询购物车
*
* @param uid 用户id
*/
public List<Cart> list(int uid) {
List<Cart> cs=new ArrayList<>();
String sql="select * from cart where uid=?";
try {
con=DBHelper.getCon();
ps=con.prepareStatement(sql);
ps.setInt(1, uid);
rs=ps.executeQuery();
while(rs.next()) {
Cart cc = new Cart();
cc.setId(rs.getInt(1));
cc.setUid(rs.getInt(2));
cc.setFid(rs.getInt(3));
cc.setNum(rs.getInt(4));
cs.add(cc);
}
} catch (Exception e) {
e.printStackTrace();
}
return cs;
}
/**
* 查询购物车(根据用户id与商品id查询)
* @param uid 用户编号
* @param fid 商品编号
*/
public Cart get(int uid,int fid) {
String sql="select * from cart where uid=? and fid=?";
try {
con=DBHelper.getCon();
ps=con.prepareStatement(sql);
ps.setInt(1, uid);
ps.setInt(2, fid);
rs=ps.executeQuery();
if(rs.next()) {
Cart cc = new Cart();
cc.setId(rs.getInt(1));
cc.setUid(rs.getInt(2));
cc.setFid(rs.getInt(3));
cc.setNum(rs.getInt(4));
return cc;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//删除购物车的方法
/**
* 添加购物车
*
* @param cart 购物车
*/
public int add(int uid,int fid) {
String sql = "insert into cart(uid,fid,num) values(?,?,?)";
try {
con=DBHelper.getCon();
ps=con.prepareStatement(sql);
ps.setInt(1, uid);
ps.setInt(2, fid);
ps.setInt(3, 1);
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* 删除购物车
*
* @param id 购物车编号
*/
public int del(int id) {
String sql = "delete from cart where id=?";
try {
con=DBHelper.getCon();
ps=con.prepareStatement(sql);
ps.setInt(1, id);
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* 更新购物车数量
* @param id 购物车编号
* @param num 购物车变动数量
*/
public int editNum(int id, int num) {
String sql = "update cart set num=num+? where id=?";
try {
con=DBHelper.getCon();
ps=con.prepareStatement(sql);
ps.setInt(1, num);
ps.setInt(2, id);
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* 清空购物车
* @param uid 用户编号
*/
public int delAll(int uid) {
String sql = "delete from cart where uid=?";
try {
con=DBHelper.getCon();
ps=con.prepareStatement(sql);
ps.setInt(1, uid);
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
categorydao
java
复制代码
package com.zking.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zking.pojo.Category;
import com.zking.util.DBHelper;
public class CategoryDao {
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
public List<Category> list(){
List<Category> ds=new ArrayList<>();
try {
con=DBHelper.getCon();
String sql = "select * from category";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
Category c=new Category();
c.setId(rs.getInt(1));
c.setName(rs.getString(2));
c.setEn(rs.getString(3));
ds.add(c);
}
} catch (Exception e) {
e.printStackTrace();
}
return ds;
}
/**
* 根据id查询单个分类
*/
public Category findById(int cid) {
String sql = "select * from category where id=?";
try {
con=DBHelper.getCon();
ps=con.prepareStatement(sql);
ps.setInt(1, cid);
rs=ps.executeQuery();
while(rs.next()) {
Category c=new Category();
c.setId(rs.getInt(1));
c.setName(rs.getString(2));
c.setEn(rs.getString(3));
return c;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//根据id查询商品的个数
public int findConut(int cid) {
String sql = "select count(*)from food a inner join category b on a.cid=b.cid where b.id=?";
try {
con=DBHelper.getCon();
ps=con.prepareStatement(sql);
ps.setInt(1, cid);
rs=ps.executeQuery();
while(rs.next()) {
int cc=rs.getInt(1);
return cc;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
fooddao
java
复制代码
package com.zking.pojo;
public class Food {
private Integer id;
private Integer cid;
private String name;
private Double price;
private String cover;
private String detail;
private Integer up;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getCover() {
return cover;
}
public void setCover(String cover) {
this.cover = cover;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public Integer getUp() {
return up;
}
public void setUp(Integer up) {
this.up = up;
}
@Override
public String toString() {
return "Food [id=" + id + ", cid=" + cid + ", name=" + name + ", price=" + price + ", cover=" + cover
+ ", detail=" + detail + ", up=" + up + "]";
}
public Food(Integer id, Integer cid, String name, Double price, String cover, String detail, Integer up) {
super();
this.id = id;
this.cid = cid;
this.name = name;
this.price = price;
this.cover = cover;
this.detail = detail;
this.up = up;
}
public Food() {
super();
// TODO Auto-generated constructor stub
}
}
recordao
java
复制代码
package com.zking.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zking.pojo.Record;
import com.zking.util.DBHelper;
public class RecorDao {
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
//查询所有的消费记录
//v:全部,充值,消费
public List<Record> list(int uid,String v){
List<Record> ds=new ArrayList<>();
try {
con=DBHelper.getCon();
String sql="select *from record where uid=? ";
if(v!=null&&!v.equals("全部")) {
sql+="and kind='"+v+"'";
//and Kind='充值'
//and Kind='消费'
}
ps=con.prepareStatement(sql);
ps.setInt(1, uid);
rs=ps.executeQuery();
while(rs.next()) {
Record r=new Record();
r.setId(rs.getInt(1));
r.setUid(rs.getInt(2));
r.setAmount(rs.getDouble(3));
r.setKind(rs.getString(4));
r.setTrstime(rs.getDate(5));
ds.add(r);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return ds;
}
public int add (Record r){
String sql="insert into record(uid,amount,kind)values(?,?,?)";
try {
con=DBHelper.getCon();
ps=con.prepareStatement(sql);
ps.setInt(1, r.getUid());
ps.setDouble(2,r.getAmount());
ps.setString(3, r.getKind());
return ps.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return 0;
}
}
seledao
java
复制代码
package com.zking.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zking.pojo.Sale;
import com.zking.util.DBHelper;
public class SaleDao {
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
/**
* 查询所有订单
* @param uid 用户编号
*/
public List<Sale> list(int uid) {
List<Sale> ss=new ArrayList<>();
String sql = "select * from sale where uid=?";
try {
con=DBHelper.getCon();
ps=con.prepareStatement(sql);
ps.setInt(1, uid);
rs=ps.executeQuery();
while(rs.next()) {
Sale sale=new Sale();
sale.setId(rs.getInt(1));
sale.setNo(rs.getString(2));
sale.setUid(rs.getInt(3));
sale.setTotal(rs.getDouble(4));
sale.setCreateTime(rs.getDate(5));
ss.add(sale);
}
} catch (Exception e) {
e.printStackTrace();
}
return ss;
}
/**
* 添加订单
*/
public int add(Sale sale) {
String sql = "insert into sale(no,uid,total) values(?,?,?)";
try {
con = DBHelper.getCon();
ps = con.prepareStatement(sql);
ps.setString(1, sale.getNo());
ps.setInt(2, sale.getUid());
ps.setDouble(3, sale.getTotal());
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* 删除订单
*/
public int del(String no) {
String sql = "delete from sale where no=?";
try {
con = DBHelper.getCon();
ps = con.prepareStatement(sql);
ps.setString(1, no);
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
SaleItemDao
java
复制代码
package com.zking.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zking.pojo.SaleItem;
import com.zking.util.DBHelper;
public class SaleItemDao {
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
/**
* 根据订单编号查询订单项目
* @param sid 订单编号
*/
public List<SaleItem> list(String sid) {
String sql = "select * from sale_item where sid=?";
List<SaleItem> ss=new ArrayList<>();
try {
con=DBHelper.getCon();
ps=con.prepareStatement(sql);
ps.setString(1, sid);
rs=ps.executeQuery();
while(rs.next()) {
SaleItem si=new SaleItem();
si.setId(rs.getInt(1));
si.setSid(rs.getString(2));
si.setFid(rs.getInt(3));
si.setNum(rs.getInt(4));
si.setTotal(rs.getDouble(5));
ss.add(si);
}
} catch (Exception e) {
e.printStackTrace();
}
return ss;
}
/**
* 添加订单
*/
public int add(SaleItem item) {
String sql = "insert into sale_item(sid,fid,num,total) values(?,?,?,?)";
try {
con = DBHelper.getCon();
ps = con.prepareStatement(sql);
ps.setString(1, item.getSid());
ps.setInt(2, item.getFid());
ps.setInt(3, item.getNum());
ps.setDouble(4, item.getTotal());
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* 删除订单
*/
public int del(String no) {
String sql = "delete from sale_item where sid=?";
try {
con = DBHelper.getCon();
ps = con.prepareStatement(sql);
ps.setString(1, no);
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
userdao
java
复制代码
package com.zking.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.zking.pojo.User;
import com.zking.util.DBHelper;
public class Userdao {
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
/**
* 登录
*/
public User login(User user) {
String sql = "select * from user where name=? and pwd=?";
try {
con=DBHelper.getCon();
ps=con.prepareStatement(sql);
ps.setString(1, user.getName());
ps.setString(2, user.getPwd());
rs=ps.executeQuery();
if(rs.next()) {
User u=new User();
u.setId(rs.getInt(1));
u.setName(rs.getString(2));
u.setPwd(rs.getString(3));
u.setMoney(rs.getDouble(4));
u.setQues(rs.getString(5));
u.setAnswer(rs.getString(6));
return u;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
/**
* 添加用户
*/
public int add(User user) {
String sql = "insert into user(name,pwd,ques,answer,avatar) values(?,?,?,?,?)";
try {
con=DBHelper.getCon();
ps=con.prepareStatement(sql);
ps.setString(1, user.getName());
ps.setString(2, user.getPwd());
ps.setString(3, user.getQues());
ps.setString(4, user.getAnswer());
ps.setString(5, user.getAvatar());
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* 根据用户名查找用户(用于查询用户的密保)
*/
public User findByName(String name) {
String sql = "select * from user where name=?";
try {
con=DBHelper.getCon();
ps=con.prepareStatement(sql);
ps.setString(1, name);
rs=ps.executeQuery();
if(rs.next()) {
User u=new User();
u.setId(rs.getInt(1));
u.setName(rs.getString(2));
u.setPwd(rs.getString(3));
u.setMoney(rs.getDouble(4));
u.setQues(rs.getString(5));
u.setAnswer(rs.getString(6));
return u;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
/**
* 修改用户密码
*/
public int editPwd(User user) {
String sql = "update user set pwd=? where name=?";
try {
con=DBHelper.getCon();
ps=con.prepareStatement(sql);
ps.setString(1, user.getPwd());
ps.setString(2, user.getName());
return ps.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return 0;
}
//根据id查询用户方法(用于刷新用户数据)
//uid 用户id
public User getById(int uid) {
String sql="select *from user where id=?";
try {
con=DBHelper.getCon();
ps=con.prepareStatement(sql);
ps.setInt(1, uid);
rs=ps.executeQuery();
if(rs.next()) {
User u=new User();
u.setId(rs.getInt(1));
u.setName(rs.getString(2));
u.setPwd(rs.getString(3));
u.setMoney(rs.getDouble(4));
u.setQues(rs.getString(5));
u.setAnswer(rs.getString(6));
u.setAvatar(rs.getString(7));
return u;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
//修改用户信息
public int edit(User user) {
String sql="update user set name=?,pwd=?,ques=?,answer=?,avatar=?,where id=?";
try {
con=DBHelper.getCon();
ps=con.prepareStatement(sql);
ps.setString(1, user.getName());
ps.setString(2, user.getPwd());
ps.setString(3, user.getQues());
ps.setString(4, user.getAnswer());
ps.setString(5, user.getAvatar());
ps.setInt(6, user.getId());
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* 修改用户余额
*
* @param uid 用户id
* @param money 变动的钱
*/
public int editMoney(int uid, double money) {
String sql = "update user set money=money+? where id=?";
try {
con=DBHelper.getCon();
ps=con.prepareStatement(sql);
ps.setDouble(1, money);
ps.setInt(2, uid);
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
由于代码太多,我会分为4个部分发这些代码,可以找我要源码,还有就是我学习Java只学习了3个月,这是第一个比较大的项目,不喜勿喷