欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jdbc實(shí)現(xiàn)寵物商店管理系統(tǒng)

 更新時間:2020年10月29日 13:56:00   作者:黃庭輝Leo  
這篇文章主要為大家詳細(xì)介紹了jdbc實(shí)現(xiàn)寵物商店管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

用jdbc實(shí)現(xiàn)寵物商店管理系統(tǒng)

1.開發(fā)語言:Java
2.開發(fā)工具:eclipse,MySql數(shù)據(jù)庫
3.開發(fā)環(huán)境:jdk1.8
4.操作系統(tǒng):win10

這里是運(yùn)行圖片,代碼在圖片下面

這里是主程序測試類Test

// Main
package petStore1;

public class Test {

 public static void main(String[] args) {
 System.out.println("寵物商店啟動");
 PetManage pm=new PetManage();
 pm.showAll();

 }
}

這里是工具類BaseDAO

用來對數(shù)據(jù)庫進(jìn)行增刪改查的一個工具類

// 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

因?yàn)槲疫€沒學(xué)怎么合理的把各種類放到各個包,以及框架什么的,我暫時先放在一個類里面了,繁雜且無序,抱歉

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.退出系統(tǒng)\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("元寶數(shù):"+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("以下是你擁有的寵物:");
// //如果結(jié)果集為空,即該主人沒有寵物,就返回上一級進(jìn)行選擇
// 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();
  //獲得當(dāng)前時間
  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){
  //提交事務(wù)
  conn9.commit();
  System.out.println("賣出成功");
  }else{
  //回滾事務(wù)
  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("請輸入數(shù)字進(jìn)行選擇:\n1.查詢店內(nèi)寵物\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')";
 //獲取當(dāng)前時間,作為寵物的生日
 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);
 }
 }

 /**
 * 在商店登錄之后進(jìn)行對店內(nèi)的寵物進(jìn)行查詢
 * @param storeName
 */
 public void storePetQuery(String storeName) {
 System.out.println("正在查詢店內(nèi)寵物。。。");
 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("請輸入選擇購買范圍,只輸入選擇項(xiàng)的序號");
 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) {
 //用于判斷查詢是否有結(jié)果
 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+"";
  
  //獲得當(dāng)前時間
  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){
  //如果都成功執(zhí)行,改變數(shù)據(jù),那就提交事務(wù)
  conn8.commit();
  System.out.println("購買成功");
  }else{
  //如果中加你有一條沒有執(zhí)行成功那就回滾事務(wù)
  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();
 //對在商店里的寵物進(jìn)行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設(shè)置為null,將寵物的owner_ID設(shè)置為購買主人的ID
  //然后主人賬戶減錢,商店的結(jié)余加錢,將該條交易添加至交易賬單中
  int store_id=rs.getInt("petstore.ID");//這里是選擇的寵物所屬商店的ID
  //這里用創(chuàng)建一個新的連接
  Connection conn1=getConnection();
  //開啟事務(wù)
  conn1.setAutoCommit(false);
  //將寵物的store_ID設(shè)置為null,將寵物的owner_ID設(shè)置為購買主人的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+"";
  //獲得當(dāng)前時間
  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){
  //如果都成功執(zhí)行,改變數(shù)據(jù),那就提交事務(wù)
  conn1.commit();
  System.out.println("購買成功");
  }else{
  //如果中加你有一條沒有執(zhí)行成功那就回滾事務(wù)
  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學(xué)習(xí)路上的小白,現(xiàn)在才剛剛開始學(xué),以后學(xué)習(xí)的路還有很遠(yuǎn),用剛學(xué)的jdbc來做了一個案例,做的不好的見諒,因?yàn)槲乙矝]時間去進(jìn)行優(yōu)化、升級,只是希望寫在這里以后我還能有個回憶,以及給看的人可能帶來一點(diǎn)點(diǎn)小收獲。

更多學(xué)習(xí)資料請關(guān)注專題《管理系統(tǒng)開發(fā)》。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論