网上书店管理系统项目【Java数据库编程实战】

打印 上一主题 下一主题

主题 554|帖子 554|积分 1662

大家好!最近学习完数据库系统,一直在写实战项目-网上书店管理系统。其功能一般包括:图书信息管理、用户信息管理、图书购买、图书订单查看、图书添加、图书维护等等。现在做一个总结。 **源码及搭建教程已经上传至资源!仅供下载学习。下载



文章目录



1.效果展示


2.需求功能

用户可以进行注册登陆系统,在用户的界面上,其可以进行查看网上书店里的图书类别和所在类别下的图书,根据自己的需求可在订单项目里添加订单购买自己喜欢的图书;
管理员可以通过自己的账号登录到管理员系统对书店进行管理,其可实现对图书的添加,修改,查询,和删除功能,可以查看用户的订单,修改和维护订单。添家客户的信息用以统计数据。
在构造系统时,首先从需求出发构造数据库,然后再由数据库表结合需求划分系统功能模块。这样,就把一个大的系统解成了几个小系统。这里把系统划分为了三个模块:用户登录模块,管理员模块,用户购买模块。模块分别能够实现以下功能:

  • 登录模块:实现登录,注册功能。
  • 管理员模块:实现对图书的添加修改和删除以及对订单的添加修改和删除功能。
  • 用户购买模块:实现对图书的查找以及对所需图书的下单功能。

3.系统总体设计及部分代码


3.1登录模块设计

