前言:
👏作者简介:我是笑霸final,一名热爱技术的在校学生。
📝个人主页:笑霸final的主页
📕系列专栏 : java专栏、项目专栏
🐉本项目用的技术栈:java(jdbc)+mysql
🐅工具:idea+jdk1.8+mysql
📧如果文章知识点有错误的地方,请指正!和大家一起学习,一起进步👀
🔥如果感觉博主的文章还不错的话,👍点赞👍 + 👀关注👀 + 🤏收藏🤏
代做可私聊
水果库存系统1.0
一:水果库存系统简介:
本文适合初学 mysql+jdbc的同学!!本水果管理系统为1.0版本后续版本请订阅系列专栏
简介:通过java实现水果的增删改查。最后能过读取和写入MySQL。
截图:
二:前置知识
专栏:java专栏
知识:jdbc实现增删改查链接、mysql的安装、
三结构说明
本项目结构图如下:配置请看前置知识的mysql安装
3.1Client类
此类是整个项目的入口,不多说直接上代码!!
package com.fianl_.fruit.view;import com.fianl_.fruit.controller.Menu;/**
* @autor 笑霸fianl~
* 欢迎访问GitHub:https://github.com/XBfinal
* 欢迎访问Gitee:https://gitee.com/XBfianl
* 欢迎访问CSDN:https://blog.csdn.net/weixin_52062043
*/publicclassClient{publicstaticvoidmain(String[] args)throws InterruptedException, ClassNotFoundException {
Menu menu =newMenu();boolean flge=true;while(flge){int i = menu.showMainMenu();;switch(i){case1:menu.showFruitList();//显示break;case2:
menu.addFruit();//添加break;case3:menu.showFruitInfo();//显示特定水果信息break;case4:menu.delFruit();//下架水果break;case5:flge=menu.exit();//退出break;default:
System.out.println("选择错误请重新选择!!!!");
Thread.sleep(1000);break;}}}}
一些代码的说明
Thread.sleep(1000);让此线程停止1秒 详情请看:Java并发之线程入门一
Menu menu = new Menu();这个类有很多常用的共能。增删改查的具体实现方法都在这里
在说 Menu类之前我们先来创建水果类fruit
3.2fruit类
这里定义了 水果的名字、id、价格、库存、备注。和一些基本的方法。
package com.fianl_.fruit.pojo;publicclassFruit{private Integer fid;//编号private String fname;//水果名字private Integer price;//水果价格private Integer fcount;//水果库存private String remark;//水果的备注publicFruit(Integer fid, String fname, Integer price, Integer fcount, String remark){this.fid = fid;this.fname = fname;this.price = price;this.fcount = fcount;this.remark = remark;}publicvoidFormat_display(){
System.out.printf("%-5d\t%-5s\t%-5d\t%-5d\t%-5s",fid,fname,price,fcount,remark);
System.out.println();//换行}public String toString(){return fid+"\t\t"+fname+"\t\t"+price+"\t\t"+fcount+"\t\t"+remark;}public Integer getFid(){return fid;}public String getFname(){return fname;}public Integer getPrice(){return price;}public Integer getFcount(){return fcount;}public String getRemark(){return remark;}publicvoidsetFid(Integer fid){this.fid = fid;}publicvoidsetFname(String fname){this.fname = fname;}publicvoidsetPrice(Integer price){this.price = price;}publicvoidsetFcount(Integer fcount){this.fcount = fcount;}publicvoidsetRemark(String remark){this.remark = remark;}}
温馨提示:
Java中类(格式化输出)格式和C语言一样,本项目为了解决读取mysql数据输出文字对齐问题。
System.out.printf("%-5d\t%-5s\t%-5d\t%-5d\t%-5s",fid,fname,price,fcount,remark);
System.out.println();//换行
3.3FruitDAO接口
DAO设计模式简介: DAO设计模式可以减少代码量,增强程序的可移植性,提高代码的可读性。 DAO (数据库操作对象)设计模式是 JavaEE 数据层的操作.主要由五部分组成: 1.数据库连接类:连接数据库并获取连接对象。 2.VO实体类:包含属性和表中字段完全对应的类。
package com.fianl_.fruit.dao;import com.fianl_.fruit.pojo.Fruit;import java.util.List;publicinterfaceFruitDAO{//显示库存列表
List<Fruit>getFruitList()throws ClassNotFoundException;//新增库存booleanaddFruit(Fruit fruit);//修改库存booleanupdateFruit(Fruit fruit);//根据名称查询
Fruit getFruitByFname(String fname);//根据名称删除记录booleandelFruit(String fname);}
3.4FruitDAOimpl类实现FruitDAO
就是把FruitDAO里面的方法写出来
package com.fianl_.fruit.dao.impl;import com.fianl_.fruit.dao.FruitDAO;import com.fianl_.fruit.pojo.Fruit;import com.sun.org.apache.bcel.internal.generic.ACONST_NULL;import com.sun.org.apache.xerces.internal.dom.DeferredElementImpl;import java.sql.*;import java.util.ArrayList;import java.util.List;/**
* @autor 笑霸fianl~
* 欢迎访问GitHub:https://github.com/XBfinal
* 欢迎访问Gitee:https://gitee.com/XBfianl
* 欢迎访问CSDN:https://blog.csdn.net/weixin_52062043
*/publicclassFruitDAOimplimplementsFruitDAO{
PreparedStatement psmt=null;
Connection connection=null;
ResultSet resultSet=null;final String DRIVER ="com.mysql.jdbc.Driver";final String URL ="jdbc:mysql://localhost:3306/fruitdb?useUnicode=true&characterEncoding=UTF-8&useSSL=false";final String USER ="root";final String password ="0615";//查询操作@Override//访问修饰符不能减小,异常不能扩大所以throws ClassNotFoundException不用。我们用try catchpublic List<Fruit>getFruitList(){
List<Fruit> fruitList=newArrayList<>();//注册驱动try{
Class.forName(DRIVER);}catch(ClassNotFoundException e){
e.printStackTrace();}//获取链接;try{
connection = DriverManager.getConnection(URL,USER,password);//编写sql语句和创建PreparedStatement对象
String sql="select * from t_fruit";
psmt = connection.prepareStatement(sql);//查询处理结果集
resultSet = psmt.executeQuery();while(resultSet.next()){//表有五liefenbie是int fid=resultSet.getInt(1);
String fname = resultSet.getString("fname");int price = resultSet.getInt(3);int fcount = resultSet.getInt(4);
String remark = resultSet.getString("remark");
Fruit fruit =newFruit(fid, fname, price, fcount, remark);
fruitList.add(fruit);}}catch(SQLException e){
e.printStackTrace();}finally{//表示无论如何都要执行的代码//释放资源try{
resultSet.close();
psmt.close();
connection.close();}catch(SQLException e){
e.printStackTrace();}}return fruitList;}//添加@OverridepublicbooleanaddFruit(Fruit fruit){try{
Class.forName(DRIVER);
connection = DriverManager.getConnection(URL, USER, password);
String sql="insert into t_fruit values(0,?,?,?,?);";
psmt = connection.prepareStatement(sql);
psmt.setString(1,fruit.getFname());
psmt.setInt(2,fruit.getPrice());
psmt.setInt(3,fruit.getFcount());
psmt.setString(4, fruit.getRemark());return psmt.executeUpdate()>0;}catch(ClassNotFoundException e){
e.printStackTrace();}catch(SQLException e){
e.printStackTrace();}finally{try{
psmt.close();
connection.close();}catch(SQLException e){
e.printStackTrace();}}returnfalse;}//修改@OverridepublicbooleanupdateFruit(Fruit fruit){try{
Class.forName(DRIVER);
connection = DriverManager.getConnection(URL, USER, password);
String sql="update t_fruit set fcount=? where fid=?";
psmt=connection.prepareStatement(sql);
psmt.setInt(1, fruit.getFcount());
psmt.setInt(2,fruit.getFid());return psmt.executeUpdate()>0;}catch(ClassNotFoundException e){
e.printStackTrace();}catch(SQLException e){
e.printStackTrace();}finally{try{
psmt.close();
connection.close();}catch(SQLException e){
e.printStackTrace();}}returnfalse;}@Override//根据名称查询public Fruit getFruitByFname(String fname){try{
Class.forName(DRIVER);
connection = DriverManager.getConnection(URL, USER, password);
String sql="select * from t_fruit where fname like ?";
psmt=connection.prepareStatement(sql);
psmt.setString(1,fname);
resultSet=psmt.executeQuery();if(resultSet.next()){int fid=resultSet.getInt(1);int price=resultSet.getInt(3);int fcount=resultSet.getInt(4);
String remark = resultSet.getString(5);returnnewFruit(fid,fname,price,fcount,remark);}}catch(ClassNotFoundException e){
e.printStackTrace();}catch(SQLException e){
e.printStackTrace();}finally{try{
resultSet.close();
psmt.close();
connection.close();}catch(SQLException e){
e.printStackTrace();}}return null;}//删除@OverridepublicbooleandelFruit(String fname){try{
Class.forName(DRIVER);
connection = DriverManager.getConnection(URL, USER, password);
String sql="delete from t_fruit where fname like ?";
psmt = connection.prepareStatement(sql);
psmt.setString(1,fname);return psmt.executeUpdate()>0;}catch(ClassNotFoundException e){
e.printStackTrace();}catch(SQLException e){
e.printStackTrace();}finally{try{
psmt.close();
connection.close();}catch(SQLException e){
e.printStackTrace();}}returnfalse;}}
知识点:
1.异常处理: //访问修饰符不能减小,异常不能扩大所以throws ClassNotFoundException不用。我们用try catch
2.链接数据库操作步骤:jdbc链接数据库、jdbc实现增删改查链接、
3.5Menu类
这里就是一些实现方法
下面就是一些具体方法实现
3.5.1showMainMenu()显示主菜单
publicintshowMainMenu(){
System.out.println("============欢迎使用水果库存系统============");
System.out.println("1.查看水果库存列表");
System.out.println("2.添加水果库存容量");
System.out.println("3.查看特定水果库存信息");
System.out.println("4.水果下架");
System.out.println("5.退出");
System.out.println("=========================================");
System.out.print("请选择:\t");int i = scanner.nextInt();return i;}
3.5.2showFruitInfo()查看特定水果信息
publicvoidshowFruitInfo(){
System.out.print("请输入水果名称:");
String fname = scanner.next();
Fruit f = fruitDAO.getFruitByFname(fname);if(f==null){
System.out.println("对不起,没有对应的记录");}else{
System.out.println("=================================");
System.out.println("编号\t\t名称\t\t单价\t\t库存\t\t备注");//System.out.println(f);//默认调用toString()方法。
f.Format_display();
System.out.println("=================================");}}
3.5.3delFruit()水果下架
publicvoiddelFruit(){
System.out.print("请输入水果名称:");
String fname = scanner.next();
Fruit fruit = fruitDAO.getFruitByFname(fname);if(fruit==null){
System.out.println("对不起,没有找到需要下架的水果信息!");}else{
System.out.print("是否确认下架?(Y/N)");
String STR = scanner.next();if("y".equalsIgnoreCase(STR)||"Y".equalsIgnoreCase(STR)){
fruitDAO.delFruit(fname);
System.out.println("下架成功!");try{
Thread.sleep(1000);}catch(InterruptedException e){
e.printStackTrace();}}}}
3.5.4showFruitList()查看水果列表;
publicvoidshowFruitList(){
List<Fruit> fruitList= null;try{
fruitList = fruitDAO.getFruitList();}catch(ClassNotFoundException e){
e.printStackTrace();}
System.out.println("=================================");
System.out.println("编号\t\t名称\t\t单价\t\t库存\t\t备注");
System.out.println("=================================");if(fruitList==null||fruitList.size()<=0){
System.out.println("没有任何数据");}else{for(int i=0;i<fruitList.size();++i){//集合获取元素可以get也可以迭代器
Fruit fruit=fruitList.get(i);/*
System.out.println(fruit.getFid()+"\t\t"
+fruit.getFname()+"\t\t"
+fruit.getPrice()+"\t\t"
+fruit.getFcount()+"\t\t"
+fruit.getRemark());*///System.out.println(fruit);//有数据是不是要调用同String?所以我们重写这个方法
fruit.Format_display();}}}
3.5.5addFruit()添加水果库存信息
添加水果库存信息----有添加也有修改 ----叫做业务方法
publicvoidaddFruit(){
System.out.print("请输入水果名称:");
String fname=scanner.next();
Fruit fruit = fruitDAO.getFruitByFname(fname);if(fruit == null){//说明库存没有此水果,那就是添加
System.out.print("请输入单价:");int price = scanner.nextInt();
System.out.print("请输入库存量:");int fcount = scanner.nextInt();
System.out.println("请输入水果备注:");
String remark = scanner.next();//封装成一个新的对象
fruit =newFruit(0,fname,price,fcount,remark);//调用DAO的添加方法boolean b = fruitDAO.addFruit(fruit);if(b){
System.out.println("添加成功!");try{
Thread.sleep(1000);}catch(InterruptedException e){
e.printStackTrace();}}}else{//有就修改
System.out.print("请输添加的库存量:");int fcount = scanner.nextInt();
fruit.setFcount(fruit.getFcount()+fcount);//追加库存是原来加现在加入的//调用DAO的修改方法
fruitDAO.updateFruit(fruit);}}
3.5.6exit()退出
publicbooleanexit(){
System.out.print("是否确认退出?(Y/N)");while(true){
String next = scanner.next();switch(next){case"y":case"Y":returnfalse;case"N":case"n":returntrue;default:
System.out.println("选择错误");try{
Thread.sleep(1000);}catch(InterruptedException e){
e.printStackTrace();}
System.out.println("请重新选择!!!");}}}}
上面的代码为啥异常不直接抛出???
子类重写父类方法时抛出的异常不能大于父类异常所以用try/catch
版权归原作者 笑霸final 所有, 如有侵权,请联系我们删除。