首页 > 分享 > jdbc实现宠物商店管理系统

jdbc实现宠物商店管理系统

state=conn1.prepareStatement(sql19);6 state=conn1.prepareStatement(sql19);5

state=conn1.prepareStatement(sql19);4 state=conn1.prepareStatement(sql19);3

state=conn1.prepareStatement(sql19);2 state=conn1.prepareStatement(sql19);1

state=conn1.prepareStatement(sql19);0   9

  8   7

  6   5

  4   3   2  1   0

int9int8 int7 int6

int5int4

int3int2

int1int0

result3=state.executeUpdate();9result3=state.executeUpdate();8

result3=state.executeUpdate();7result3=state.executeUpdate();6

result3=state.executeUpdate();5result3=state.executeUpdate();4

result3=state.executeUpdate();3result3=state.executeUpdate();2

result3=state.executeUpdate();1result3=state.executeUpdate();0

  9  8   7   6

  5  4

  3  2  1  0

state=conn1.prepareStatement(sql20);9state=conn1.prepareStatement(sql20);8 state=conn1.prepareStatement(sql20);7

state=conn1.prepareStatement(sql20);6state=conn1.prepareStatement(sql20);5

state=conn1.prepareStatement(sql20);4state=conn1.prepareStatement(sql20);3

state=conn1.prepareStatement(sql20);2state=conn1.prepareStatement(sql20);1state=conn1.prepareStatement(sql20);0  9

  8  7  6  5

  4  3  2

  1  0 int9int8int7

int6int5int4int3

int2int1int0result4=state.executeUpdate();9result4=state.executeUpdate();8result4=state.executeUpdate();7

result4=state.executeUpdate();6result4=state.executeUpdate();5

result4=state.executeUpdate();4result4=state.executeUpdate();3result4=state.executeUpdate();2result4=state.executeUpdate();1

result4=state.executeUpdate();0  9  8   7

  6  5

  4  3  2  1

  0state=conn1.prepareStatement(sql21);9

state=conn1.prepareStatement(sql21);8state=conn1.prepareStatement(sql21);7

state=conn1.prepareStatement(sql21);6state=conn1.prepareStatement(sql21);5

state=conn1.prepareStatement(sql21);4state=conn1.prepareStatement(sql21);3

state=conn1.prepareStatement(sql21);2state=conn1.prepareStatement(sql21);1

state=conn1.prepareStatement(sql21);0  9

  8  7   6   5

  4  3

  2  1  0int9

int8int7 int6

int5int4

int3int2

int1int0result5=state.executeUpdate();9result5=state.executeUpdate();8

result5=state.executeUpdate();7result5=state.executeUpdate();6result5=state.executeUpdate();5result5=state.executeUpdate();4

result5=state.executeUpdate();3result5=state.executeUpdate();2result5=state.executeUpdate();1

result5=state.executeUpdate();0  9   8  7  6

  5  4  3  2

  1  0if9if8if7if6

if5if4

if3if2if1if0

(result2>9(result2>8(result2>7 (result2>6

(result2>5(result2>4

(result2>3(result2>2(result2>1(result2>0

0908

0706

0504

0302

0100

&& result3>9&& result3>8

&& result3>7&& result3>6 && result3>5 && result3>4

&& result3>3&& result3>2

&& result3>1&& result3>00908

0706 05

0403

0201

00&& result4>9&& result4>8&& result4>7

&& result4>6&& result4>5&& result4>4&& result4>3

&& result4>2&& result4>1&& result4>0

09080706

05040302

0100

&& result5>9&& result5>8&& result5>7&& result5>6

&& result5>5&& result5>4&& result5>3 && result5>2

&& result5>1&& result5>0

09080706

0504

0302

0100

){9){8

){7){6

){5){4

){3){2 ){1 ){0

  9  8  7  6

  5  4   3

  2  1  0 conn1.commit();9

conn1.commit();8conn1.commit();7 conn1.commit();6

conn1.commit();5conn1.commit();4conn1.commit();3conn1.commit();2conn1.commit();1conn1.commit();0  9

  8  7  6  5

  4  3

  2  1  0System.out.println(9

System.out.println(8System.out.println(7 System.out.println(6

System.out.println(5System.out.println(4 System.out.println(3System.out.println(2

System.out.println(1System.out.println(0

"购买成功"9"购买成功"8"购买成功"7

"购买成功"6"购买成功"5 "购买成功"4"购买成功"3

"购买成功"2"购买成功"1

"购买成功"0);9);8

);7);6 );5);4

);3);2);1);0

  9  8  7  6

  5  4  3

  2  1  0

}9}8}7

}6}5

}4}3

}2}1}0 else9

else8else7else6else5

else4else3

else2else1

else0{9

{8{7

{6{5

{4{3

{2{1

{0  9   8   7

  6  5   4  3  2

  1  0 conn1.rollback();9

conn1.rollback();8conn1.rollback();7conn1.rollback();6 conn1.rollback();5

conn1.rollback();4conn1.rollback();3conn1.rollback();2conn1.rollback();1

conn1.rollback();0  9

  8  7  6  5

  4  3

  2  1

  0}9}8}7

}6}5 }4

}3}2

}1}0  9  8

  7  6  5  4

  3  2

  1  0buyPet(ownerName,uid);9

buyPet(ownerName,uid);8buyPet(ownerName,uid);7buyPet(ownerName,uid);6buyPet(ownerName,uid);5

buyPet(ownerName,uid);4buyPet(ownerName,uid);3buyPet(ownerName,uid);2buyPet(ownerName,uid);1

buyPet(ownerName,uid);0  9

  8  7  6  5

  4  3

  2  1  0

}9}8 }7}6}5

}4}3}2}1

}0else9else8else7

