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 */ public class FruitDAOimpl implements FruitDAO { 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 catch public List<Fruit> getFruitList() { List<Fruit> fruitList=new ArrayList<>(); //注册驱动 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 = new Fruit(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; } //添加 @Override public boolean addFruit(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(); } } return false; } //修改 @Override public boolean updateFruit(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(); } } return false; } @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); return new Fruit(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; } //删除 @Override public boolean delFruit(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(); } } return false; } }
3.5Menu类
这里就是一些实现方法
下面就是一些具体方法实现
3.5.1showMainMenu()显示主菜单
public int showMainMenu(){ 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()查看特定水果信息
public void showFruitInfo(){ 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()水果下架
public void delFruit(){ 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()查看水果列表;
public void showFruitList() { 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()添加水果库存信息
添加水果库存信息----有添加也有修改 ----叫做业务方法
public void addFruit(){ 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 = new Fruit(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()退出
public boolean exit() { System.out.print("是否确认退出?(Y/N)"); while(true){ String next = scanner.next(); switch(next){ case "y": case "Y": return false; case "N": case "n": return true; default: System.out.println("选择错误"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("请重新选择!!!"); } } } }
上面的代码为啥异常不直接抛出???
子类重写父类方法时抛出的异常不能大于父类异常所以用try/catch