JavaWeb购物平台小项目

JavaWeb购物平台小项目

最近也快到期末了,web程序设计的课程也迎来了第一次大作业,内容是要做一个购物平台,用户能够添加购物车,购买商品,留言,反馈等,而管理员可以添加商品,浏览反馈,查看账务等,花了一周时间终于做好了。
首先是数据库的设计
在这里插入图片描述
项目整体的架构如下,没有用到特殊复杂的技术,只是简单的使用了JavaBean,Servlet等基础内容,毕竟还没有学到JEE和Spring,目前还只自己多去了解。
在这里插入图片描述
整个项目的功能如下:
在这里插入图片描述
在数据库的连接上采用了单例模式,将构造函数设为私有,只能在类内部实例化一个类对象,其他类想要获取一个实例时,只能通过get方法获取。

public class ConnectDB {
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/goods";

    static final String USER = "root";
    static final String PASS = "LZSF2239";

    Connection con;
    Statement stm;
    ResultSet resSet;

    private static ConnectDB db;

    private ConnectDB() throws SQLException{
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("驱动注册成功");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("驱动注册失败");
        }
        try {
            con = DriverManager.getConnection(DB_URL,USER,PASS);
            System.out.println("数据库连接成功");
            stm = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
        }
        catch (Exception e){
            System.out.println("数据库连接失败");
        }

    }

    public static ConnectDB getDb() throws SQLException {
        if (db == null){
            db = new ConnectDB();
        }
        return db;
    }

    public ResultSet query(String sql) throws SQLException{
        resSet = stm.executeQuery(sql);
        return resSet;
    }

    public int update(String sql) throws SQLException{
        int rows = stm.executeUpdate(sql);
        return rows;
    }

    public void close() throws SQLException{
        if (resSet != null)resSet.close();
        if (stm != null)stm.close();
        if (con != null)con.close();
    }
}

另外在结算时,对商品库存的更新采用乐观锁的方式,乐观锁即乐观的认为数据一般不会冲突,只有需要提交数据时才对数据进行检测。

String sqlminus = String .format("update goods set count=count-%s where goods_id='%s' and count-%s>=0",
                shoppingcarBean.getCount(),shoppingcarBean.getGoods_id(),shoppingcarBean.getCount()
        );

其余没有用到什么特别的东西。整体上感觉项目还有很多不完善的地方,例如类之间的耦合过高,没有对数据库传入的数据进行过滤不能避免SQL注入等,后续的练习中还需要多加学习。
项目运行结果如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 10
    点赞
  • 68
    收藏
    觉得还不错? 一键收藏
  • 17
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值