用户正确输入用户名和密码,连接到数据库,登录成功!
  1. private void loginActionPerformed(ActionEvent evt) {
  2.              String userName=this.userNameTxt.getText();
  3.                 String password=new String(this.passwordTxt.getPassword());
  4.                 if(StringUtil.isEmpty(userName)){
  5.                         JOptionPane.showMessageDialog(null, "用户名不能为空!");
  6.                         return;
  7.                 }
  8.                 if(StringUtil.isEmpty(password)){
  9.                         JOptionPane.showMessageDialog(null, "密码不能为空!");
  10.                         return;
  11.                 }
  12.                 CUser cuser=new CUser(userName,password);
  13.                 Connection con=null;
  14.                 try {
  15.                         con=dbUtil.getCon();
  16.                         CUser currentCUser =cuserDao.login(con,cuser);
  17.                         if(currentCUser!=null){
  18.                                 dispose();
  19.                                 new CMainFrm().setVisible(true);
  20.                         }else{
  21.                                 JOptionPane.showMessageDialog(null, "用户名或者密码错误!");
  22.                         }
  23.                
  24.                 } catch (Exception e) {
  25.                         // TODO 自动生成的 catch 块
  26.                         e.printStackTrace();
  27.                 }
复制代码
3.2新用户的注册

此模块的核心是创建实例化对象。
  1. private void registrationActionPerformed(ActionEvent evt) {
  2.                 String userName=this.userNameTxt.getText();
  3.                 String password=this.passwordTxt.getText();
  4.                 if(StringUtil.isEmpty(userName)){
  5.                         JOptionPane.showMessageDialog(null, "用户名不能为空!");
  6.                         return;
  7.                 }
  8.                 if(StringUtil.isEmpty(password)){
  9.                         JOptionPane.showMessageDialog(null, "密码不能为空!");
  10.                         return;
  11.                 }
  12.                 Registration registration= new Registration(userName,password);
  13.                 Connection con= null;
  14.                 try {
  15.                         con=dbUtil.getCon();
  16.                         int n= registrationDao.add(con, registration);
  17.                         if(n==1){
  18.                                 JOptionPane.showMessageDialog(null, "注册成功!");
  19.                                 resetValue();
  20.                         }else{
  21.                                 JOptionPane.showMessageDialog(null, "注册失败!");
  22.                         }
  23.                 }catch(Exception e) {
  24.                 }finally {
  25.                         try {
  26.                                 dbUtil.closeCon(con);
  27.                         } catch (Exception e) {
  28.                                 // TODO 自动生成的 catch 块
  29.                                 e.printStackTrace();
  30.                                 JOptionPane.showMessageDialog(null, "注册失败!");
  31.                         }
  32.                 }
  33.         }
复制代码
3.3图书添加模块

管理员在此界面上可对系统里的图书进行查询修改和删除。
  1. public static void main(String[] args) {
  2.                 EventQueue.invokeLater(new Runnable() {
  3.                         public void run() {
  4.                                 try {
  5.                                         BookAddInterFrm frame = new BookAddInterFrm();
  6.                                         frame.setVisible(true);
  7.                                 } catch (Exception e) {
  8.                                         e.printStackTrace();
  9.                                 }
  10.                         }
  11.                 });
  12.         }
复制代码
3.4图书添加事件

此界面主要实现图书的添加功能。
  1. /**
  2. *图书添加事件
  3. */
  4. private void bookAddActionPerformed(ActionEvent evt) {
  5.                 String bookName=this.bookNameTxt.getText();
  6.                 String author=this.authorTxt.getText();
  7.                 String price=this.priceTxt.getText();
  8.                 String bookDesc=this.bookDescTxt.getText();
  9.                 if(StringUtil.isEmpty(bookName)){
  10.                         JOptionPane.showMessageDialog(null, "图书名称不能为空!");
  11.                         return;
  12.                 }
  13.                 if(StringUtil.isEmpty(author)){
  14.                         JOptionPane.showMessageDialog(null, "图书作者不能为空!");
  15.                         return;
  16.                 }
  17.                 if(StringUtil.isEmpty(price)){
  18.                         JOptionPane.showMessageDialog(null, "图书价格不能为空!");
  19.                         return;
  20.                 }
  21.                 String sex="";
  22.                 if(manJrb.isSelected()){
  23.                         sex="男";
  24.                 }else if(femaleJrb.isSelected()){
  25.                         sex="女";
  26.                 }
  27.                 BookType bookType=(BookType) bookTypeJcb.getSelectedItem();
  28.                 int bookTypeId=bookType.getId();
  29.                 Book book=new Book(bookName,author, sex, Float.parseFloat(price) , bookTypeId,  bookDesc);
  30.                 Connection con=null;
  31.                 try{
  32.                         con=dbUtil.getCon();
  33.                         int addNum=bookDao.add(con, book);
  34.                         if(addNum==1){
  35.                                 JOptionPane.showMessageDialog(null, "图书添加成功!");
  36.                                 resetValue();
  37.                         }else{
  38.                                 JOptionPane.showMessageDialog(null, "图书添加失败!");
  39.                         }
  40.                 }catch(Exception e){
  41.                         e.printStackTrace();
  42.                         JOptionPane.showMessageDialog(null, "图书添加失败!");
  43.                 }finally{
  44.                         try {
  45.                                 dbUtil.closeCon(con);
  46.                         } catch (Exception e) {
  47.                                 // TODO Auto-generated catch block
  48.                                 e.printStackTrace();
  49.                         }
  50.                 }
  51.         }
  52.         /**
  53.          * 重置表单
  54.          */
  55.         private void resetValue(){
  56.                 this.bookNameTxt.setText("");
  57.                 this.authorTxt.setText("");
  58.                 this.priceTxt.setText("");
  59.                 this.manJrb.setSelected(true);
  60.                 this.bookDescTxt.setText("");
  61.                 if(this.bookTypeJcb.getItemCount()>0){
  62.                         this.bookTypeJcb.setSelectedIndex(0);
  63.                 }
  64.         }
  65.         /**
  66.          * 初始化图书类别下拉框
  67.          */
  68.         private void fillBookType(){
  69.                 Connection con=null;
  70.                 BookType bookType=null;
  71.                 try{
  72.                         con=dbUtil.getCon();
  73.                         ResultSet rs=bookTypeDao.list(con, new BookType());
  74.                         while(rs.next()){
  75.                                 bookType=new BookType();
  76.                                 bookType.setId(rs.getInt("id"));
  77.                                 bookType.setBookTypeName(rs.getString("bookTypeName"));
  78.                                 this.bookTypeJcb.addItem(bookType);
  79.                         }
  80.                 }catch(Exception e){
  81.                         e.printStackTrace();
  82.                 }finally{
  83.                 }
  84.         }
  85. }
复制代码
3.5买家信息维护

