欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > MySQL最后练习,转转好物交易平台项目

MySQL最后练习,转转好物交易平台项目

2025/5/9 11:27:49 来源:https://blog.csdn.net/it_s_raining/article/details/143935210  浏览:    关键词:MySQL最后练习,转转好物交易平台项目

第一步,做一个项目要先做数据库,创建表

这边已经帮你们创好了,

CREATE TABLE UserInformation_普通用户信息表 (
id INT(4) NOT NULL COMMENT '编号' AUTO_INCREMENT,
username VARCHAR(10) NOT NULL COMMENT '用户名',
`password` VARCHAR(20) NOT NULL COMMENT '密码',
balance FLOAT NOT NULL COMMENT '余额',
PRIMARY KEY (id)  
);create table AdministratorInformation_管理员信息表(
id int(4) not null comment'编号' PRIMARY KEY auto_increment,
adname varchar(10) comment'用户名',
`password` varchar(20) comment'密码'
)create table transactionrecord_交易记录表(
id int(4) not null comment'编号' PRIMARY KEY auto_increment,
buyer int(4) comment'买家编号',
Gooditemnumber int(4) comment'好物编号',
SellerID int(4) comment'卖家编号',
TradingHours datetime comment'交易时间',
Receiptinformation varchar(50) comment'收货信息'
)create table GoodItemCategory_好物类别表(
id int(4) not null comment'编号' PRIMARY KEY auto_increment,
category varchar(10) comment'好物名称'
)create table GoodItemInformation_好物信息表(
id int(4) not null comment'编号' PRIMARY KEY auto_increment,
`name` varchar(10) comment'名称',
price float comment'价格',
introduce varchar(50) comment'介绍',
Listingtime datetime comment'上架时间',
Isitforsale tinyint(1) comment'是否在售',
Typenumber int(4) comment'类型编号',
SellerID int(4) comment'卖家编号'
)

解下来,给你们一个练习,把这些表的主外键连接

表数据可以生成

有一些MySQL软件没有,可能版本比较低,可以去搜索“安杰爱编程”,他有详细的MySQL安装教程

接下来表有了,来做功能

 public static void main(String[] args) throws SQLException {boolean f = true;System.out.println("欢迎来到转转好物交易平台");System.out.println("请先登录!!! ");den();while (f) {if (a == 1 && ff) {System.out.println("   1,查看好物列表     2,上架新的好物    3,查看我的在售好物    4,账户操作    0,退出");switch (input.nextInt()) {case 1:Gooditeminformationdao g = new GooditeminformationdaoImpl();g.haowu(1,"饼干",30,"2000-3-2");break;case 2:Gooditeminformationxianxidao go = new GooditeminformationxianxidaoImpl();go.xiaoxi();break;case 3:Gooditeminformationzijidao goo = new GooditeminformationzijidaoImpl();goo.zaisho();break;case 4:break;case 0:f = false;break;}} else if (a == 2) {System.out.println("   1,查看交易记录     2,生成交易记录     3,查看好物列表    4,下架不合格商品      5,封禁违规账号   0,退出系统");switch (input.nextInt()) {case 1:Transactionrecorddao t = new TransactionrecorddaoImpl();List<Text11duix> list =  t.chak();for (Text11duix text11duix : list) {System.out.print(text11duix.a);System.out.print(text11duix.b);System.out.print(text11duix.date);System.out.print(text11duix.name);System.out.print(text11duix.pric);System.out.print(text11duix.uname);System.out.print(text11duix.username);System.out.println(" ");}break;case 2:break;case 3:break;case 4:break;case 0:f = false;break;}}}}public static void den() throws SQLException {System.out.println("用户登录,账号类型:1,普通用户   2,管理员");System.out.println("请输入账号类型:");a = input.nextInt();System.out.println("请输入账号:");String b = input.next();System.out.println("请输入密码:");String c = input.next();Userinformationdao u = new UserinformationdaoImpl();u.tx(a, b, c);}

第一个功能,在最下面就是登录,进去前要先登录 ,人物可以看表,一共有普通人员和管理员

普通人员的功能做三个,最后一个账号操作当做练习,做一个可以对账号进行操作修改密码的功能

