700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > MYSQL-JDBC数据库实战-宠物商店

MYSQL-JDBC数据库实战-宠物商店

时间:2024-01-24 22:59:36

相关推荐

MYSQL-JDBC数据库实战-宠物商店

dao/AccountDao

package com.xk.jdbc.dao;public interface AccountDao {/*** 增加账单**/int addCount(Integer deal_type, Integer pet_id,Integer buyer_id,Integer seller_id,Integer price);}

dao/BaseDao

package com.xk.jdbc.dao;import mons.beanutils.BeanUtils;import java.io.IOException;import java.io.InputStream;import java.lang.reflect.InvocationTargetException;import java.sql.*;import java.util.ArrayList;import java.util.List;import java.util.Properties;public class BaseDao {private static String driver;private static String url;private static String user;private static String password;private static Connection connection;private static Statement sm;private static PreparedStatement pst;private static ResultSet rs;static {init();try {Class.forName(driver);} catch (ClassNotFoundException e) {e.printStackTrace();}}public static void init(){//创建Propeities对象 保存 配置文件中的信息Properties properties = new Properties();//加载配置文件到输入流中InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("database.properties");//执行properties的load方法,加载配置文件try {properties.load(is);} catch (IOException e) {e.printStackTrace();}driver = properties.getProperty("driver");url = properties.getProperty("url");user = properties.getProperty("user");password = properties.getProperty("password");}public static Connection getconnection(){try {connection = DriverManager.getConnection(url,user,password);} catch (SQLException e) {e.printStackTrace();}return connection;}public static <T> List<T> executeQuery(String sql, List list,Class<T> Clazz){getconnection();List<T> tList = new ArrayList<>();try {//通过prepareStatement预编译sqlpst = connection.prepareStatement(sql);//判断list中是否存在 需要进入sql语句的参数if(list != null){//通过循环将参数载入prepareStatement中for (int i = 0; i <list.size(); i++) {pst.setObject((i+1),list.get(i));}}rs = pst.executeQuery();//获取查询后的虚拟结果集的结构信息ResultSetMetaData rsmd = rs.getMetaData();while(rs.next()){//实例化对象(clazz是什么类型 obj就是什么类型)T obj = Clazz.newInstance();for (int i = 0; i <rsmd.getColumnCount() ; i++) {//获取数据库列名String colName = rsmd.getColumnName(i+1);//获取数据库值Object value = rs.getObject(colName);//调用导入的包下的方法,传入对象,列名,值,会拿列名与对象中的属性对比找到与列名相同名的属性,将值赋给该对象的属性BeanUtils.setProperty(obj,colName,value);}tList.add(obj);}} catch (Exception e) {e.printStackTrace();} finally {closeAll();}return tList;}public static ResultSet showExecuteQuery(String sql) {getconnection();try {// 通过 prepareStatement 预编译SQLsm = connection.createStatement();rs = sm.executeQuery(sql);} catch (SQLException throwables) {throwables.printStackTrace();}return rs;}public static int executeUpdate(String sql,List list){getconnection();int row = 0;try {connection.setAutoCommit(false);//通过prepareStatement预编译sqlpst = connection.prepareStatement(sql);//判断list集合中是否存在 需要进入sql语句的参数if (null != list && list.size() != 0){//通过循环 将参数写入prepareStatement中for (int i = 0; i <list.size() ; i++) {pst.setObject((i+1),list.get(i));}}row = pst.executeUpdate();mit();} catch (SQLException throwables) {throwables.printStackTrace();try {connection.rollback();}catch (SQLException e) {e.printStackTrace();}} finally {closeAll();}return row;}public static void closeAll(){if (rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (pst != null) {try {pst.close();} catch (SQLException e) {e.printStackTrace();}}if (connection != null){try {connection.close();} catch (SQLException e) {e.printStackTrace();}}}}

dao/PetDao

package com.xk.jdbc.dao;import com.xk.jdbc.entity.Pet;import java.util.List;public interface PetDao {/*** 查询所有的宠物*/List<Pet> queryPetList(Integer pet_id,String type_name,Integer health,Integer love,String birthday,Integer owner_id,Integer store_id,Integer pet_money,int sell);/*** 修改宠物信息*/int revisePet(String name, String whatinformation,Integer value,String id,Integer number);/*** 增加宠物*/int addPet(String petType,String name,Integer health,Integer love,Integer store_id,Integer pet_money);}

dao/PetMasterDao

package com.xk.jdbc.dao;import com.xk.jdbc.entity.PetMaster;import java.util.List;public interface PetMasterDao {/*** 查询所有的宠物主人*/List<PetMaster> queryPetMasterList(Integer owner_id,String masterName,String masterPwd,Integer masterMoney);}

dao/PetShopDao

package com.xk.jdbc.dao;import com.xk.jdbc.entity.PetShop;import java.util.List;public interface PetShopDao {/*** 查询所有的宠物商店*/List<PetShop> queryPetShopList( Integer store_id,String name,String passwor,Integer balance);/*** 开宠物商店*/int openShop(String name,String passwd,Integer money);}

dao/impl/Accountimpl

package com.xk.jdbc.dao.impl;import com.xk.jdbc.dao.AccountDao;import com.xk.jdbc.dao.BaseDao;import java.util.ArrayList;import java.util.List;public class Accountimpl implements AccountDao {@Overridepublic int addCount(Integer deal_type, Integer pet_id,Integer buyer_id,Integer seller_id,Integer price) {List list = new ArrayList();String sql = "INSERT into account(deal_type,pet_id,buyer_id,seller_id,price) VALUES (?,?,?,?,?)";list.add(deal_type);list.add(pet_id);list.add(buyer_id);list.add(seller_id);list.add(price);int index = BaseDao.executeUpdate(sql,list);return 0;}}

dao/impl/PetDaoimpl

package com.xk.jdbc.dao.impl;import com.xk.jdbc.dao.BaseDao;import com.xk.jdbc.dao.PetDao;import com.xk.jdbc.entity.Pet;import java.util.ArrayList;import java.util.List;public class PetDaoimpl implements PetDao {/*** 查询所有的宠物*/@Overridepublic List<Pet> queryPetList(Integer pet_id, String type_name, Integer health, Integer love, String birthday,Integer owner_id, Integer store_id, Integer pet_money,int sell) {List list = new ArrayList();StringBuffer sql = new StringBuffer();sql.append("select pet.*, ");sql.append("petmaster.masterName, ");sql.append("from pet ");sql.append("right JOIN petmaster ");sql.append("on petmaster.owner_id = pet.owner_id ");sql.append("where 1=1 ");if (null != pet_id){sql.append("and pet_id = ?");list.add(pet_id);}if (null != type_name){sql.append("and type_name like ?");list.add("%"+type_name+"%");}if (null != pet_id){sql.append("and health = ?");list.add(health);}if (null != pet_id){sql.append("and love = ?");list.add(love);}if (null != pet_id){sql.append("and birthday like ?");list.add("%"+birthday+"%");}if (null != pet_id){sql.append("and owner_id = ?");list.add(owner_id);}if (null != pet_id){sql.append("and store_id = ?");list.add(store_id);}if (null != pet_id){sql.append("and pet_money = ?");list.add(pet_money);}List<Pet> petlist = BaseDao.executeQuery(sql.toString(),list,Pet.class);return petlist;}/*** 修改宠物信息*/@Overridepublic int revisePet(String name, String whatinformation,Integer value,String id,Integer number) {List<Object> list = new ArrayList();int index = 0;List<String> list1 = new ArrayList<>();//"pet","Owner_id",petMaster.getOwner_id(),"Pet_id",oldPet.get(number1-1).getPet_id());if (name.equalsIgnoreCase("petMaster") && id.equalsIgnoreCase("Owner_id")){//"petMaster","masterMoney",(petMaster.getMasterMoney()-oldPet.get(number1-1).getPet_money()),"Owner_id"list1.add("UPDATE petMaster set masterMoney = ? where Owner_id = ?");}else if(name.equalsIgnoreCase("petShop") && id.equalsIgnoreCase("Store_id")){list1.add("UPDATE petShop set balance = ? where Store_id = ?");}else if(whatinformation.equalsIgnoreCase("Store_id") ){list1.add("UPDATE pet set Store_id = ? where pet_id = ?");} else {list1.add("UPDATE pet set Owner_id = ? where pet_id = ? ");}String sql = list1.get(0);list.add(value);list.add(number);index = BaseDao.executeUpdate(sql,list);return index;}/*** 增加宠物*/@Overridepublic int addPet(String petType, String name, Integer health, Integer love, Integer store_id,Integer pet_money) {List list = new ArrayList();String sql = "INSERT into pet(`name`,type_name,health,love,store_id,pet_money) VALUES (?,?,?,?,?,?)";list.add(name);list.add(petType);list.add(health);list.add(love);list.add(store_id);list.add(pet_money);int index = BaseDao.executeUpdate(sql,list);return 0;}}

dao/impl/PetMasterDaoimpl

package com.xk.jdbc.dao.impl;import com.xk.jdbc.dao.BaseDao;import com.xk.jdbc.dao.PetMasterDao;import com.xk.jdbc.entity.Pet;import com.xk.jdbc.entity.PetMaster;import java.util.ArrayList;import java.util.List;public class PetMasterDaoimpl implements PetMasterDao {/*** 查询所有的宠物主人*/@Overridepublic List<PetMaster> queryPetMasterList(Integer owner_id, String masterName, String masterPwd, Integer masterMoney) {List list = new ArrayList();StringBuffer sql = new StringBuffer();sql.append("select * ");sql.append("from petMaster ");sql.append("where 1=1 ");if (null != owner_id){sql.append("and owner_id = ?");list.add(owner_id);}if (null != masterName){sql.append("and masterName like ?");list.add("%"+masterName+"%");}if (null != masterPwd){sql.append("and masterPwd like ?");list.add("%"+masterPwd+"%");}if (null != masterMoney){sql.append("and masterMoney = ?");list.add(masterMoney);}List<PetMaster> petMasterlist = BaseDao.executeQuery(sql.toString(),list,PetMaster.class);return petMasterlist;}}

dao/impl/PetShopDaoimpl

package com.xk.jdbc.dao.impl;import com.xk.jdbc.dao.BaseDao;import com.xk.jdbc.dao.PetShopDao;import com.xk.jdbc.entity.PetMaster;import com.xk.jdbc.entity.PetShop;import java.util.ArrayList;import java.util.List;public class PetShopDaoImpl implements PetShopDao {/*** 查询所有的宠物商店*/@Overridepublic List<PetShop> queryPetShopList(Integer store_id, String name, String passwor, Integer balance) {List list = new ArrayList();StringBuffer sql = new StringBuffer();sql.append("select * ");sql.append("from petShop ");sql.append("where 1=1 ");if (null != store_id){sql.append("and store_id = ?");list.add(store_id);}if (null != name){sql.append("and name like ?");list.add("%"+name+"%");}if (null != passwor){sql.append("and passwor like ?");list.add("%"+passwor+"%");}if (null != balance){sql.append("and balance = ?");list.add(balance);}List<PetShop> petShopList = BaseDao.executeQuery(sql.toString(),list,PetShop.class);return petShopList;}/*** 开宠物商店*/@Overridepublic int openShop(String name, String passwd, Integer money) {List list = new ArrayList();String sql = "INSERT into petshop(name,passwor,balance) VALUES (?,?,?)";list.add(name);list.add(passwd);list.add(money);int index = BaseDao.executeUpdate(sql,list);return 0;}}

demoa/test1

package com.xk.jdbc.demoa;import com.xk.jdbc.entity.Pet;import com.xk.jdbc.entity.PetMaster;import com.xk.jdbc.service.PetService;import com.xk.jdbc.service.impl.PetMasterimpl;import com.xk.jdbc.service.impl.PetShopimpl;import com.xk.jdbc.service.impl.Petimpl;import java.util.List;import java.util.Scanner;public class Test1 {public static void main(String[] args) {//声明变量 保存主人登录信息System.out.println("宠物商店启动\nWonderland醒来,所有宠物从Mysql中醒来\n*****************************************");System.out.println("序号 "+"宠物名称");new Petimpl().showAllPet();System.out.println("*****************************************\n");System.out.println("所有宠物主人从Mysql中醒来");System.out.println("序号 "+"主人姓名");new PetMasterimpl().showPetMaster();System.out.println("*****************************************\n");System.out.println("所有宠物商店从Mysql中醒来");System.out.println("序号 "+"宠物商店名称");new PetShopimpl().showPetShop();System.out.println("*****************************************\n");boolean judge = true;do {Scanner input = new Scanner(System.in);System.out.println("请选择输入登陆模式,输入1为宠物主人登录,输入2为宠物商店登录,输入其他退出系统");switch (input.nextInt()) {case 1:System.out.println("请先登录,请您输入主人的名字");String mastername = input.next();System.out.println("请您输入主人的密码:");String masterpassword = input.next();PetMaster petMaster = new PetMasterimpl().masterLogin(mastername, masterpassword);if (petMaster == null) {System.out.println("账号或密码错误,请重输");break;} else {boolean close = true;do {System.out.println("登陆成功,您可以购买和卖出宠物,如果您想购买宠物请输入1,如果想卖出宠物请输入2(输入其他退出)");System.out.println("1,购买宠物\n2,卖出宠物");int input1 = input.nextInt();switch (input1) {case 1:new PetMasterimpl().petbuy(mastername);break;case 2:new PetMasterimpl().petSell(mastername);break;default:close = false;break;}} while (close);}break;case 2:boolean isok = true;do {System.out.println("请先登录,请您输入宠物商店的名字");String shopname = input.next();System.out.println("请您输入宠物商店的密码:");String shoppassword = input.next();boolean isnot1 = new PetShopimpl().petShopLogin(shopname, shoppassword);boolean close2 = true;if (!isnot1) {System.out.println("账号或密码错误,请重输");continue;} else {do {System.out.println("登陆成功,可以进行如下操作");System.out.println("1.购买宠物\n2.卖出宠物\n3.培育宠物\n4.查看代售宠物\n5.查看商店结余\n6.查看商店账目\n7.开宠物上商店");System.out.println("请根据需要执行的操作,选择序号,输入0回到主页面");int number3 = input.nextInt();switch (number3) {case 1:new PetShopimpl().buyPet(shopname);break;case 2:new PetShopimpl().sellPet(shopname);break;case 3:new PetShopimpl().nurturePet(shopname);break;case 4:new PetShopimpl().inquirePet();break;case 5:new PetShopimpl().checkBalance(shopname);break;case 6:new PetShopimpl().checkCount(shopname);break;case 7:new PetShopimpl().openShop(shopname);break;default:close2 = false;isok = false;break;}}while (close2);}} while (isok);break;default:judge = false;System.out.println("欢迎您下次再光顾本系统!");break;}} while (judge);}}

entity/Account

package com.xk.jdbc.entity;public class Account {private Integer accountId;private Integer deal_type ;private Integer pet_id ;private Integer buyer_id ;private Integer seller_id ;private Integer price;private String deal_time ;public Account() {}public Account(Integer accountId, Integer deal_type, Integer pet_id, Integer buyer_id, Integer seller_id, Integer price, String deal_time) {this.accountId = accountId;this.deal_type = deal_type;this.pet_id = pet_id;this.buyer_id = buyer_id;this.seller_id = seller_id;this.price = price;this.deal_time = deal_time;}public Integer getAccountId() {return accountId;}public void setAccountId(Integer accountId) {this.accountId = accountId;}public Integer getDeal_type() {return deal_type;}public void setDeal_type(Integer deal_type) {this.deal_type = deal_type;}public Integer getPet_id() {return pet_id;}public void setPet_id(Integer pet_id) {this.pet_id = pet_id;}public Integer getBuyer_id() {return buyer_id;}public void setBuyer_id(Integer buyer_id) {this.buyer_id = buyer_id;}public Integer getSeller_id() {return seller_id;}public void setSeller_id(Integer seller_id) {this.seller_id = seller_id;}public Integer getPrice() {return price;}public void setPrice(Integer price) {this.price = price;}public String getDeal_time() {return deal_time;}public void setDeal_time(String deal_time) {this.deal_time = deal_time;}}

entity/Pet

package com.xk.jdbc.entity;public class Pet {private int pet_id; //宠物idprivate String name;//宠物名字private String type_name;//宠物类型private int health;//健康指数private int love;//爱心指数private String birthday;//出生日期private Integer owner_id;//宠物主人idprivate Integer store_id;//宠物所属商店idprivate int pet_money;//宠物的价值元宝数private int sell;//是否出售,出售为1,否则为0public Pet() {}public Pet(int pet_id, String name, String type_name, int health, int love, String birthday, Integer owner_id, Integer store_id, int pet_money, int sell) {this.pet_id = pet_id;this.name = name;this.type_name = type_name;this.health = health;this.love = love;this.birthday = birthday;this.owner_id = owner_id;this.store_id = store_id;this.pet_money = pet_money;this.sell = sell;}public int getPet_money() {return pet_money;}public void setPet_money(int pet_money) {this.pet_money = pet_money;}public int getSell() {return sell;}public void setSell(int sell) {this.sell = sell;}public int getPet_id() {return pet_id;}public void setPet_id(int pet_id) {this.pet_id = pet_id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getType_name() {return type_name;}public void setType_name(String type_name) {this.type_name = type_name;}public int getHealth() {return health;}public void setHealth(int health) {this.health = health;}public int getLove() {return love;}public void setLove(int love) {this.love = love;}public String getBirthday() {return birthday;}public void setBirthday(String birthday) {this.birthday = birthday;}public Integer getOwner_id() {return owner_id;}public void setOwner_id(Integer owner_id) {this.owner_id = owner_id;}public Integer getStore_id() {return store_id;}public void setStore_id(Integer store_id) {this.store_id = store_id;}public int getMoney() {return pet_money;}public void setMoney(int pet_money) {this.pet_money = pet_money;}}

entity/PetMaster

package com.xk.jdbc.entity;public class PetMaster {private int owner_id;//宠物主人idprivate String masterName;//宠物主人名private String masterPwd;//宠物主人密码private int masterMoney;//宠物主人元宝public PetMaster() {}public PetMaster(int owner_id, String masterName, String masterPwd, int masterMoney) {this.owner_id = owner_id;this.masterName = masterName;this.masterPwd = masterPwd;this.masterMoney = masterMoney;}public int getOwner_id() {return owner_id;}public void setOwner_id(int owner_id) {this.owner_id = owner_id;}public String getMasterName() {return masterName;}public void setMasterName(String masterName) {this.masterName = masterName;}public String getMasterPwd() {return masterPwd;}public void setMasterPwd(String masterPwd) {this.masterPwd = masterPwd;}public int getMasterMoney() {return masterMoney;}public void setMasterMoney(int masterMoney) {this.masterMoney = masterMoney;}}

entity/PetShop

package com.xk.jdbc.entity;public class PetShop {private int store_id;//宠物商店IDprivate String name;//宠物商店名字private String passwor;//宠物商店密码private int balance;//宠物商店结余public PetShop() {}public PetShop(int store_id, String name, String passwor, int balance) {this.store_id = store_id;this.name = name;this.passwor = passwor;this.balance = balance;}public int getStore_id() {return store_id;}public void setStore_id(int store_id) {this.store_id = store_id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPasswor() {return passwor;}public void setPasswor(String passwor) {this.passwor = passwor;}public int getBalance() {return balance;}public void setBalance(int balance) {this.balance = balance;}}

service/AccountService

package com.xk.jdbc.service;public interface AccountService {/*** 展示账单*/void showCount(String name);}

service/PetMasterService

package com.xk.jdbc.service;import com.xk.jdbc.entity.PetMaster;public interface PetMasterService {/*** 通过主人编号查询主人*/PetMaster queryPetMaster_id(Integer owner_id);/*** 通过主人姓名查询主人*/PetMaster queryPetMastername(String mastername);/*** 展示所有宠物主人信息*/void showPetMaster();/***主人登录*/PetMaster masterLogin(String name,String password);/*** 购买宠物*/void petbuy(String name);/*** 卖出宠物*/void petSell(String name);}

service/PetService

package com.xk.jdbc.service;import com.xk.jdbc.entity.Pet;import java.util.List;public interface PetService {/*** 通过输入的宠物商店还是宠物主人来找到其下所有宠物信息*/List<Pet> queryPet_owner_id(String MasterName);/*** 展示所有宠物信息*/void showAllPet();/*** 展示宠物集合宠物信息*/void showPetList(List<Pet> petList);}

service/PetShopService

package com.xk.jdbc.service;import com.xk.jdbc.entity.Pet;import com.xk.jdbc.entity.PetShop;import java.util.List;public interface PetShopService {/*** 通过商店编号查询主人*/PetShop queryPetShop_id(Integer store_id);/*** 通过商店姓名查询商店*/PetShop queryPetShopname(String shopName);/*** 展示所有宠物商店信息*/void showPetShop();/*** 宠物商店登录*/boolean petShopLogin(String shopName,String password);/*** 购买宠物*/void buyPet(String shopName);/*** 卖出宠物*/void sellPet(String shopName);/*** 培育宠物*/void nurturePet(String name);/*** 查询代售宠物*/List<Pet> inquirePet();/*** 查看商店结余*/void checkBalance(String name);/*** 查看商店账目*/void checkCount(String name);/*** 开宠物商店*/void openShop(String name);}

service/impl/PetShopimpl

package com.xk.jdbc.service.impl;import com.xk.jdbc.dao.BaseDao;import com.xk.jdbc.dao.PetShopDao;import com.xk.jdbc.dao.impl.PetDaoimpl;import com.xk.jdbc.dao.impl.PetShopDaoImpl;import com.xk.jdbc.entity.Pet;import com.xk.jdbc.entity.PetMaster;import com.xk.jdbc.entity.PetShop;import com.xk.jdbc.service.PetShopService;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class PetShopimpl implements PetShopService{private PetShopDao petShopDao = new PetShopDaoImpl();/*** 通过商店编号查商店* @param store_id* @return*/@Overridepublic PetShop queryPetShop_id(Integer store_id) {List<PetShop> petShopList = petShopDao.queryPetShopList(store_id,null,null,null);if (petShopList != null){return petShopList.get(0);}elsereturn null;}/*** 用商店名字找到商店* @param shopName* @return*/@Overridepublic PetShop queryPetShopname(String shopName) {List<PetShop> petShopList = petShopDao.queryPetShopList(null,shopName,null,null);if (petShopList != null){return petShopList.get(0);}elsereturn null;}/*** 展示商店的信息*/@Overridepublic void showPetShop() {List list = new ArrayList();ResultSet rs = BaseDao.showExecuteQuery("select * from petshop");try {while (rs.next()){int store_id = rs.getInt("store_id");String name = rs.getString("name");System.out.println(store_id+"\t\t"+name);}} catch (SQLException throwables) {throwables.printStackTrace();}}/*** 商店登录* @param shopName* @param password* @return*/@Overridepublic boolean petShopLogin(String shopName, String password) {PetShop petShop = queryPetShopname(shopName);if (petShop == null){return false;}if (petShop.getPasswor().equals(password)){System.out.println("-------恭喜您成功登录-------");System.out.println("-------您的基本信息:-------");System.out.println("名字"+petShop.getName());System.out.println("元宝数"+petShop.getBalance());return true;}elsereturn false;}/*** 购买宠物* @param shopName*/@Overridepublic void buyPet(String shopName) {PetShop petShop = new PetShopimpl().queryPetShopname(shopName);Scanner input = new Scanner(System.in);System.out.println("--------以下是宠物主人正在出售的宠物--------");List<Pet> masterPet = new Petimpl().queryPet_owner_id("master");for (Pet pet:masterPet) {if (pet.getSell()==0){masterPet.remove(pet);break;}}new Petimpl().showPetList(masterPet);System.out.println("请选择要购买哪一个宠物,输入选项序号:");int number1 = input.nextInt();PetMaster petMaster = new PetMasterimpl().queryPetMaster_id(masterPet.get(number1-1).getOwner_id());new PetDaoimpl().revisePet("petmaster","masterMoney",petMaster.getMasterMoney()+masterPet.get(number1-1).getPet_money(),"Owner_id",petMaster.getOwner_id());new PetDaoimpl().revisePet("petshop","balance",petShop.getBalance()-masterPet.get(number1-1).getPet_money(),"Store_id",petShop.getStore_id());new PetDaoimpl().revisePet("pet","Owner_id",null,"Pet_id",masterPet.get(number1-1).getPet_id());new PetDaoimpl().revisePet("pet","Store_id",petShop.getStore_id(),"Pet_id",masterPet.get(number1-1).getPet_id());new com.xk.jdbc.dao.impl.Accountimpl().addCount(2,masterPet.get(number1-1).getPet_id(),petMaster.getOwner_id(),petShop.getStore_id(),masterPet.get(number1-1).getMoney());System.out.println("购买成功!");}/*** 卖出宠物* @param shopName*/@Overridepublic void sellPet(String shopName) {PetShop petShop = new PetShopimpl().queryPetShopname(shopName);Scanner input = new Scanner(System.in);System.out.println("---------以下是宠物商店正在出售的宠物--------");List<Pet> pet = inquirePet();System.out.println("------------请选择要卖出的序号------------");int number = input.nextInt();System.out.println("------------要卖出的宠物信息如下------------");System.out.println("宠物名字叫"+pet.get(number-1).getName()+"宠物类型是:"+pet.get(number-1).getType_name());System.out.println("请确认是否购买,y卖出,n表示不卖");if (input.next().equals("n")){System.out.println("您选择不卖");return;}else{System.out.println("下面是现有宠物买家,请选择序号");new PetMasterimpl().showPetMaster();int number1 = input.nextInt();PetMaster petMaster = new PetMasterimpl().queryPetMaster_id(number1);new PetDaoimpl().revisePet("petmaster","masterMoney",petMaster.getMasterMoney()-pet.get(number-1).getPet_money(),"Owner_id",petMaster.getOwner_id());new PetDaoimpl().revisePet("petshop","balance",petShop.getBalance()+pet.get(number-1).getPet_money(),"Store_id",petShop.getStore_id());new PetDaoimpl().revisePet("pet","Owner_id",null,"Pet_id",pet.get(number-1).getPet_id());new PetDaoimpl().revisePet("pet","Store_id",petShop.getStore_id(),"Pet_id",pet.get(number-1).getPet_id());new com.xk.jdbc.dao.impl.Accountimpl().addCount(1,pet.get(number-1).getPet_id(),petMaster.getOwner_id(),petShop.getStore_id(),pet.get(number-1).getMoney());System.out.println("卖出成功!");return;}}/*** 培育宠物* @param shopName*/@Overridepublic void nurturePet(String shopName) {PetShop petShop = new PetShopimpl().queryPetShopname(shopName);Scanner input = new Scanner(System.in);System.out.println("请输入要培育宠物的类型");String type = input.next();System.out.println("请在下面输入宠物属性");System.out.println("请输入宠物名字");String petName = input.next();System.out.println("请输入宠物健康指数");int health = input.nextInt();System.out.println("请输入爱心指数");int love = input.nextInt();int shopNumber = petShop.getStore_id();System.out.println("请估算宠物价值");int money = input.nextInt();new PetDaoimpl().addPet(petName,type,health,love,shopNumber,money);System.out.println("培育成功了一只"+type+"宠物!");}/*** 查询代售宠物* @return*/@Overridepublic List<Pet> inquirePet() {List<Pet> shopPet = new Petimpl().queryPet_owner_id("shop");new Petimpl().showPetList(shopPet);return shopPet;}/*** 查看商店结余*/@Overridepublic void checkBalance(String name) {PetShop petShop = new PetShopimpl().queryPetShopname(name);System.out.println(petShop.getName()+"结余为:"+petShop.getBalance());}/*** 查看商店账目*/@Overridepublic void checkCount(String name) {new Accountimpl().showCount(name);}/*** 开宠物商店*/@Overridepublic void openShop(String name) {Scanner input = new Scanner(System.in);System.out.println("请在下面输入宠物商店属性:");System.out.println("请输入宠物商店名字:");String name1 = input.next();System.out.println("请输入宠物商店密码:");String password = input.next();System.out.println("请输入宠物商店资金");int money = input.nextInt();int index = new PetShopDaoImpl().openShop(name1,password,money);System.out.println("成功创建了一个宠物商店,商店名字叫"+name1);}/*** 找到库存或者新培育的宠物*/public List<Pet> showPetType(String newOrOld){List<Pet> petList = new Petimpl().queryPet_owner_id("shop");List<Pet> oldPet1 = new ArrayList<>();List<Pet> newPet1 = new ArrayList<>();for (Pet pet1:petList) {if (pet1.getHealth()<100){oldPet1.add(pet1);}elsenewPet1.add(pet1);}if (newOrOld.equals("old")){return oldPet1;}else{return newPet1;}}}

service/impl/Accountimpl

package com.xk.jdbc.service.impl;import com.xk.jdbc.dao.BaseDao;import com.xk.jdbc.service.AccountService;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;public class Accountimpl implements AccountService {/*** 展示账单*/@Overridepublic void showCount(String name) {List list = new ArrayList();ResultSet rs = BaseDao.showExecuteQuery("select * from account ");try {while (rs.next()){int accountId = rs.getInt("accountId");int deal_type = rs.getInt("deal_type");int price = rs.getInt("price");//1.商店卖给宠物主人2.宠物主人卖给商店System.out.println("第"+accountId+"笔交易类型为:"+(deal_type==1?"商店卖给宠物主人":"宠物主人卖给商店")+",交易金额为"+price);}} catch (SQLException throwables) {throwables.printStackTrace();}}}

service/impl/Petimpl

package com.xk.jdbc.service.impl;import com.xk.jdbc.dao.BaseDao;import com.xk.jdbc.dao.PetDao;import com.xk.jdbc.entity.Pet;import com.xk.jdbc.service.PetService;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;public class Petimpl implements PetService{/*** 通过输入的宠物商店还是宠物主人来找到其下所有宠物信息*/@Overridepublic List<Pet> queryPet_owner_id(String MasterName) {String sql = null;List<Pet> petList = null;if (MasterName.equalsIgnoreCase("Master")){sql = "select * from pet where pet.store_id is null";}else {sql = "select * from pet where pet.owner_id is null ";}petList = BaseDao.executeQuery(sql,null,Pet.class);return petList;}/*** 展示所有宠物信息*/@Overridepublic void showAllPet() {List list = new ArrayList();ResultSet rs = BaseDao.showExecuteQuery("select * from pet");try {while (rs.next()){int pet_id = rs.getInt("pet_id");String name = rs.getString("name");System.out.println(pet_id+"\t\t"+name);}} catch (SQLException throwables) {throwables.printStackTrace();}}/*** 展示宠物集合宠物信息*/@Overridepublic void showPetList(List<Pet> petList) {System.out.println("序号\t宠物名称\t类型\t元宝数");int i = 1;if (petList.size()!=0){for (Pet pet1:petList) {System.out.println(i+"\t"+pet1.getName()+"\t"+pet1.getType_name()+"\t"+pet1.getMoney());i++;}}}}

service/impl/PetMasterimpl

package com.xk.jdbc.service.impl;import com.xk.jdbc.dao.BaseDao;import com.xk.jdbc.dao.PetMasterDao;import com.xk.jdbc.dao.impl.Accountimpl;import com.xk.jdbc.dao.impl.PetDaoimpl;import com.xk.jdbc.dao.impl.PetMasterDaoimpl;import com.xk.jdbc.entity.Pet;import com.xk.jdbc.entity.PetMaster;import com.xk.jdbc.entity.PetShop;import com.xk.jdbc.service.PetMasterService;import com.xk.jdbc.service.PetShopService;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class PetMasterimpl implements PetMasterService{/*** 通过主人编号查询主人*/@Overridepublic PetMaster queryPetMaster_id(Integer owner_id) {List<PetMaster> petMasterList = new PetMasterDaoimpl().queryPetMasterList(owner_id,null,null,null);if (petMasterList != null){return petMasterList.get(0);}elsereturn null;}/*** 通过主人姓名查询主人*/@Overridepublic PetMaster queryPetMastername(String mastername) {List<PetMaster> petMasterList = new PetMasterDaoimpl().queryPetMasterList(null,mastername,null,null);if (petMasterList.size()>0){return petMasterList.get(0);}elsereturn null;}/*** 展示所有宠物主人信息*/@Overridepublic void showPetMaster() {List list = new ArrayList();ResultSet rs = BaseDao.showExecuteQuery("select * from petmaster");try {while (rs.next()){int owner_id = rs.getInt("owner_id");String mastername = rs.getString("mastername");System.out.println(owner_id+"\t\t"+mastername);}} catch (SQLException throwables) {throwables.printStackTrace();}}/***主人登录*/@Overridepublic PetMaster masterLogin(String name, String password) {PetMaster petMaster = queryPetMastername(name);if (petMaster == null){return null;}if (petMaster.getMasterPwd().equals(password)){System.out.println("-------恭喜您成功登录-------");System.out.println("-------您的基本信息:-------");System.out.println("名字"+petMaster.getMasterName());System.out.println("元宝数"+petMaster.getMasterMoney());return petMaster;}elsereturn null;}/***买宠物* @param name*/@Overridepublic void petbuy(String name) {PetMaster petMaster = new PetMasterimpl().queryPetMastername(name);boolean isok = true;do {Scanner input = new Scanner(System.in);System.out.println("-------请输入选择要购买的范围,只输入选择项的序号-------");System.out.println("1,购买库存宠物\n2,购买新培育宠物");String number = input.next();if (!(number.equals("1") || number.equals("2"))){System.out.println("输入错误,请重新输入");continue;}switch (number){case "1":System.out.println("--------以下是库存宠物-------");List<Pet> oldPet = new PetShopimpl().showPetType("old");new Petimpl().showPetList(oldPet);System.out.println("--------请选择要购买哪一个宠物,并输入选择项的序号--------");int number1 = input.nextInt();PetShop petShop =new PetShopimpl().queryPetShop_id(oldPet.get(number1-1).getStore_id());new PetDaoimpl().revisePet("petMaster","masterMoney",(petMaster.getMasterMoney()-oldPet.get(number1-1).getPet_money()),"Owner_id",petMaster.getOwner_id());new PetDaoimpl().revisePet("petShop","balance",(petShop.getBalance()+oldPet.get(number1-1).getPet_money()),"Store_id",petShop.getStore_id());new PetDaoimpl().revisePet("pet","Store_id",null,"Pet_id",oldPet.get(number1-1).getPet_id());new PetDaoimpl().revisePet("pet","Owner_id",petMaster.getOwner_id(),"Pet_id",oldPet.get(number1-1).getPet_id());new Accountimpl().addCount(1,oldPet.get(number1-1).getPet_id(),petMaster.getOwner_id(),petShop.getStore_id(),oldPet.get(number1-1).getMoney());System.out.println("恭喜您,购买成功!");break;case "2":System.out.println("--------以下是新培育宠物-------");System.out.println("序号\t宠物名称\t类型\t元宝数");List<Pet> newPet = new PetShopimpl().showPetType("new");new Petimpl().showPetList(newPet);System.out.println("--------请选择要购买哪一个宠物,并输入选择项的序号--------");int number2 = input.nextInt();PetMaster petMaster1 = new PetMasterimpl().queryPetMastername(name);PetShop petShop1 =new PetShopimpl().queryPetShop_id(newPet.get(number2-1).getStore_id());new PetDaoimpl().revisePet("petmaster","masterMoney",petMaster1.getMasterMoney()-newPet.get(number2-1).getPet_money(),"Owner_id",petMaster1.getOwner_id());new PetDaoimpl().revisePet("petshop","balance",petShop1.getBalance()+newPet.get(number2-1).getPet_money(),"Store_id",petShop1.getStore_id());new PetDaoimpl().revisePet("pet","Store_id",null,"Pet_id",newPet.get(number2-1).getPet_id());new PetDaoimpl().revisePet("pet","Owner_id",petMaster1.getOwner_id(),"Pet_id",newPet.get(number2-1).getPet_id());new Accountimpl().addCount(1,newPet.get(number2-1).getPet_id(),petMaster1.getOwner_id(),petShop1.getStore_id(),newPet.get(number2-1).getMoney());System.out.println("恭喜您,购买成功!");break;}isok = false;}while (isok);}/*** 卖出宠物* @param name*/@Overridepublic void petSell(String name) {PetMaster petMaster2 = new PetMasterimpl().queryPetMastername(name);boolean index = true;do {Scanner input = new Scanner(System.in);System.out.println("-------我的宠物列表-------");System.out.println("序号\t宠物名称\t类型");List<Pet> masterPet = new Petimpl().queryPet_owner_id("master");List<Pet> pet2 = new ArrayList<>();if (masterPet.size()!= 0){for (Pet pet1:masterPet) {if (pet1.getOwner_id().equals(queryPetMastername(name).getOwner_id())){pet2.add(pet1);}}new Petimpl().showPetList(pet2);System.out.println("--------请选择要出售的宠物序号(输入其他退出)--------");int number = input.nextInt();if (number<=0 || number>pet2.size()){index = false;}else {System.out.println("--------您要出售的宠物信息如下--------");System.out.println("宠物名字叫:"+pet2.get(number-1).getName()+" 宠物类型是:"+pet2.get(number-1).getType_name());System.out.println("请确认是否卖出,y代表卖出,n代表不买");String ys = input.next();if (ys.equals("y")){System.out.println("--------下面是现有宠物商店名字,请选择您要卖给的卖家序号--------");new PetShopimpl().showPetShop();int shopNumber = input.nextInt();PetShop petShop1 =new PetShopimpl().queryPetShop_id(shopNumber);new PetDaoimpl().revisePet("petmaster","masterMoney",petMaster2.getMasterMoney()-pet2.get(number-1).getPet_money(),"Owner_id",petMaster2.getOwner_id());new PetDaoimpl().revisePet("petshop","balance",petShop1.getBalance()+pet2.get(number-1).getPet_money(),"Store_id",petShop1.getStore_id());new PetDaoimpl().revisePet("pet","Owner_id",null,"Pet_id",pet2.get(number-1).getPet_id());new PetDaoimpl().revisePet("pet","Store_id",shopNumber,"Pet_id",pet2.get(number-1).getPet_id());new Accountimpl().addCount(2,pet2.get(number-1).getPet_id(),petMaster2.getOwner_id(),petShop1.getStore_id(),pet2.get(number-1).getMoney());System.out.println("卖出成功!");index = false;}else{System.out.println("您选择不卖,请重新选择");continue;}}}}while (index);}}

database.properties

# \u9A71\u52A8\u7C7Bdriver=com.mysql.jdbc.Driver# \u8FDE\u63A5\u5B57\u7B26\u4E32url=jdbc:mysql://localhost:3306/petshop# \u6570\u636E\u5E93\u8D26\u53F7user=root# \u6570\u636E\u5E93\u5BC6\u7801password=root

petshop.sql

/*Navicat MySQL Data TransferSource Server : xk_mysqlSource Server Version : 50140Source Host : localhost:3306Source Database : petshopTarget Server Type : MYSQLTarget Server Version : 50140File Encoding : 65001Date: -08-11 15:27:50*/SET FOREIGN_KEY_CHECKS=0;-- ------------------------------ Table structure for account-- ----------------------------DROP TABLE IF EXISTS `account`;CREATE TABLE `account` (`accountId` int(4) NOT NULL AUTO_INCREMENT COMMENT '账单id',`deal_type` int(4) NOT NULL COMMENT '交易类型,1.商店卖给宠物主人2.宠物主人卖给商店',`pet_id` int(4) DEFAULT NULL COMMENT '宠物id',`buyer_id` int(4) DEFAULT NULL COMMENT '买家id',`seller_id` int(4) DEFAULT NULL COMMENT '卖家id',`price` int(4) DEFAULT NULL COMMENT '交易价格',`deal_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '交易时间',PRIMARY KEY (`accountId`),KEY `FK_petMaster_account_buyer_id` (`buyer_id`),KEY `FK_pet_account_pet_id` (`pet_id`),CONSTRAINT `FK_petMaster_account_buyer_id` FOREIGN KEY (`buyer_id`) REFERENCES `petmaster` (`owner_id`),CONSTRAINT `FK_pet_account_pet_id` FOREIGN KEY (`pet_id`) REFERENCES `pet` (`pet_id`)) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8;-- ------------------------------ Records of account-- ----------------------------INSERT INTO `account` VALUES ('1', '1', '2', '1', '2', '7', '-08-10 14:26:58');INSERT INTO `account` VALUES ('2', '1', '2', '1', '2', '7', '-08-10 14:30:24');INSERT INTO `account` VALUES ('3', '1', '2', '1', '2', '7', '-08-10 14:41:46');INSERT INTO `account` VALUES ('4', '1', '2', '1', '2', '7', '-08-10 14:44:15');INSERT INTO `account` VALUES ('5', '1', '2', '1', '2', '7', '-08-10 15:03:02');INSERT INTO `account` VALUES ('6', '1', '2', '1', '2', '7', '-08-10 15:03:58');INSERT INTO `account` VALUES ('7', '1', '2', '1', '2', '7', '-08-10 15:08:31');INSERT INTO `account` VALUES ('8', '2', '3', '1', '1', '6', '-08-10 15:15:02');INSERT INTO `account` VALUES ('9', '2', '3', '1', '1', '6', '-08-10 15:16:50');INSERT INTO `account` VALUES ('10', '2', '4', '2', '1', '3', '-08-10 15:29:48');INSERT INTO `account` VALUES ('11', '1', '1', '1', '1', '5', '-08-10 15:33:14');INSERT INTO `account` VALUES ('12', '1', '2', '1', '2', '7', '-08-10 15:36:13');INSERT INTO `account` VALUES ('13', '1', '2', '1', '2', '7', '-08-10 15:42:27');INSERT INTO `account` VALUES ('14', '1', '2', '1', '2', '7', '-08-10 15:46:12');INSERT INTO `account` VALUES ('15', '1', '2', '1', '2', '7', '-08-10 15:48:04');INSERT INTO `account` VALUES ('16', '1', '2', '1', '2', '7', '-08-10 15:49:26');INSERT INTO `account` VALUES ('17', '1', '2', '1', '2', '7', '-08-10 15:52:01');INSERT INTO `account` VALUES ('18', '1', '2', '1', '2', '7', '-08-10 15:54:01');INSERT INTO `account` VALUES ('19', '1', '2', '1', '2', '7', '-08-10 16:00:34');INSERT INTO `account` VALUES ('20', '1', '2', '1', '2', '7', '-08-10 16:18:06');INSERT INTO `account` VALUES ('21', '1', '2', '1', '2', '7', '-08-10 16:41:03');INSERT INTO `account` VALUES ('22', '2', '2', '1', '2', '7', '-08-10 16:41:30');INSERT INTO `account` VALUES ('23', '2', '4', '2', '1', '3', '-08-10 16:42:03');INSERT INTO `account` VALUES ('24', '1', '1', '1', '1', '5', '-08-10 16:49:35');INSERT INTO `account` VALUES ('25', '1', '1', '1', '1', '5', '-08-11 09:44:24');INSERT INTO `account` VALUES ('26', '1', '2', '1', '2', '7', '-08-11 09:45:10');INSERT INTO `account` VALUES ('27', '2', '2', '1', '1', '7', '-08-11 09:45:20');INSERT INTO `account` VALUES ('28', '2', '3', '1', '1', '6', '-08-11 15:20:25');INSERT INTO `account` VALUES ('29', '1', '2', '1', '1', '7', '-08-11 15:20:34');INSERT INTO `account` VALUES ('30', '2', '2', '1', '3', '7', '-08-11 15:20:50');-- ------------------------------ Table structure for pet-- ----------------------------DROP TABLE IF EXISTS `pet`;CREATE TABLE `pet` (`pet_id` int(4) NOT NULL AUTO_INCREMENT COMMENT '宠物id',`name` char(50) DEFAULT NULL COMMENT '宠物名',`type_name` char(20) DEFAULT NULL COMMENT '宠物类型',`health` int(4) DEFAULT NULL COMMENT '是否健康',`love` int(4) DEFAULT NULL COMMENT '爱心指数',`birthday` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '出生日期',`owner_id` int(4) DEFAULT NULL COMMENT '宠物主人id',`store_id` int(4) DEFAULT NULL COMMENT '宠物所属商店id',`pet_money` int(10) DEFAULT NULL COMMENT '宠物价值',`sell` int(1) DEFAULT NULL COMMENT '是否愿意出售,1为愿意出售,0为不愿意出售',PRIMARY KEY (`pet_id`),KEY `FK_petMaster_pet_owner_id` (`owner_id`),KEY `FK_petshop_pet_store_id` (`store_id`),CONSTRAINT `FK_petMaster_pet_owner_id` FOREIGN KEY (`owner_id`) REFERENCES `petmaster` (`owner_id`),CONSTRAINT `FK_petshop_pet_store_id` FOREIGN KEY (`store_id`) REFERENCES `petshop` (`store_id`)) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;-- ------------------------------ Records of pet-- ----------------------------INSERT INTO `pet` VALUES ('1', '珠珠', 'pig', '100', '120', '-08-08 13:36:46', '1', null, '5', '1');INSERT INTO `pet` VALUES ('2', '花花', 'cat', '90', '100', '-08-08 13:45:26', null, '3', '7', '1');INSERT INTO `pet` VALUES ('3', '贝贝', 'cat', '100', '130', '-08-08 13:45:37', null, '1', '6', '0');INSERT INTO `pet` VALUES ('4', '成成', 'dog', '120', '100', '-08-08 13:45:46', null, '1', '3', '1');INSERT INTO `pet` VALUES ('5', '露露', 'dog', '130', '130', '-08-08 13:45:50', null, '1', '4', '1');INSERT INTO `pet` VALUES ('6', '老虎', 'tiger', '160', '80', '-08-08 13:45:54', '2', null, '8', '1');INSERT INTO `pet` VALUES ('7', '狮子', 'lion', '150', '70', '-08-08 13:45:59', null, '1', '2', '1');INSERT INTO `pet` VALUES ('8', '鱼', 'fish', '80', '100', '-08-08 13:46:02', '1', null, '5', '1');INSERT INTO `pet` VALUES ('9', '小狗', 'dog', '100', '100', '-08-08 13:46:13', null, '1', '6', '1');INSERT INTO `pet` VALUES ('10', '猪', '珠珠', '120', '100', '-08-10 16:50:58', null, '1', '3', null);-- ------------------------------ Table structure for petmaster-- ----------------------------DROP TABLE IF EXISTS `petmaster`;CREATE TABLE `petmaster` (`owner_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '宠物主人id',`masterName` char(10) NOT NULL COMMENT '宠物主人名',`masterPwd` char(10) NOT NULL COMMENT '宠物主人密码',`masterMoney` int(11) DEFAULT NULL COMMENT '宠物主人元宝',PRIMARY KEY (`owner_id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;-- ------------------------------ Records of petmaster-- ----------------------------INSERT INTO `petmaster` VALUES ('1', '小明', '123456', '146');INSERT INTO `petmaster` VALUES ('2', '小强', '000000', '203');-- ------------------------------ Table structure for petshop-- ----------------------------DROP TABLE IF EXISTS `petshop`;CREATE TABLE `petshop` (`store_id` int(4) NOT NULL AUTO_INCREMENT COMMENT '宠物商店ID',`name` char(10) DEFAULT NULL COMMENT '宠物商店名字',`passwor` char(10) DEFAULT NULL COMMENT '宠物商店密码',`balance` int(4) DEFAULT NULL COMMENT '宠物商店结余',PRIMARY KEY (`store_id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;-- ------------------------------ Records of petshop-- ----------------------------INSERT INTO `petshop` VALUES ('1', '宝强宠物商店', '123456', '2027');INSERT INTO `petshop` VALUES ('2', '利丰宠物商店', '000000', '1021');INSERT INTO `petshop` VALUES ('3', '134', '123456', '93');

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。