1.开发语言:Java
2.开发工具:eclipse,MySql数据库
3.开发环境:jdk1.8
4.操作系统:win10
// Main package petStore1; public class Test {public static void main(String[] args) {System.out.println("宠物商店启动");PetManage pm=new PetManage();pm.showAll();} } 12345678910111213
用来对数据库进行增删改查的一个工具类
// BaseDAO package petStore1; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class BaseDAO {public Connection conn = null;public PreparedStatement state = null;public ResultSet rs = null;public Connection getConnection() {try {Class.forName("com.mysql.jdbc.Driver");conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/petshop", "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 blocke.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();}} }
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182因为我还没学怎么合理的把各种类放到各个包,以及框架什么的,我暂时先放在一个类里面了,繁杂且无序,抱歉
package petStore1; import java.sql.Connection; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.InputMismatchException; import java.util.Scanner; public class PetManage extends BaseDAO{public void showAll(){showPetName();showPetOwner();showPetStore();login();}/** * 显示宠物的姓名以及ID方法 */public void showPetName(){conn=getConnection();String sql="SELECT ID,name from pet";try {state=conn.prepareStatement(sql);rs=state.executeQuery();System.out.println("Wonderland醒来,所有宠物从MySQL中醒来");System.out.println("*************************************");while(rs.next()){int id=rs.getInt("ID");String name=rs.getString("name");System.out.println("第"+id+"只宠物,名字叫:"+name);}System.out.println("*************************************n");} catch (Exception e) {e.printStackTrace();}finally{closeObject1();}}/** * 显示宠物主人方法 */public void showPetOwner(){conn=getConnection();String sql="SELECT pet.ID,petowner.name from petowner,pet where petowner.ID=owner_id";try {state=conn.prepareStatement(sql);rs=state.executeQuery();System.out.println("所有宠物主人从MySQL中醒来");System.out.println("*************************************");while(rs.next()){int id=rs.getInt("pet.ID");String name=rs.getString("petowner.name");System.out.println("第"+id+"只宠物主人,名字叫:"+name);}System.out.println("*************************************n");} catch (Exception e) {e.printStackTrace();}finally{closeObject1();}}/** * 显示宠物商店方法 */public void showPetStore(){conn=getConnection();String sql="SELECT name from petstore";try {state=conn.prepareStatement(sql);rs=state.executeQuery();System.out.println("所有宠物商店从MySQL中醒来");System.out.println("*************************************");while(rs.next()){String name=rs.getString("name");System.out.println("我的名字叫:"+name);}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 name from petowner where 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,name,money from petowner where name=?"; //state=conn.prepareStatement(sql2); //state.setString(1, name); //rs=state.executeQuery();rs=search(sql2,name);if(rs.next()){int uid=rs.getInt("ID");String uname=rs.getString("name");Double uMoney=rs.getDouble("money");System.out.println("姓名:"+uname);System.out.println("元宝数:"+uMoney);System.out.print("登录成功,");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.返回上一级");try {Scanner input2=new Scanner(System.in);int choise2=input2.nextInt();if(choise2<1||choise2>3){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;default:break;}}} catch (InputMismatchException e) {System.out.println("输入有误");dealPet(ownerName,uid);}}/** * 显示主人拥有的宠物 */public void showSellPet(String ownerName,int uid) {conn=getConnection();String sql25="SELECT pet.ID,pet.name from petowner,pet where petowner.ID=owner_id and petowner.ID="+uid+"";try {state=conn.prepareStatement(sql25);rs=state.executeQuery();System.out.println("以下是你拥有的宠物:"); ////如果结果集为空,即该主人没有宠物,就返回上一级进行选择 //if(!rs.next()){ //System.out.println("您没有宠物,将自动返回上一级"); //buyPet(ownerName, uid); //}while(rs.next()){int petid=rs.getInt("pet.ID");String petName=rs.getString("pet.name");System.out.println("这是"+petid+"号宠物,名字叫:"+petName);}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();System.out.println("请输入你要卖给的商店n1.北京西苑t2.重庆观音桥");int choiseStore=input27.nextInt();String sql30="SELECT pet.ID,pet.name from petowner,pet where petowner.ID=owner_id and petowner.ID="+uid+" and pet.ID="+choisePetId+"";Connection conn6=getConnection();try {state=conn6.prepareStatement(sql30);rs=state.executeQuery();if(rs.next()){Connection conn9=getConnection();conn9.setAutoCommit(false);String sql40="UPDATE pet set owner_id=null,store_id="+choiseStore+" where pet.ID="+choisePetId+"";state=conn9.prepareStatement(sql40);int result20=state.executeUpdate();String sql41="update petowner set money=money+5 where petowner.ID="+uid+"";state=conn9.prepareStatement(sql41);int result21=state.executeUpdate();String sql42="update petstore set balance=balance-5 where petstore.ID="+choiseStore+"";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 (deal_type,pet_id,seller_id,buyer_id,price,deal_time) VALUES (2,"+choisePetId+","+choiseStore+","+uid+",5,'"+dealTime+"')";state=conn9.prepareStatement(sql43);int result23=state.executeUpdate();if(result20>0 && result21>0 && result22>0 & result23>0){//提交事务conn9.commit();System.out.println("卖出成功");}else{//回滚事务conn9.rollback();}dealPet(ownerName,uid);}else{System.out.println("没有该宠物,卖出失败");dealPet(ownerName,uid);}} catch (SQLException e) {e.printStackTrace();}} 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 name,balance from petstore where name=? and password=?";state=conn.prepareStatement(sq110);rs=search(sq110, name,password);if(rs.next()){System.out.println("登录成功");PetStoreMake(name);}else{System.out.println("登录失败");login();}} catch (Exception e) {// TODO: handle exception}return false;}/* * 宠物商店培育新宠物 */public void PetStoreMake(String storeName) throws SQLException {System.out.println("请输入数字进行选择:n1.查询店内宠物n2.培育新宠物n3.退出登录");try {Scanner input=new Scanner(System.in);int choise7=input.nextInt();if(choise7<1||choise7>3){System.out.println("输入有误");PetStoreMake(storeName);}else{switch (choise7) {case 1:storePetQuery(storeName);break;case 2:storeAddPet(storeName);break;case 3://退出登录,返回上一级login();break;default:break;}}} catch (InputMismatchException e) {System.out.println("输入有误");PetStoreMake(storeName);}}/** * 宠物商店培育新宠物的方法 * @param storeName * @throws SQLException */public void storeAddPet(String storeName) throws SQLException {System.out.println("请输入你想添加的宠物的类型:");Scanner input=new Scanner(System.in);String typename=input.next();System.out.println("请输入该宠物的名字:");String petname=input.next();//查询该商店的IDString sql14="SELECT ID from petstore where name='"+storeName+"'";rs=search(sql14);int id=0;if(rs.next()){id=rs.getInt("ID");}conn=getConnection();String sql13="insert into pet (name,typename,health,love,birthday,store_id,neworold)VALUES(?,?,1,100,?,"+id+",'new')";//获取当前时间,作为宠物的生日Long time1=System.currentTimeMillis();SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");String birth=sdf.format(time1);int a=update(sql13, petname,typename,birth);if(a>0){System.out.println("培育新宠物成功");PetStoreMake(storeName);}else{System.out.println("培育新宠物失败");PetStoreMake(storeName);}}/** * 在商店登录之后进行对店内的宠物进行查询 * @param storeName */public void storePetQuery(String storeName) {System.out.println("正在查询店内宠物。。。");conn=getConnection();String sql11="SELECT pet.ID,pet.name,typename,birthday from pet,petstore where petstore.name=? and pet.store_id=petstore.ID";try {state=conn.prepareStatement(sql11);rs=search(sql11, storeName);int i=1;while(rs.next()){int id=rs.getInt("pet.ID");String name=rs.getString("pet.name");String typename=rs.getString("typename");String birthday=rs.getString("birthday");System.out.println("第"+i+"只宠物名字:"+name+",宠物类型:"+typename+",生日:"+birthday);i++;}System.out.println("----------------------------");PetStoreMake(storeName);} 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);break;case 2:buyNewPet(ownerName,uid);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) {//用于判断查询是否有结果boolean havePet=false;System.out.println("正在帮你查询新宠物。。。。。");conn=getConnection();String sql31="SELECT pet.ID,pet.name from pet where pet.neworold='new'";try {state=conn.prepareStatement(sql31);rs=state.executeQuery();while(rs.next()){int petid=rs.getInt("pet.ID");String petName=rs.getString("pet.name");System.out.println("序号为:"+petid+",名字为:"+petName);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 pet.ID,pet.name,pet.store_id from pet where pet.neworold='new' and pet.ID="+newPetId+"";state=conn7.prepareStatement(sql32);rs=state.executeQuery();if(rs.next()){int storeid=rs.getInt("pet.store_id");Connection conn8=getConnection();conn8.setAutoCommit(false);String sql33="UPDATE pet set pet.neworold='old',pet.owner_id="+uid+",pet.store_id=null where pet.ID="+newPetId+"";String sql34="update petowner set money=money-5 where petowner.ID="+uid+"";String sql35="update petstore set balance=balance+5 where petstore.ID="+storeid+"";//获得当前时间Long time1=System.currentTimeMillis();SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");String dealTime=sdf.format(time1);//将该条交易添加至交易账单中String sql36="insert into account (deal_type,pet_id,seller_id,buyer_id,price,deal_time) VALUES (1,"+newPetId+","+storeid+","+uid+",5,'"+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{//如果中加你有一条没有执行成功那就回滚事务conn8.rollback();}buyPet(ownerName, uid);}else{System.out.println("输入错误,没有该序号的新宠物");buyNewPet(ownerName, uid);}} catch (InputMismatchException e) {e.printStackTrace();}}else{System.out.println("暂时还没新宠物");buyPet(ownerName, uid);}} catch (SQLException e) {e.printStackTrace();}}/** * 展示宠物名字,序号,类型的方法 */public void showPetAll(String ownerName,int uid) {System.out.println("---------以下是库存宠物--------");conn=getConnection();String sql6="SELECT pet.ID,pet.name,pet.typename from pet,petstore where pet.store_id=petstore.ID";try {state=conn.prepareStatement(sql6);rs=state.executeQuery();while(rs.next()){int petId=rs.getInt("ID");String petName=rs.getString("name");String petType=rs.getString("typename");System.out.println("序号:"+petId+",我的名字叫:"+petName+",我是:"+petType+",要购买我要花:5.0个元宝");}System.out.println("请输入你想购买的宠物编号:");try {Scanner input17=new Scanner(System.in);int choise6=input17.nextInt();//对在商店里的宠物进行ID查询,符合的就购买conn=getConnection();String sql15="SELECT pet.ID,pet.name,pet.typename,petstore.ID from pet,petstore where pet.store_id=petstore.ID and pet.ID="+choise6+"";try {state=conn.prepareStatement(sql15);rs=state.executeQuery();if(rs.next()){//这里是宠物主人购买宠物的代码,将宠物的store_ID设置为null,将宠物的owner_ID设置为购买主人的ID//然后主人账户减钱,商店的结余加钱,将该条交易添加至交易账单中int store_id=rs.getInt("petstore.ID");//这里是选择的宠物所属商店的ID//这里用创建一个新的连接Connection conn1=getConnection();//开启事务conn1.setAutoCommit(false);//将宠物的store_ID设置为null,将宠物的owner_ID设置为购买主人的IDString sql18="update pet set owner_id=1,store_id=NULL where pet.ID="+choise6+"";//宠物主人减钱String sql19="update petowner set money=money-5 where petowner.ID="+uid+"";//宠物商店加钱String sql20="update petstore set balance=balance+5 where petstore.ID="+store_id+"";//获得当前时间Long time1=System.currentTimeMillis();SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");String dealTime=sdf.format(time1);//将该条交易添加至交易账单中String sql21="insert into account (deal_type,pet_id,seller_id,buyer_id,price,deal_time) VALUES (1,"+choise6+","+store_id+","+uid+",5,'"+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{//如果中加你有一条没有执行成功那就回滚事务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);}} catch (SQLException e) {e.printStackTrace();}} }
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630相关知识
Java入门:用jdbc实现宠物商店管理系统
java入门:用jdbc实现宠物商店管理系统续篇
[附源码]java毕业设计宠物商店管理系统
java宠物商店项目
宠物商店详细设计说明书
基于java的宠物管理系统设计与实现
java ssm宠物医院诊断预约管理系统springboot
基于java+springboot的宠物商店、宠物管理系统设计与实现
毕业设计:基于java的宠物管理系统设计与实现
宠物商店 案例分析
网址: Java入门:用jdbc实现宠物商店管理系统 https://m.mcbbbk.com/newsview405790.html
上一篇: 毕设项目案例实战II基于SSM的 |
下一篇: 基于SpringBoot+Vue |