首页 > 分享 > java入门:用jdbc实现宠物商店管理系统续篇

java入门:用jdbc实现宠物商店管理系统续篇

用jdbc实现宠物商店管理系统
1,开发语言:java(JDK8)
2,开发工具:IntelliJ IDEA 2021.2.3
3,数据库:MySQL
4,操作系统:Windows10
5,需要引入的jar包:mysql-connector-java-8.0.26.jar,下载地址:https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.26

因为上篇的数据库文件没有了,重新建了数据库以及修改了代码里的脚本,用下面的新代码就可以了
以下是运行图片:
程序初始化
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码实现:
逻辑代码

package com.impl; import com.dao.BaseDAO; import java.sql.Connection; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.InputMismatchException; import java.util.Scanner; /** * @author 咕噜科 * ClassName: PetManage * date: 2021/8/26 22:12 * Description: * version 1.0 */ public class PetManage extends BaseDAO { /** * */ public void showAll() { showPetName(); showPetOwner(); showPetStore(); login(); } /** * 显示宠物的姓名以及ID方法 */ public void showPetName() { conn = getConnection(); String sql = "SELECT id,pet_name from pet"; try { state = conn.prepareStatement(sql); rs = state.executeQuery(); System.out.println("Wonderland醒来,所有宠物从MySQL中醒来"); System.out.println("*************************************"); int num = 1; while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("pet_name"); System.out.println("第" + num + "只宠物,名字叫:" + name); num++; } System.out.println("*************************************n"); } catch (Exception e) { e.printStackTrace(); } finally { closeObject1(); } } /** * 显示宠物主人方法 */ public void showPetOwner() { conn = getConnection(); String sql = "select id,owner_id,owner_name,money from pet_owner where owner_type = 1"; try { state = conn.prepareStatement(sql); rs = state.executeQuery(); System.out.println("所有宠物主人从MySQL中醒来"); System.out.println("*************************************"); int num = 1; while (rs.next()) { String name = rs.getString("owner_name"); System.out.println("第" + num + "主人的名字叫:" + name); num++; } System.out.println("*************************************n"); } catch (Exception e) { e.printStackTrace(); } finally { closeObject1(); } } /** * 显示宠物商店方法 */ public void showPetStore() { conn = getConnection(); String sql = "select id,owner_id,owner_name,money,address from pet_owner where owner_type = 2"; try { state = conn.prepareStatement(sql); rs = state.executeQuery(); System.out.println("所有宠物商店从MySQL中醒来"); System.out.println("*************************************"); int num = 1; while (rs.next()) { String storeName = rs.getString("owner_name"); String storeAddress = rs.getString("address"); System.out.println("第" + num + "个商店的名字叫: " + storeName + " ,地址在: " + storeAddress); num++; } System.out.println("*************************************n"); } catch (Exception e) { e.printStackTrace(); } finally { closeObject1(); } } /** * 登录界面选择是主人登录还是商店登录方法 */ public void login() { System.out.println("请选择输入登录模式n1.宠物主人登录n2.宠物商店登录n3.退出系统n-------------------"); try { Scanner input = new Scanner(System.in); int choise = input.nextInt(); if (choise < 1 || choise > 3) { System.out.println("输入有误,请重新选择"); login(); } else { switch (choise) { case 1: petOwnerLogin(); break; case 2: petStoreLogin(); break; case 3: System.out.println("谢谢使用"); System.exit(0); break; default: break; } } } catch (InputMismatchException e) { System.out.println("输入有误,请重新选择"); login(); } } /** * 宠物主人登录方法 * * @return */ public boolean petOwnerLogin() { boolean flag = false; try { Scanner input = new Scanner(System.in); System.out.println("请先登录,请您先输入主人的名字"); String name = input.next(); System.out.println("请您输入主人的密码:"); String password = input.next(); conn = getConnection(); String sql = "SELECT owner_name,password from pet_owner where owner_name=? and password=?"; try { state = conn.prepareStatement(sql); state.setString(1, name); state.setString(2, password); rs = state.executeQuery(); if (rs.next()) { System.out.println("---------恭喜您成功登录!---------"); System.out.println("----------您的基本信息---------"); conn = getConnection(); String sql2 = "SELECT id,owner_name,owner_id,password,money from pet_owner where owner_name=?"; //state=conn.prepareStatement(sql2); //state.setString(1, name); //rs=state.executeQuery(); rs = search(sql2, name); if (rs.next()) { int uid = rs.getInt("owner_id"); String uname = rs.getString("owner_name"); Double uMoney = rs.getDouble("money"); System.out.println("登录成功!!!!"); System.out.println("姓名:" + uname); System.out.println("元宝数:" + uMoney); dealPet(uname, uid); } } else { System.out.println("登录失败,账户与密码不匹配"); login(); } } catch (Exception e) { e.printStackTrace(); } } catch (InputMismatchException e) { System.out.println("输入有误"); login(); } return false; } /** * 选择买宠物或者卖宠物的方法 */ public void dealPet(String ownerName, int uid) { System.out.println("您可以购买和卖出宠物,购买宠物请输入1,卖出宠物请输入2n1.购买宠物n2.卖出宠物n3.返回上一级n4.退出系统"); try { Scanner input2 = new Scanner(System.in); int choise2 = input2.nextInt(); if (choise2 < 1 || choise2 > 4) { System.out.println("输入有误"); dealPet(ownerName, uid); } else { switch (choise2) { case 1: //购买宠物 buyPet(ownerName, uid); break; case 2: //出售宠物 showSellPet(ownerName, uid); break; case 3: //返回上一级 login(); break; case 4: System.out.println("--------谢谢使用------"); System.exit(0); default: break; } } } catch (InputMismatchException e) { System.out.println("输入有误"); dealPet(ownerName, uid); } } /** * 显示主人拥有的宠物 */ public void showSellPet(String ownerName, int uid) { conn = getConnection(); String sql25 = "select id,pet_name,pet_price,pet_role_type,pet_type from pet where owner_type = 1 and owner_id =" + uid; try { state = conn.prepareStatement(sql25); rs = state.executeQuery(); System.out.println("以下是你拥有的宠物:"); ////如果结果集为空,即该主人没有宠物,就返回上一级进行选择 //if(!rs.next()){ //System.out.println("您没有宠物,将自动返回上一级"); //buyPet(ownerName, uid); //} int num = 1; while (rs.next()) { int petid = rs.getInt("id"); String petName = rs.getString("pet_name"); String petPrice = rs.getString("pet_price"); System.out.println("这是" + num + "只宠物,编号是" + petid + ",名字叫:" + petName + ",需要" + petPrice + "个元宝"); num++; } System.out.println("**************************************"); } catch (SQLException e) { e.printStackTrace(); } closeObject1(); sellPet(ownerName, uid); } public void sellPet(String ownerName, int uid) { System.out.println("请输入你想卖出的宠物编号:"); try { Scanner input27 = new Scanner(System.in); int choisePetId = input27.nextInt(); //先查询下有哪些商店 String queryStore = "select owner_id,owner_name from pet_owner where owner_type = 2"; Connection queryStoreConn = getConnection(); try { state = queryStoreConn.prepareStatement(queryStore); rs = state.executeQuery(); int num = 1; if (rs.next()) { //说明有商店存在 System.out.println("请输入你要卖给的商店编号"); int storeId1 = rs.getInt("owner_id"); String storeName = rs.getString("owner_name"); System.out.println(num + "." + storeName+"编号t:"+storeId1); num++; while (rs.next()) { int storeId = rs.getInt("owner_id"); String storeName2 = rs.getString("owner_name"); System.out.println(num + "." + storeName2+"t编号:"+storeId); num++; } System.out.println("8888.返回上一级n9999.退出系统"); int storeId = input27.nextInt(); if (storeId == 8888) { dealPet(ownerName, uid); } else if (storeId == 9999) { System.out.println("-------谢谢使用-------"); System.exit(0); } String sql30 = "select pet_name,pet_price,pet_role_type,pet_type from pet where owner_type = 1 and owner_id = "+uid+" and id = "+choisePetId; Connection conn6 = getConnection(); try { state = conn6.prepareStatement(sql30); rs = state.executeQuery(); if (rs.next()) { String petPrice = rs.getString("pet_price");//宠物价格 Connection conn9 = getConnection(); conn9.setAutoCommit(false); //修改宠物信息 String sql40 = "update pet set owner_type= 2,owner_id=" + storeId + " where id=" + choisePetId; state = conn9.prepareStatement(sql40); int result20 = state.executeUpdate(); //卖主加钱 String sql41 = "update pet_owner set money = money +" + petPrice + " where owner_type = 1 and owner_id =" +uid; state = conn9.prepareStatement(sql41); int result21 = state.executeUpdate(); //商店扣钱 String sql42 = "update pet_owner set money = money -" + petPrice + " where owner_type = 2 and owner_id =" + storeId; state = conn9.prepareStatement(sql42); int result22 = state.executeUpdate(); //获得当前时间 Long time1 = System.currentTimeMillis(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String dealTime = sdf.format(time1); //将该条交易添加至交易账单中 String sql43 = "insert into account_info (account_type,pet_id,sale_owner_id,buy_owner_id,price,create_time) VALUES (2," + choisePetId + "," + storeId + "," + uid + "," + petPrice + ",'" + dealTime + "')"; state = conn9.prepareStatement(sql43); int result23 = state.executeUpdate(); if (result20 > 0 && result21 > 0 && result22 > 0 & result23 > 0) { //提交事务 conn9.commit(); System.out.println("卖出成功"); } else { //回滚事务 System.out.println("出售失败"); conn9.rollback(); } dealPet(ownerName, uid); } else { System.out.println("没有该宠物,卖出失败"); dealPet(ownerName, uid); } } catch (SQLException e) { e.printStackTrace(); System.out.println("系统异常"); dealPet(ownerName, uid); } } else { //没有商店,返回到上一级 dealPet(ownerName, uid); } } catch (SQLException e) { e.printStackTrace(); System.out.println("数据库查询异常!!!"); dealPet(ownerName, uid); } } catch (InputMismatchException e) { System.out.println("输入错误,请重新输入"); sellPet(ownerName, uid); } } ///** // * 显示新培育宠物并且购买 // */ //public void showNewPet() { //// TODO Auto-generated method stub // //} /** * 宠物商店登录的方法 * * @return */ public boolean petStoreLogin() { boolean flag = false; try { Scanner input = new Scanner(System.in); System.out.println("请先登录,请您先输入宠物商店的名字"); String name = input.next(); System.out.println("请您输入宠物商店的密码:"); String password = input.next(); conn = getConnection(); String sq110 = "select owner_id,owner_name,money from pet_owner where owner_type = 2 and owner_name=? and password=?"; state = conn.prepareStatement(sq110); rs = search(sq110, name, password); if (rs.next()) { String storeId = rs.getString("owner_id"); System.out.println("登录成功"); PetStoreMake(name,storeId); } else { System.out.println("登录失败"); login(); } } catch (Exception e) { // TODO: handle exception } return false; } /* * 宠物商店培育新宠物 */ public void PetStoreMake(String storeName,String storeId) throws SQLException { System.out.println("请输入数字进行选择:n1.查询店内宠物n2.培育新宠物n3.退出登录n4.退出系统"); try { Scanner input = new Scanner(System.in); int choise7 = input.nextInt(); if (choise7 < 1 || choise7 > 3) { System.out.println("输入有误"); PetStoreMake(storeName,storeId); } else { switch (choise7) { case 1: storePetQuery(storeName,storeId); break; case 2: storeAddPet(storeName,storeId); break; case 3: //退出登录,返回上一级 login(); break; case 4: System.out.println("-------谢谢使用-------"); System.exit(0); default: break; } } } catch (InputMismatchException e) { System.out.println("输入有误"); PetStoreMake(storeName,storeId); } } /** * 宠物商店培育新宠物的方法 * * @param storeName * @throws SQLException */ public void storeAddPet(String storeName,String storeId) throws SQLException { System.out.println("请输入你想添加的宠物的品种名称(例如:金渐层):"); Scanner input = new Scanner(System.in); String typename = input.next(); System.out.println("请输入该宠物的名字:"); String petName = input.next(); System.out.println("请输入该宠物的类型:n1.普通宠物n2.新型宠物"); int animalType = input.nextInt(); System.out.println("请输入该宠物的价格:"); int price = input.nextInt(); try { Connection conn9 = getConnection(); String sql13 = "insert into pet(pet_name,owner_id,owner_type,pet_price,pet_role_type,pet_type) values ('"+petName+"',"+storeId+",2,"+price+",'"+animalType+"','"+typename+"')"; state = conn9.prepareStatement(sql13); int a = state.executeUpdate(); if (a > 0) { System.out.println("培育新宠物成功"); PetStoreMake(storeName,storeId); } else { System.out.println("培育新宠物失败"); PetStoreMake(storeName,storeId); } }catch (Exception e){ e.printStackTrace(); } } /** * 在商店登录之后进行对店内的宠物进行查询 * * @param storeName */ public void storePetQuery(String storeName,String storeId) { System.out.println("正在查询店内宠物。。。"); conn = getConnection(); String sql11 = "select id,pet_name,pet_price,pet_role_type,pet_type from pet where owner_type = 2 and owner_id = " + storeId; try { state = conn.prepareStatement(sql11); rs = state.executeQuery(); int i = 1; while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("pet_name"); String typename = rs.getString("pet_type"); String petPrice = rs.getString("pet_price"); System.out.println("第" + i + "只宠物编号是:"+id+",名字:" + name + ",宠物类型:" + typename + ",价格:" + petPrice); i++; } System.out.println("----------------------------"); PetStoreMake(storeName,storeId); } catch (Exception e) { e.printStackTrace(); } } /** * 购买宠物的方法 */ public void buyPet(String ownerName, int uid) { System.out.println("请输入选择购买范围,只输入选择项的序号"); System.out.println("1:购买库存宠物n2.购买新培育宠物n3.返回上一级"); try { Scanner input3 = new Scanner(System.in); int choise5 = input3.nextInt(); if (choise5 < 1 || choise5 > 3) { System.out.println("输入有误"); buyPet(ownerName, uid); } else { switch (choise5) { case 1: showPetAll(ownerName, uid, 2); break; case 2: buyNewPet(ownerName, uid, 2); break; case 3: //返回上一级 dealPet(ownerName, uid); break; default: break; } } } catch (InputMismatchException e) { System.out.println("输入有误"); buyPet(ownerName, uid); } } public void buyNewPet(String ownerName, int uid, int petRoleType) { //用于判断查询是否有结果 boolean havePet = false; System.out.println("正在帮你查询新宠物。。。。。"); conn = getConnection(); String sql31 = "SELECT id,pet_name,pet_price from pet where pet_role_type= 2 and owner_type = 2"; try { state = conn.prepareStatement(sql31); rs = state.executeQuery(); while (rs.next()) { int petid = rs.getInt("id"); String petName = rs.getString("pet_name"); String petPrice = rs.getString("pet_price"); System.out.println("序号为:" + petid + ",名字为:" + petName + ",需要" + petPrice + "个元宝"); havePet = true; } if (havePet) { System.out.println("请输入你要购买的新宠物的序号:"); try { //boolean havePet2=false; Scanner input28 = new Scanner(System.in); int newPetId = input28.nextInt(); Connection conn7 = getConnection(); String sql32 = "SELECT id,pet_name,pet_price,pet_role_type,owner_id from pet where owner_type = 2 and id = " + newPetId; state = conn7.prepareStatement(sql32); rs = state.executeQuery(); if (rs.next()) { int storeid = rs.getInt("owner_id"); int petPrice = rs.getInt("pet_price"); Connection conn8 = getConnection(); conn8.setAutoCommit(false); //修改宠物所属信息 String sql33 = "update pet set owner_type= 1,owner_id=" + uid + " where id=" + newPetId; //修改买主账户金额 String sql34 = "update pet_owner set money=money-" + petPrice + " where owner_type = 1 and owner_id =" + uid; //修改商店金额 String sql35 = "update pet_owner set money=money+" + petPrice + " where owner_type = 2 and owner_id =" + storeid; //获得当前时间 Long time1 = System.currentTimeMillis(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String dealTime = sdf.format(time1); //将该条交易添加至交易账单中 String sql36 = "insert into account_info (account_type,pet_id,sale_owner_id,buy_owner_id,price,create_time) VALUES (2," + newPetId + "," + storeid + "," + uid + "," + petPrice + ",'" + dealTime + "')"; state = conn8.prepareStatement(sql33); int result13 = state.executeUpdate(); state = conn8.prepareStatement(sql34); int result14 = state.executeUpdate(); state = conn8.prepareStatement(sql35); int result15 = state.executeUpdate(); state = conn8.prepareStatement(sql36); int result16 = state.executeUpdate(); if (result13 > 0 && result14 > 0 && result15 > 0 && result16 > 0) { //如果都成功执行,改变数据,那就提交事务 conn8.commit(); System.out.println("购买成功"); } else { //如果中加你有一条没有执行成功那就回滚事务 System.out.println("购买失败"); conn8.rollback(); } buyPet(ownerName, uid); } else { System.out.println("输入错误,没有该序号的新宠物"); buyNewPet(ownerName, uid, 2); } } catch (InputMismatchException e) { e.printStackTrace(); } } else { System.out.println("暂时还没新宠物"); buyPet(ownerName, uid); } } catch (SQLException e) { e.printStackTrace(); } } /** * 展示库存宠物名字,序号,类型的方法 */ public void showPetAll(String ownerName, int uid, int petRoleType) { System.out.println("---------以下是库存宠物--------"); conn = getConnection(); String sql6 = "SELECT pet.id,pet_name,pet_type,pet_role_type,pet_price,pet.owner_id,pet.owner_type,owner_name from pet,pet_owner where pet.owner_id = pet_owner.owner_id and pet_role_type = 1 and pet.owner_type = 2"; try { state = conn.prepareStatement(sql6); rs = state.executeQuery(); while (rs.next()) { int petId = rs.getInt("pet.id"); int owner_id = rs.getInt("pet.owner_id"); String petName = rs.getString("pet_name"); String storeName = rs.getString("owner_name"); String petType = rs.getString("pet_type"); String money = rs.getString("pet_price"); System.out.println("序号:" + petId + ",我的名字叫:" + petName + ",我的品种是:" + petType + ",要购买我要花:" + money + "个元宝!我属于:" + storeName); } System.out.println("请输入你想购买的宠物编号:"); try { Scanner input17 = new Scanner(System.in); int choise6 = input17.nextInt(); //对在商店里的宠物进行ID查询,符合的就购买 conn = getConnection(); String sql15 = "select id,pet_name,pet_price,pet_role_type,pet_type,owner_id from pet where id =" + choise6; try { state = conn.prepareStatement(sql15); rs = state.executeQuery(); if (rs.next()) { //这里是宠物主人购买宠物的代码,将宠物的store_ID设置为null,将宠物的owner_ID设置为购买主人的ID //然后主人账户减钱,商店的结余加钱,将该条交易添加至交易账单中 int store_id = rs.getInt("owner_id");//这里是选择的宠物所属商店的ID int petPrice = rs.getInt("pet_price");//宠物价格 //这里用创建一个新的连接 Connection conn1 = getConnection(); //开启事务 conn1.setAutoCommit(false); //将宠物的主人类型改为个人- 1,将宠物的主人id设置为购买主人的ID String sql18 = "update pet set owner_type=1,owner_id=" + uid + " where pet.id=" + choise6; //宠物主人减钱 String sql19 = "update pet_owner set money = money -" + petPrice + " where owner_type = 1 and owner_id=" + uid; //宠物商店加钱 String sql20 = "update pet_owner set money = money +" + petPrice + " where owner_type = 2 and owner_id=" + store_id; //获得当前时间 Long time1 = System.currentTimeMillis(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dealTime = sdf.format(time1); //将该条交易添加至交易账单中 String sql21 = "insert into account_info (account_type,pet_id,sale_owner_id,buy_owner_id,price,create_time) VALUES (1," + choise6 + "," + store_id + "," + uid + "," + petPrice + ",'" + dealTime + "')"; state = conn1.prepareStatement(sql18); int result2 = state.executeUpdate(); state = conn1.prepareStatement(sql19); int result3 = state.executeUpdate(); state = conn1.prepareStatement(sql20); int result4 = state.executeUpdate(); state = conn1.prepareStatement(sql21); int result5 = state.executeUpdate(); if (result2 > 0 && result3 > 0 && result4 > 0 && result5 > 0) { //如果都成功执行,改变数据,那就提交事务 conn1.commit(); System.out.println("购买成功"); } else { //如果中加你有一条没有执行成功那就回滚事务 System.out.println("购买失败"); conn1.rollback(); } //返回上一级 buyPet(ownerName, uid); } else { System.out.println("购买失败"); //返回上一级 buyPet(ownerName, uid); } } catch (SQLException e) { e.printStackTrace(); } } catch (InputMismatchException e) { System.out.println("输入有误"); showPetAll(ownerName, uid, 1); } } catch (SQLException e) { e.printStackTrace(); } } }

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716

操作数据库的工具类BaseDAO:

package com.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /** * @author 咕噜科 * ClassName: BaseDao * date: 2021/8/26 22:11 * Description: * version 1.0 */ public class BaseDAO { public Connection conn = null; public PreparedStatement state = null; public ResultSet rs = null; /** * 获取连接对象 * @return */ public Connection getConnection() { try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/pet_store", "root", "123"); } catch (Exception e) { e.printStackTrace(); } return conn; } public int update(String sql, Object...obs) throws SQLException { int result = 0; conn = getConnection(); state = conn.prepareStatement(sql); for (int i = 0; i < obs.length; i++) { state.setObject(i + 1, obs[i]); } result = state.executeUpdate(); return result; } public ResultSet search(String sql, Object...obs) { try { conn = getConnection(); state = conn.prepareStatement(sql); for (int i = 0; i < obs.length; i++) { state.setObject(i + 1, obs[i]); } rs = state.executeQuery(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rs; } public void closeObject1() { try { if (rs != null) { rs.close(); } if (state != null) { state.close(); } if (conn != null) { conn.close(); } } catch (Exception e) { e.printStackTrace(); } } public void closeObject2(AutoCloseable... obs) { try { for (int i = 0; i < obs.length; i++) { if (obs[i] != null) { obs[i].close(); } } } catch (Exception e) { e.printStackTrace(); } } }

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990

Main方法入口:

public class MainTest { public static void main(String[] args) { System.out.println("=========宠物商店启动======="); PetManage pm = new PetManage(); //显示所有宠物 pm.showAll(); } } 12345678910

需要将jar包导入,才能正常运行
在这里插入图片描述
数据库表
宠物表(pet)

/* Navicat MySQL Data Transfer Source Server : hth Source Server Version : 50562 Source Host : localhost:3306 Source Database : pet_store Target Server Type : MYSQL Target Server Version : 50562 File Encoding : 65001 Date: 2021-11-07 20:28:33 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `pet` -- ---------------------------- DROP TABLE IF EXISTS `pet`; CREATE TABLE `pet` ( `id` int(11) NOT NULL AUTO_INCREMENT, `pet_name` varchar(20) NOT NULL, `pet_price` decimal(10,2) NOT NULL, `pet_role_type` int(3) NOT NULL DEFAULT '1' COMMENT '宠物类型:1-普通宠物;2-培育宠物', `pet_type` varchar(20) NOT NULL COMMENT '宠物品种', `owner_id` int(5) NOT NULL, `owner_type` int(5) NOT NULL COMMENT '主人类型:1-个人;2-商店', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of pet -- ---------------------------- INSERT INTO `pet` VALUES ('1', '欢欢', '5.00', '1', '哈士奇', '1', '1'); INSERT INTO `pet` VALUES ('2', '齐齐', '10.00', '1', '藏獒', '4', '2'); INSERT INTO `pet` VALUES ('3', '露露', '20.00', '1', '银渐层', '3', '1'); INSERT INTO `pet` VALUES ('4', '布布1', '5.00', '1', '金毛', '3', '1'); INSERT INTO `pet` VALUES ('5', '布布2', '5.00', '1', '哈士奇', '4', '2'); INSERT INTO `pet` VALUES ('6', '布布3', '5.00', '1', '银渐层', '5', '2'); INSERT INTO `pet` VALUES ('7', '布布4', '5.00', '1', '金毛', '4', '2'); INSERT INTO `pet` VALUES ('8', '布布5', '5.00', '1', '银渐层', '5', '2'); INSERT INTO `pet` VALUES ('9', '布布6', '5.00', '1', '哈士奇', '4', '2'); INSERT INTO `pet` VALUES ('10', '布布7', '5.00', '1', '金毛', '5', '2'); INSERT INTO `pet` VALUES ('11', '龙龙1', '30.00', '2', '哈士奇', '3', '1'); INSERT INTO `pet` VALUES ('12', '龙龙2', '40.00', '2', '银渐层', '5', '2'); INSERT INTO `pet` VALUES ('13', '龙龙3', '50.00', '2', '藏獒', '4', '2'); INSERT INTO `pet` VALUES ('14', '小辉2', '50.00', '2', '金渐层', '4', '2'); INSERT INTO `pet` VALUES ('15', '小辉3', '50.00', '2', '金渐层', '4', '2'); INSERT INTO `pet` VALUES ('16', '小辉4', '50.00', '2', '金渐层', '4', '2'); INSERT INTO `pet` VALUES ('17', '小哈', '50.00', '2', '金渐层', '5', '2'); INSERT INTO `pet` VALUES ('18', '小哈', '5.00', '1', '金渐层', '5', '2');

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354

用户-商店信息表(pet_owner)

/* Navicat MySQL Data Transfer Source Server : hth Source Server Version : 50562 Source Host : localhost:3306 Source Database : pet_store Target Server Type : MYSQL Target Server Version : 50562 File Encoding : 65001 Date: 2021-11-07 20:28:44 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `pet_owner` -- ---------------------------- DROP TABLE IF EXISTS `pet_owner`; CREATE TABLE `pet_owner` ( `id` int(11) NOT NULL AUTO_INCREMENT, `owner_id` int(11) NOT NULL, `owner_name` varchar(20) NOT NULL COMMENT '宠物拥有者名称', `owner_type` int(11) NOT NULL DEFAULT '1' COMMENT '主人类型:1-个人;2-商店', `money` decimal(11,0) NOT NULL DEFAULT '0' COMMENT '金币', `address` varchar(30) DEFAULT NULL, `password` varchar(30) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of pet_owner -- ---------------------------- INSERT INTO `pet_owner` VALUES ('10', '3', 'hth', '1', '9825', '江西', '123'); INSERT INTO `pet_owner` VALUES ('12', '2', '韦德', '1', '14710', '湖北', '123'); INSERT INTO `pet_owner` VALUES ('13', '1', 'kobe', '1', '5000', '山东', '123'); INSERT INTO `pet_owner` VALUES ('14', '4', '重庆观音桥', '2', '332240', '重庆', '123'); INSERT INTO `pet_owner` VALUES ('15', '5', '西苑', '2', '134625', '深圳', '123');

1234567891011121314151617181920212223242526272829303132333435363738394041

交易信息表(account_info)

/* Navicat MySQL Data Transfer Source Server : hth Source Server Version : 50562 Source Host : localhost:3306 Source Database : pet_store Target Server Type : MYSQL Target Server Version : 50562 File Encoding : 65001 Date: 2021-11-07 20:28:39 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `account_info` -- ---------------------------- DROP TABLE IF EXISTS `account_info`; CREATE TABLE `account_info` ( `id` int(11) NOT NULL AUTO_INCREMENT, `account_type` varchar(255) DEFAULT NULL, `buy_owner_id` int(11) NOT NULL, `buy_owner_name` varchar(30) DEFAULT NULL, `buy_owner_type` int(11) DEFAULT NULL, `sale_owner_id` int(11) NOT NULL, `sale_owner_type` int(11) DEFAULT NULL, `sale_owner_name` varchar(30) DEFAULT NULL, `pet_id` int(11) NOT NULL, `price` decimal(10,0) DEFAULT NULL, `create_time` varchar(30) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of account_info -- ---------------------------- INSERT INTO `account_info` VALUES ('8', '1', '3', null, null, '4', null, null, '2', '5', '2021-11-07 17:51:38'); INSERT INTO `account_info` VALUES ('9', '1', '3', null, null, '5', null, null, '4', '5', '2021-11-07 17:52:23'); INSERT INTO `account_info` VALUES ('10', '2', '3', null, null, '5', null, null, '3', '20', '2021-11-07'); INSERT INTO `account_info` VALUES ('11', '2', '3', null, null, '4', null, null, '11', '30', '2021-11-07'); INSERT INTO `account_info` VALUES ('12', '2', '3', null, null, '5', null, null, '12', '40', '2021-11-07'); INSERT INTO `account_info` VALUES ('13', '2', '3', null, null, '4', null, null, '13', '50', '2021-11-07'); INSERT INTO `account_info` VALUES ('16', '2', '3', null, null, '4', null, null, '2', '10', '2021-11-07'); INSERT INTO `account_info` VALUES ('17', '1', '3', null, null, '4', null, null, '2', '10', '2021-11-07 20:14:25'); INSERT INTO `account_info` VALUES ('18', '2', '3', null, null, '4', null, null, '11', '30', '2021-11-07'); INSERT INTO `account_info` VALUES ('19', '2', '3', null, null, '4', null, null, '2', '10', '2021-11-07');

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950

相关知识

[附源码]java毕业设计宠物商店管理系统
java ssm宠物医院诊断预约管理系统springboot
基于java的宠物管理系统设计与实现
宠物商店详细设计说明书
基于java+springboot的宠物商店、宠物管理系统设计与实现
毕业设计:基于java的宠物管理系统设计与实现
宠物商店 案例分析
基于java的宠物用品店系统的设计与实现
Java基于java鸟类管理系统与实现(开题+源码)
宠物管理系统(宠物管理系统的设计与实现)

网址: java入门:用jdbc实现宠物商店管理系统续篇 https://m.mcbbbk.com/newsview314689.html

所属分类:萌宠日常
上一篇: 【关爱动物,尊重生命】流浪动物领
下一篇: 宠物医院合作协议范本(精选5篇)