public class UserinformationdaoImpl extends BaseDao implements Userinformationdao {public Text1 tt=new Text1();public Text1duix t=new Text1duix();@Overridepublic Text1duix tx(int lei, String zhang, String mim) throws SQLException {String sql=null;if(lei==1) {sql="select * from userinformation_普通用户信息表 where username=? and password=?";}else if(lei==2){sql="select * from administratorinformation_管理员信息表 where username=? and password=?";}PreparedStatement pr=con.prepareStatement(sql);pr.setString(1,zhang);pr.setString(2,mim);ResultSet re=pr.executeQuery();if(re.next()){System.out.println("登录成功");Text1.ff =true;t.setId(Integer.valueOf(re.getInt("id")));t.setZhanghaoleixing(lei);t.setPwd(re.getString("password"));t.setName(re.getString("username"));}else{System.out.println("登录失败");}return t;}
}

这里是登录的内部代码,这是一个实现接口,前面的接口由你们自行创建

第一个功能

public class GooditeminformationdaoImpl extends BaseDao implements Gooditeminformationdao {Scanner inpt = new Scanner(System.in);@Overridepublic List<Text2duixi> haowu(int id, String na, int jiage, String date) throws SQLException {int i = 2;int a = 0;int b = 0;PreparedStatement pr;ResultSet re = null;String sql = null;List<Text2duixi> l = new ArrayList<>();while (true) {if (i == 2) {sql = "SELECT g.id,g.`name`,g.price,g.Listingtime,go.category,u.username,g.introduce,g.Listingtime FROM gooditeminformation_好物信息表 g join gooditemcategory_好物类别表 go on g.Typenumber=go.id join userinformation_普通用户信息表 u on g.SellerID=u.id GROUP BY g.id,g.`name`,g.price,g.Listingtime,go.category,u.username,g.introduce,g.Listingtime ORDER BY id LIMIT 3 OFFSET ?;";if (a != 0) {a = a - 3;}} else if (i == 3) {sql = "SELECT g.id,g.`name`,g.price,g.Listingtime,go.category,u.username,g.introduce,g.Listingtime FROM gooditeminformation_好物信息表 g join gooditemcategory_好物类别表 go on g.Typenumber=go.id join userinformation_普通用户信息表 u on g.SellerID=u.id GROUP BY g.id,g.`name`,g.price,g.Listingtime,go.category,u.username,g.introduce,g.Listingtime ORDER BY id LIMIT 3 OFFSET ?;";if ((b - 3) != a) {a = a + 3;} else {System.out.println("没有下一页了哦!");}} else if (i == 0) {break;} else if (i == 1) {System.out.println("请输入要查看的好物编号:");int in=inpt.nextInt();String sqll = sql;PreparedStatement ppr=con.prepareStatement(sqll);ppr.setInt(1, a);ResultSet rre=ppr.executeQuery();int iid=0;int haowu=0;int idd=0;String dat=null;int jiag=0;while (rre.next()){if(rre.getInt("id")==in){System.out.print("好物名称:");System.out.println(rre.getString("name"));System.out.print("好物类型:");System.out.println(rre.getString("category"));System.out.print("好物价格:");System.out.println(rre.getInt("price"));System.out.print("好物主人:");System.out.println(rre.getString("username"));System.out.print("好物介绍:");System.out.println(rre.getString("introduce"));System.out.print("上架时间:");System.out.println(rre.getString("Listingtime"));iid=rre.getInt("id");haowu=rre.getInt("id");idd=rre.getInt("id");dat=rre.getString("Listingtime");jiag=rre.getInt("price");}System.out.println("1,购买此好物    输入其他返回上一级");int ai=inpt.nextInt();if(ai==1){System.out.println("请输入收货信息地址:");String ab=inpt.next();String ssql="insert into transactionrecord_交易记录表 values (null,?,?,?,?,?)";PreparedStatement prr=con.prepareStatement(ssql);prr.setInt(1,iid);prr.setInt(2,haowu);prr.setInt(3,idd);prr.setString(4,dat);prr.setString(5,ab);prr.executeUpdate();String sqql="update userinformation_普通用户信息表 set balance=(balance-?) where id=?";PreparedStatement prp=con.prepareStatement(sqql);prp.setInt(1,jiag);prp.setInt(2,iid);prp.executeUpdate();System.out.println("购买成功");}else{break;}}} else {break;}pr = con.prepareStatement(sql);pr.setInt(1, a);re = pr.executeQuery();while (re.next()) {Text2duixi t = new Text2duixi();System.out.print(re.getString("id"));System.out.print(" ");System.out.print(re.getString("name"));System.out.print(" ");System.out.print(re.getString("price"));System.out.print(" ");System.out.print(re.getString("Listingtime"));System.out.println(" ");t.setId(re.getInt("Id"));t.setName(re.getString("name"));t.setJiage(re.getInt("price"));t.setDate(re.getString("Listingtime"));l.add(t);}System.out.println("1,查看好物详细      2,上一页     3,下一页     0,返回上一级");i = inpt.nextInt();}return l;}
}