此模块主要用于对买家信息的查找和维护。
  1.         /**
  2.          * 买家信息搜索事件处理
  3.          */
  4.         protected void consumerSerachActionPerformed(ActionEvent evt) {
  5.                 String s_consumerName= this.s_consumerNameTxt.getText();
  6.                 Consumer consumer=new Consumer();
  7.                 consumer.setConsumerName(s_consumerName);
  8.                 this.fillTable(consumer);
  9.         }
  10.         private void fillTable(Consumer consumer){
  11.                 DefaultTableModel dtm=(DefaultTableModel) consumerTable.getModel();
  12.                 dtm.setRowCount(0); // 设置成0行
  13.                 Connection con=null;
  14.                 try{
  15.                         con=dbUtil.getCon();
  16.                         ResultSet rs=consumerDao.list(con, consumer);
  17.                         while(rs.next()){
  18.                                 Vector v=new Vector();
  19.                                 v.add(rs.getString("id"));
  20.                                 v.add(rs.getString("consumerName"));
  21.                                 v.add(rs.getString("sex"));
  22.                                 v.add(rs.getString("age"));
  23.                                 v.add(rs.getString("number"));
  24.                                 v.add(rs.getString("bookName"));
  25.                                 dtm.addRow(v);
  26.                         }
  27.                 }catch(Exception e){
  28.                         e.printStackTrace();
  29.                 }finally {
  30.                         try {
  31.                                 dbUtil.closeCon(con);
  32.                         } catch (Exception e) {
  33.                                 // TODO 自动生成的 catch 块
  34.                                 e.printStackTrace();
  35.                         }
  36.                 }
  37.         }
  38.         /**
  39.          * 买家信息修改
  40.          */
  41.         private void consumerUpdateActionEvet(ActionEvent evt) {
  42.                 String id=idTxt.getText();
  43.                 String consumerName=consumerNameTxt.getText();
  44.                 String sex=sexTxt.getText();
  45.                 String age=ageTxt.getText();
  46.                 String number=numberTxt.getText();
  47.                 String bookName=bookNameTxt.getText();
  48.                 if(StringUtil.isEmpty(id)){
  49.                         JOptionPane.showMessageDialog(null, "请选择要修改的记录");
  50.                         return;
  51.                 }
  52.                 if(StringUtil.isEmpty(consumerName)){
  53.                         JOptionPane.showMessageDialog(null, "购书者名称不能为空");
  54.                         return;
  55.                 }
  56.                 if(StringUtil.isEmpty(age)){
  57.                         JOptionPane.showMessageDialog(null, "年龄不能为空");
  58.                         return;
  59.                 }
  60.                 if(StringUtil.isEmpty(number)){
  61.                         JOptionPane.showMessageDialog(null, "联系方式不能为空");
  62.                         return;
  63.                 }
  64.                 if(StringUtil.isEmpty(bookName)){
  65.                         JOptionPane.showMessageDialog(null, "图书名称不能为空");
  66.                         return;
  67.                 }
  68.                 if(StringUtil.isEmpty(sex)){
  69.                         JOptionPane.showMessageDialog(null, "性别不能为空");
  70.                         return;
  71.                 }
  72.                 Consumer consumer=new Consumer(Integer.parseInt(id),consumerName,sex,age,number,bookName);
  73.                 Connection con=null;
  74.                 try {
  75.                         con=dbUtil.getCon();
  76.                         con=dbUtil.getCon();
  77.                         int modifyNum=consumerDao.update(con, consumer);
  78.                         if(modifyNum==1){
  79.                                 JOptionPane.showMessageDialog(null, "修改成功");
  80.                                 this.resetValue();
  81.                                 this.fillTable(new Consumer());
  82.                         }else{
  83.                                 JOptionPane.showMessageDialog(null, "修改失败");
  84.                         }
  85.                 }catch(Exception e) {
  86.                         e.printStackTrace();
  87.                         JOptionPane.showMessageDialog(null, "修改失败");
  88.                 }finally {
  89.                         try {
  90.                                 dbUtil.closeCon(con);
  91.                         } catch (Exception e) {
  92.                                 // TODO 自动生成的 catch 块
  93.                                 e.printStackTrace();
  94.                         }
  95.                 }
  96.         }
复制代码
3.6订单管理模块