else6else5else4else3

else2else1else0{9

{8{7{6{5

{4{3

{2{1

{0  9  8  7

  6  5  4  3

  2  1

  0System.out.println(9

System.out.println(8System.out.println(7System.out.println(6 System.out.println(5

System.out.println(4System.out.println(3

System.out.println(2System.out.println(1

System.out.println(0"购买失败"9"购买失败"8 "购买失败"7

"购买失败"6"购买失败"5"购买失败"4"购买失败"3

"购买失败"2"购买失败"1

"购买失败"0);9

);8);7 );6);5

);4);3

);2);1

);0  9

  8  7

  6  5   4   3  2   1

  0buyPet(ownerName,uid);9buyPet(ownerName,uid);8buyPet(ownerName,uid);7

buyPet(ownerName,uid);6buyPet(ownerName,uid);5 buyPet(ownerName,uid);4

buyPet(ownerName,uid);3buyPet(ownerName,uid);2buyPet(ownerName,uid);1 buyPet(ownerName,uid);0

  9  8   7

  6  5  4  3  2  1  0

}9}8}7}6

}5}4

}3}2}1}0

 9 8  7

 6 5  4 3

 2 1

 0} 9} 8

} 7} 6 } 5} 4

} 3} 2

} 1} 0catch9

catch8catch7 catch6catch5

catch4catch3

catch2catch1catch0

(SQLException e) {9(SQLException e) {8(SQLException e) {7

(SQLException e) {6(SQLException e) {5(SQLException e) {4

(SQLException e) {3(SQLException e) {2

(SQLException e) {1(SQLException e) {0

  9  8  7   6

  5  4  3  2

  1  0

e.printStackTrace();9e.printStackTrace();8

e.printStackTrace();7e.printStackTrace();6

e.printStackTrace();5e.printStackTrace();4

e.printStackTrace();3e.printStackTrace();2

e.printStackTrace();1e.printStackTrace();0

 9 8  7  6 5  4

 3 2

 1 0}9}8}7}6

}5}4 }3

}2}1

}0 9

 8 7 6 5

 4 3 2

 1 0 } 9} 8} 7

} 6} 5} 4} 3

} 2} 1} 0catch9catch8catch7

catch6catch5

catch4catch3catch2catch1

catch0(InputMismatchException e) {9(InputMismatchException e) {8 (InputMismatchException e) {7

(InputMismatchException e) {6(InputMismatchException e) {5

(InputMismatchException e) {4(InputMismatchException e) {3

(InputMismatchException e) {2(InputMismatchException e) {1

(InputMismatchException e) {0 9

 8 7

 6 5  4  3 2  1

 0System.out.println(9System.out.println(8System.out.println(7

System.out.println(6System.out.println(5 System.out.println(4

System.out.println(3System.out.println(2System.out.println(1 System.out.println(0

"输入有误"9"输入有误"8 "输入有误"7

"输入有误"6"输入有误"5"输入有误"4"输入有误"3

"输入有误"2"输入有误"1 "输入有误"0

);9);8);7);6);5);4);3);2

);1);0

 9 8  7

 6 5

 4 3

 2 1 0

showPetAll(ownerName,uid);9showPetAll(ownerName,uid);8

showPetAll(ownerName,uid);7showPetAll(ownerName,uid);6showPetAll(ownerName,uid);5showPetAll(ownerName,uid);4

showPetAll(ownerName,uid);3showPetAll(ownerName,uid);2showPetAll(ownerName,uid);1showPetAll(ownerName,uid);0 9 8 7 6

 5 4

 3 2  1

 0}9}8}7}6}5

}4}3

}2}1 }0

 9 8 7 6 5 4

 3 2

 1 0 } 9

} 8} 7

} 6} 5} 4 } 3} 2} 1

} 0catch9

catch8catch7catch6catch5catch4catch3catch2catch1catch0(SQLException e) {9(SQLException e) {8(SQLException e) {7

(SQLException e) {6(SQLException e) {5

(SQLException e) {4(SQLException e) {3 (SQLException e) {2

(SQLException e) {1(SQLException e) {0 9 8  7 6  5 4  3 2 1

 0e.printStackTrace();9

e.printStackTrace();8e.printStackTrace();7e.printStackTrace();6e.printStackTrace();5

e.printStackTrace();4e.printStackTrace();3e.printStackTrace();2e.printStackTrace();1

e.printStackTrace();0 9

 8 7

 6 5

 4 3 2 1

 0}9}8}7

}6}5

}4}3

}2}1}0  9

 8 7

 6 5

 4 3 2  1

 0}9}8}7

}6}5

}4}3

}2}1

}0}9

}8}7

}6}5

}4}3

}2}1 }0 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) {

 }

 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();

 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 {

  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();

 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()){

  int store_id=rs.getInt("petstore.ID");

  Connection conn1=getConnection();

  conn1.setAutoCommit(false);

  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实现宠物商店管理系统
java入门:用jdbc实现宠物商店管理系统续篇
宠物商店管理系统设计与实现.pptx
宠物商店管理系统功能详解与操作指南
Eclipse+Java+Swing实现宠物商店管理系统
宠物商店中商店购买主人的方法怎样实现
jdbc实现宠物商店管理系统
jsp宠物商店管理系统0ueh8
【宠物商店管理系统】要求采用java面向对象的基本知识,实现宠物商店管理系统
java宠物商店代码

网址: jdbc实现宠物商店管理系统 https://m.mcbbbk.com/newsview989371.html

所属分类:萌宠日常
上一篇: 宠物商店宠物装备类网站pboot
下一篇: #宠物友好商场 #宠物 成都龙湖