主要!!,我这些代码都是用dao模式和三层架构写的,如果不会的,可以点击我头像寻找详细的dao模式讲解和三层架构的讲解

上面第一个功能主要做一个分页查看商品,还可以选商品id进行购买,内容比较多,请仔细观看

第二个功能

public class GooditeminformationxianxidaoImpl extends BaseDao implements Gooditeminformationxianxidao {Userinformationdao u=new UserinformationdaoImpl();@Overridepublic void xiaoxi() throws SQLException {String sql = "insert into gooditeminformation_好物信息表 values (null,?,?,?,null,?,?,?)";PreparedStatement pr = con.prepareStatement(sql);Scanner in=new Scanner(System.in);System.out.println("请输入物品名称:");pr.setString(1,in.next());System.out.println("请输入价格:");pr.setInt(2,in.nextInt());System.out.println("请输入物品的介绍:");pr.setString(3,in.next());System.out.println("是否要出售:      1,出售   2,不出售");pr.setInt(4,in.nextInt());System.out.println("类型编号:      1,食品  2,菜品  3,工具   4,用具   5,零食");pr.setInt(5,in.nextInt());pr.setInt(6,1);int i= pr.executeUpdate();if(i>0){System.out.println("添加成功");}else{System.out.println("添加失败");}}
}

添加商品,?代表占位符,我的dao模式也有讲解、

第三个功能

public class GooditeminformationzijidaoImpl extends BaseDao implements Gooditeminformationzijidao {@Overridepublic void zaisho() throws SQLException {String sql="select * from gooditeminformation_好物信息表 g join userinformation_普通用户信息表 u on g.SellerID=u.id join gooditemcategory_好物类别表 go on g.Typenumber=go.id";PreparedStatement pr=con.prepareStatement(sql);ResultSet re=pr.executeQuery();while (re.next()){System.out.print(re.getString("id"));System.out.print(" ");System.out.print(re.getString("name"));System.out.print(" ");System.out.print(re.getString("category"));System.out.print(" ");System.out.print(re.getString("price"));System.out.print(" ");System.out.print(re.getString("Listingtime"));System.out.print(" ");System.out.print(re.getString("introduce"));System.out.println(" ");}}
}

这是第三的功能,主要是查看自己的商品,然后可以进行对自己的商品进行下架

管理员,第一个功能

public class TransactionrecorddaoImpl extends BaseDao implements Transactionrecorddao {@Overridepublic List<Text11duix> chak() throws SQLException {String sql = "select t.id,go.`name`,go.price,t.TradingHours, (select  username from userinformation_普通用户信息表  where  id=t.SellerID) as username1 ,(select  username from userinformation_普通用户信息表  where  id=t.buyer) as username2  from transactionrecord_交易记录表 t join userinformation_普通用户信息表 u on t.SellerID=u.id join gooditeminformation_好物信息表 go on t.Gooditemnumber=go.id ";PreparedStatement pr = con.prepareStatement(sql);ResultSet re = pr.executeQuery();List<Text11duix> t = new ArrayList<>();while (re.next()) {Text11duix tt = new Text11duix();tt.setA(re.getInt("id"));tt.setB(re.getInt("id"));tt.setName(re.getString("name"));tt.setPric(re.getInt("price"));tt.setUname(re.getString("username1"));tt.setUsername(re.getString("username2"));tt.setDate(re.getString("TradingHours"));t.add(tt);}return t;}
}

可以查看所有商品交易记录,查看所有的商品的卖家和买家,有点长,主要是子查询,可以点击头像看我MySQL子查询的讲解

以上就是转转好物的功能

练习!

把普通人员的第5个功能做了,主要是对账号操作修改密码就行了

把管理员全部功能也补上

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词