模拟实现以下商品管理业务系统,商品管理系统的整体功能有:
注:商品数据存储在一个数据库中(用静态HashMap来模拟),MySQL
 
1、用户可以通过输入商品信息来向库中添加商品,如:
idname,price,库存数量  p01,透明胶带,8.8,1000 
2、用户可以查看数据库中所有的商品
3、用户可以通过输入一个id来查看一个商品
4、用户可以通过输入一个name来查看一个商品
5、用户可以通过输入一个价格范围(比如: 100->200)来查看符合条件的商品
6、用户可以通过输入一个id来从数据库中删除一个商品
额外添加的需求:
7、用户可以通过指定一个id来修改一个商品的名称
8、用户可以通过制定一个id来修改一个商品的价格
9、用户可以通过制定一个id来修改一个商品的库存数量
要求:
a、整个程序要分成:交互层、 业务逻辑(service)层、 数据访问(dao)层
b、交互层和service层之间要用“面向接口编程”的模式来写
c、业务逻辑层和dao层之间要用“面向接口编程”的模式来写
注:交互层打印菜单的过程很麻烦,可以不写交互层,只要完成service层的接口及实现类, dao层的接口及实现类即可。
 
需要完成这个项目,不仅需要有一定的Java基础知识,还需要掌握一定的数据库的操作和Java GUI的设计,对于这方面内容,推荐读者阅读《MySql必知必会》这本书入门数据库的基本操作。在构建项目的时候还需要勤查百度和相关文档。为了让没有相关基础的读者快速掌握关于该项目中的一些简单操作,在此推荐B站中的三个视频供大家参考: 
Java图像界面编程:Java图形界面编程  
MySQL数据库SQL语言入门:MySQL数据库SQL语言入门  
Java语言连接操作MySQL数据库,JDBC技术:Java语言连接操作MySQL数据库,JDBC技术  
当然,视频的作用只是辅助快速入门,关键还是需要多查询文档和百度。 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 package  service;import  java.sql.*;import  java.util.ArrayList;import  java.util.List;public  class  getData  public  static   List<Object> search (String My_id)  new  ArrayList<Object>();null ;null ;null ;null ;try  {"com.mysql.cj.jdbc.Driver" );try  {"jdbc:mysql://localhost:3306/goods" ,"root" ,"1234" );"select * from goods where id =? or name = ?" ;1 , My_id);2 , My_id);int  i = 0 ;while (resultset.next()){"id" );"name" );int  price = resultset.getInt("price" );int  num = resultset.getInt("num" );catch  (SQLException e) {catch  (ClassNotFoundException e) {finally {return  list;public  static  void  close (AutoCloseable autoCloseable) if (autoCloseable != null ){try catch  (Exception e) {
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 package  service;import  java.sql.*;public  class  addData  public  static  void  addData (String id,String name,int  price,int  num)  null ;null ;null ;null ;try  {"com.mysql.cj.jdbc.Driver" );try  {"jdbc:mysql://localhost:3306/goods" ,"root" ,"1234" );"select * from goods" );"INSERT INTO goods VALUES(?,?,?,?)" ;1 , id);2 , name);3 , price);4 , num);"添加" +id+"数据成功!" );catch  (SQLException e) {catch  (ClassNotFoundException e) {finally {public  static  void  close (AutoCloseable autoCloseable) if (autoCloseable != null ){try catch  (Exception e) {
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 package  gui.service;import  java.sql.*;import  java.util.Objects;import  java.util.Scanner;public  class  changeData  public  static  void  change (String My_id,String My_name ,int  My_price,int  My_num)  null ;null ;null ;null ;try  {"com.mysql.cj.jdbc.Driver" );try  {"jdbc:mysql://localhost:3306/goods" ,"root" ,"1234" );"UPDATE goods SET " ;"name = ? " ;",price = ? ," ;"num = ?" ;" WHERE id=?" ;1 , My_name);2 , My_price);3 , My_num);4 , My_id);catch  (SQLException e) {catch  (ClassNotFoundException e) {finally {public  static  void  close (AutoCloseable autoCloseable) if (autoCloseable != null ){try catch  (Exception e) {
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 package  gui.service;import  java.sql.*;public  class  delData  public  static  void  del_data (String My_id)  null ;null ;null ;null ;try  {"com.mysql.cj.jdbc.Driver" );try  {"jdbc:mysql://localhost:3306/goods" ,"root" ,"1234" );"select * from goods" );"DELETE FROM goods WHERE id=?" ;1 ,My_id);"删除" +My_id+"数据成功!" );catch  (SQLException e) {catch  (ClassNotFoundException e) {finally {public  static  void  close (AutoCloseable autoCloseable) if (autoCloseable != null ){try catch  (Exception e) {
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 package  gui.service;import  java.sql.*;import  java.util.ArrayList;import  java.util.List;import  java.util.Scanner;public  class  chooseData  public  static  List<Object> search (String My_data)  new  ArrayList<Object>();null ;null ;null ;null ;try  {"com.mysql.cj.jdbc.Driver" );try  {"jdbc:mysql://localhost:3306/goods" ,"root" ,"1234" );"select * from goods where id =? or name = ?" ;1 , My_data);2 , My_data);int  i = 0 ;while (resultset.next()){"id" );"name" );int  price = resultset.getInt("price" );int  num = resultset.getInt("num" );catch  (SQLException e) {catch  (ClassNotFoundException e) {finally {return  list;public  static  void  close (AutoCloseable autoCloseable) if (autoCloseable != null ){try catch  (Exception e) {
该层主要采用Swing类 的各种组件和方法实现图形用户界面,并且下载了相应的主题优化了组件的外观,使其达到精美的效果!
 
使用JFrame 作为面板组件的载体。
 
使用JPanel 组件作为基础组件的载体,并且设置面板的大小,位置以及布局方式。
 
使用JBox 打包对应组件为一个完整的整体,并确定竖直和横向的排布。
 
使用JLabel、JTextField、JButton、JPassword、JSplit 等基本组件并且设置其格式 ,包括颜色 、大小 、位置 等。
 
因为需要为交互的组件添加监视器 来实现动态响应 ,最终组合JFrame 的内容为总体系统。
 
 
由于本项目代码量巨大,详细工程就不展示在本博客中,同时为了促进大家相互学习和共同进步,在此提供公开版项目供大家使用,一起进步!
 
公开版项目是精简了一些冗余的外观,尽量保留原汁原味JavaGUI,更加关注后台数据处理和交互的过程!读者可以根据初始GUI进行深度定制,随时随地制作出合你心意的作品。鉴于本人水平有限,本项目依然有很多不足之处,欢迎大家在下方评论区留言指正!
 
本公开版项目能够直接运行Main.java即可,若连接数据库,读者需要首先自行在本机安装MySql并将相关内容在service文件夹即业务逻辑层修改部分代码才可完美运行。
 
本项目所有功能已经测试通过,如仍然有Bug欢迎指正!
 
下载连接:公开版项目点此下载