jdbc实现宠物商店管理系统
程序员文章站
2022-06-22 11:34:22
用jdbc实现宠物商店管理系统1.开发语言:java2.开发工具:eclipse,mysql数据库3.开发环境:jdk1.84.操作系统:win10这里是运行图片,代码在图片下面 这里是主程序测试...
用jdbc实现宠物商店管理系统
1.开发语言:java
2.开发工具:eclipse,mysql数据库
3.开发环境:jdk1.8
4.操作系统:win10
这里是运行图片,代码在图片下面
这里是主程序测试类test
// main package petstore1; public class test { public static void main(string[] args) { system.out.println("宠物商店启动"); petmanage pm=new petmanage(); pm.showall(); } }
这里是工具类basedao
用来对数据库进行增删改查的一个工具类
// 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 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(); } } }
这里是要用到的所有方法的一个类petmanage
因为我还没学怎么合理的把各种类放到各个包,以及框架什么的,我暂时先放在一个类里面了,繁杂且无序,抱歉
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,卖出宠物请输入2\n1.购买宠物\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(); //查询该商店的id string 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设置为购买主人的id string 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(); } } }
我是一个java学习路上的小白,现在才刚刚开始学,以后学习的路还有很远,用刚学的jdbc来做了一个案例,做的不好的见谅,因为我也没时间去进行优化、升级,只是希望写在这里以后我还能有个回忆,以及给看的人可能带来一点点小收获。
更多学习资料请关注专题《管理系统开发》。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Python字典,函数,全局变量代码解析
下一篇: 校园生活其实就是这样,谁说不是呢
推荐阅读
-
vue权限管理系统的实现代码
-
abp(net core)+easyui+efcore实现仓储管理系统——ABP WebAPI与EasyUI结合增删改查之一(二十七)
-
基于jsp实现新闻管理系统 附完整源码
-
abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十五)
-
python面向对象法实现图书管理系统
-
网站制作教程之PageAdmin网站管理系统(cms)实现多域名访问
-
abp(net core)+easyui+efcore实现仓储管理系统——EasyUI之货物管理三 (二十一)
-
django认证系统实现自定义权限管理的方法
-
abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十二)
-
abp(net core)+easyui+efcore实现仓储管理系统——ABP WebAPI与EasyUI结合增删改查之五(三十一)