此模块用于图书订单管理,查找,修改,删除等功能的实现。
  1.      /**
  2.          * 订单修改事件
  3.          */
  4.         protected void orderUpdateActionPerformed(ActionEvent evt) {
  5.                 String id=this.idTxt.getText();
  6.                 if(StringUtil.isEmpty(id)){
  7.                         JOptionPane.showMessageDialog(null, "请选择要修改的记录");
  8.                         return;
  9.                 }
  10.                 String addressee=this.addresseeTxt.getText();
  11.                 String number=this.numberTxt.getText();
  12.                 String deliveryMent=this.deliveryMentTxt.getText();
  13.                 String paymentMethod=this.paymentMethodTxt.getText();
  14.                 String shippingAddress=this.shippingAddressTxt.getText();
  15.                 if(StringUtil.isEmpty(addressee)){
  16.                         JOptionPane.showMessageDialog(null, "收件人不能为空!");
  17.                         return;
  18.                 }
  19.                 if(StringUtil.isEmpty(number)){
  20.                         JOptionPane.showMessageDialog(null, "购买数量不能为空!");
  21.                         return;
  22.                 }
  23.                 if(StringUtil.isEmpty(deliveryMent)){
  24.                         JOptionPane.showMessageDialog(null, "运送方式不能为空!");
  25.                         return;
  26.                 }
  27.                 if(StringUtil.isEmpty(paymentMethod)){
  28.                         JOptionPane.showMessageDialog(null, "支付方式不能为空!");
  29.                         return;
  30.                 }
  31.                 if(StringUtil.isEmpty(paymentMethod)){
  32.                         JOptionPane.showMessageDialog(null, "收件地址不能为空!");
  33.                         return;
  34.                 }
  35.                 Book book=(Book) this.bookNameJcb.getSelectedItem();
  36.                 int bookId=book.getId();
  37.                 Order order =new Order(Integer.parseInt(id), addressee, number, deliveryMent, paymentMethod, shippingAddress,
  38.                                 bookId);
  39.                 Connection con =null;
  40.                 try {
  41.                         con=dbUtil.getCon();
  42.                         int addNum=orderDao.update(con, order);
  43.                         if(addNum==1) {
  44.                                 JOptionPane.showMessageDialog(null, "订单修改成功!");
  45.                                 resetValue();
  46.                                 this.fillTable(new Order());
  47.                         }else {
  48.                                 JOptionPane.showMessageDialog(null, "订单修改失败!");
  49.                         }
  50.                 }catch(Exception e) {
  51.                         e.printStackTrace();
  52.                 }finally {
  53.                         try {
  54.                                 dbUtil.closeCon(con);
  55.                         } catch (Exception e) {
  56.                                 // TODO 自动生成的 catch 块
  57.                                 e.printStackTrace();
  58.                                 JOptionPane.showMessageDialog(null, "订单添加失败!");
  59.                         }
  60.                 }
  61.         }
复制代码

4.数据库设计

4.1系统数据库设计

使用sql语句查询项目存储数据用到的数据库表格:

1.管理员信息表

列名数据类型长度主键非空自增IdInt11√√√usenamevarchar20passwordvarchar202.图书类型信息表

列名数据类型长度主键非空自增idInt11√√√BookTypeNameVarchar20bookTypeDesVarchar203.图书信息表

列名数据类型长度主键非空自增BooknameInt11√√√AuthorVarchar20SexVarchar10PriceFloat10bookTypeIdInt11bookDescVarchar10004.订单信息表

列名数据类型长度主键非空自增BuyidInt11√√√NameVarchar20SexVarchar20BuybooknamtelVarchar20WayVarchar20AddressVarchar205.买家信息表

列名数据类型长度主键非空自增IdInt11√√√ConsumernameVarchar50SexVarchar50AgeVarchar50NumberVarchar50BooknameVarchar504.2系统E-R图设计



5.JDBC连接数据库


一定要安装数据库jdbc驱动包!
代码展示:
  1. package com.util;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. /**
  5. * 数据库工具类
  6. */
  7. public class DbUtil {
  8.         private String jdbcName="com.mysql.cj.jdbc.Driver";      // 驱动名称
  9.         数据库连接地址   由于数据库为最新版本  导致驱动名称已改为com.mysql.cj.jdbc.Driver
  10.         //由于时区错乱  执行命令给MySQL服务器设置时区为东八区    serverTimezone=GMT%2B8
  11.         private String dbUrl="jdbc:mysql://localhost:3306/db_book?serverTimezone=GMT%2B8";// 数据库连接地址
  12.         private String dbuserName = "root";                         // 用户名
  13.         private String dbpassWord = "abc123";                         // 密码
  14.         /**
  15.          * 获取数据库连接
  16.          */
  17.         public Connection getCon()throws Exception{
  18.                 Class.forName(jdbcName);
  19.                 Connection con=DriverManager.getConnection(dbUrl, dbuserName, dbpassWord);
  20.                 return con;
  21.         }
  22.         /**
  23.          * 关闭数据库连接
  24.          */
  25.         public void closeCon(Connection con)throws Exception{
  26.                 if(con!=null){
  27.                         con.close();
  28.                 }
  29.         }
  30.         public static void main(String[] args) {
  31.                 DbUtil dbUtil=new DbUtil();
  32.                 try {
  33.                         dbUtil.getCon();
  34.                         System.out.println("数据库连接成功!");
  35.                 } catch (Exception e) {
  36.                         // TODO Auto-generated catch block
  37.                         e.printStackTrace();
  38.                         System.out.println("数据库连接失败");
  39.                 }
  40.         }
  41. }
复制代码
6.总结

两周的课程设计圆满结束,在整个项目搭建的过程中也解决了很多的问题,对整体的思路有了把握,进一步理解了Java数据库编程,GUI图形用户界面,JDBC技术。虽然项目设计的时间匆忙,但是Java学习任重而道远,一个人走的更快,一群人走的更远,共勉!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

莫张周刘王

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表