Java宠物商店连接数据库

package 实训2;

public abstract class Pet {
private String name;
private String sex;
private int age;
private String kind;
private int price;
public Pet() {
super();
}
public Pet(String name, String sex, int age, String kind, int price) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.kind = kind;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}

public abstract void eat();

public abstract void howl();

public abstract void show();

public abstract void run();

}
package 实训2;

public class Cat extends Pet {
private String character;

public Cat() {
    super();
}

public Cat(String name, String sex, int age, String kind, int price,String character) {
    super(name,sex,age,kind,price);
    this.character = character;
}

public String getCharacter() {
    return character;
}

public void setCharacter(String character) {
    this.character = character;
}

@Override
public void eat() {

}

@Override
public void howl() {

}

@Override
public void show() {
    System.out.println(this.toString());
}

@Override
public void run() {

}

public void actingCute() {

}

@Override
public String toString() {
    return "猫:"  + "\t姓名:" + this.getName() +  
    "\t性别:" + this.getSex() + "\t年龄:" + this.getAge() + 
    "\t种类:"+ this.getKind() + "\t价格:" + this.getPrice() 
     + "\t性格:" + this.getCharacter();
}

}
package 实训2;

public class Dog extends Pet {
private String hobby;

public Dog() {
    super();
}

public Dog(String name, String sex, int age, String kind, int price,String hobby) {
    super(name,sex,age,kind,price);
    this.hobby = hobby;
}
public String getHobby() {
    return hobby;
}

public void setHobby(String hobby) {
    this.hobby = hobby;
}

@Override
public void eat() {

}

@Override
public void howl() {

}

@Override
public void show() {
    System.out.println(this.toString());
}

@Override
public void run() {

}

public void dealer() {

}

@Override
public String toString() {
    return "狗:"  + "\t姓名:" + this.getName() +  
    "\t性别:" + this.getSex() + "\t年龄:" + this.getAge() + 
    "\t种类:"+ this.getKind() + "\t价格:" + this.getPrice() 
    + "\t兴趣:" + this.getHobby();
}

}
package 实训2;

import java.util.Random;

public class PetShop {
private String name;
private Pet[] pets = new Pet[10];
private int count = 0;

public PetShop(String name) {
    this.name = name;
    addPet(new Dog("小黑", "雄", 2, "拉布拉多", 75, "散步"));
    addPet(new Dog("andy", "雌", 1, "牧羊犬", 86, "游泳"));
    addPet(new Dog("追风", "雄", 1, "藏獒", 156, "睡觉"));
    addPet(new Dog("小小", "雌", 2, "拉布拉多", 86, "吃零食"));
    addPet(new Dog("阿强", "雄", 2, "中华田园犬", 999, "遥望远方"));
    addPet(new Cat("莉莉", "母", 4, "英国短毛猫", 68, "恬静"));
    addPet(new Cat("露西", "母", 6, "布偶猫", 153, "粘人"));
    addPet(new Cat("默默", "母", 2, "中华田园猫", 648, "温柔"));
    addPet(new Cat("小名", "公", 8, "折耳猫", 243, "聪明"));
    addPet(new Cat("peter", "母", 8, "缅因猫", 333, "调皮"));
    addPet(new Cat("阿布", "公", 8, "波斯猫", 293, "傲娇"));
    addPet(new Cat("小罗", "公", 3, "暹罗猫", 98, "害羞"));
}
public boolean hasDogKind(String kind) {
    for (Pet pet : pets) {
        if (pet instanceof Dog) {
            Dog d = (Dog) pet;
            boolean equals = d.getKind().equals(kind);
            if (equals) {
                return true;
            }
        }
    }
    return false;
}

public String getName() {
    return name;
}

public int size() {
    return count;
}
private boolean addPet(Pet pet) {
    if (count >= pets.length) {
        return false;
    }
    pets[count] = pet;
    count++;
    return true;
}
public boolean purchasePet(Pet pet) {
    return this.addPet(pet);
}
public Pet buyDog(String kind) {
    int kindNum = 0;
    for (int i = 0; i < pets.length; i++) {
        Pet pet = pets[i];
        if (pet instanceof Dog) {
            Dog d = (Dog) pet;
            boolean equals = d.getKind().equals(kind);
            if (equals) {
                kindNum++;
            }
        }
    }
    int[] kindIndex = new int[kindNum];
    int k = 0;
    for (int i = 0; i < pets.length; i++) {
        Pet pet = pets[i];
        if (pet instanceof Dog) {
            Dog d = (Dog) pet;
            boolean equals = d.getKind().equals(kind);
            if (equals) {
                kindIndex[k++] = i;
            }
        }
    }
    Random r = new Random();
    int nextInt = r.nextInt(kindIndex.length);
    int i = kindIndex[nextInt];

    Pet result = pets[i];
    capaticyConfirm(i);
    return result;
}
private void capaticyConfirm(int index) {
    int tag = index;
    while (tag < count - 1) {
        pets[tag] = pets[tag + 1];
        tag++;
    }
    pets[--count] = null;
}
public void showPets() {
    for (int i = 0; i < count; i++) {
        pets[i].show();
    }
}
public boolean hasCatCharacter(String character) {
    for (Pet pet : pets) {
        if (pet instanceof Cat) {
            Cat c = (Cat) pet;
            boolean equals = c.getCharacter().equals(character);
            if (equals) {
                return true;
            }
        }
    }
    return false;
}
public Pet buyCat(String character) {
    int kindNum = 0;
    for (int i = 0; i < pets.length; i++) {
        Pet pet = pets[i];
        if (pet instanceof Cat) {
            Cat c = (Cat) pet;
            boolean equals = c.getCharacter().equals(character) ;
            if (equals) {
                kindNum++;
            }
        }
    }
    int[] kindIndex = new int[kindNum];
    int k = 0;
    for (int i = 0; i < pets.length; i++) {
        Pet pet = pets[i];
        if (pet instanceof Cat) {
            Cat c = (Cat) pet;
            boolean equals = c.getCharacter().equals(character) ;
            if (equals) {
                kindIndex[k++] = i;
            }
        }
    }

    Random r = new Random();
    int nextInt = r.nextInt(kindIndex.length);
    int i = kindIndex[nextInt];

    Pet result = pets[i];
    capaticyConfirm(i);
    return result;
}

}
package 实训2;

import java.util.Scanner;

public class Main {
static Scanner sc = new Scanner(System.in);
PetShop petShop = new PetShop("宠物店");

public static void main(String[] args) {
    Main main = new Main();
    main.start();
}
private void start() {
    while (true) {
        System.out.println("欢迎来到" + petShop.getName() + "!!" + "你需要:" + "\n1.买宠物?  2.出售宠物?  3.查看所有宠物?  0.还是随便看看然后离开?");
        int choise = sc.nextInt();
        switch (choise) {
        case 1:
            this.buyPetView();
            break;
        case 2:
            this.purchasePetView();
            break;
        case 3:
            petShop.showPets();
            break;

        default:
            System.out.println("欢迎下次再来!!");
            return;
        }
    }
}
private void purchasePetView() {
    int choise = inputInt("你的宠物是?\n1.狗\t2.猫");
    switch (choise) {
    case 1:
        purchaseDogView();
        break;
    case 2:
        purchaseCatView();
        break;

    default:
        break;
    }
}
private void purchaseCatView() {
    String name = inputStr("猫的名字是?");
    String sex = inputStr("猫的性别是?");
    int age = inputInt("猫的年龄是?");
    String kind = inputStr("猫的种类是?");
    int price = inputInt("猫的价格是?");
    String character = inputStr("猫的性格是?");
    Cat cat = new Cat(name, sex, age, kind,price,character);
    boolean purchasePet = petShop.purchasePet(cat);
    System.out.println(purchasePet ? "收购成功!" : "收购失败!");
}
private void purchaseDogView() {
    String name = inputStr("狗的名字是?");
    String sex = inputStr("狗的性别是?");
    int age = inputInt("狗的年龄是?");
    String kind = inputStr("狗的种类是?");
    int price = inputInt("狗的价格是?");
    String hobby = inputStr("狗的兴趣是?");
    Cat cat = new Cat(name, sex, age, kind,price,hobby);
    boolean purchasePet = petShop.purchasePet(cat);
    System.out.println(purchasePet ? "收购成功!" : "收购失败!");
}
private void buyPetView() {
    switch (inputInt("需要买什么宠物?\n1.狗\t2.猫")) {
    case 1:
        buyDogView();
        break;
    case 2:
        buyCatView();
        break;

    default:
        break;
    }
}
private void buyCatView() {
    while (true) {
        String character = inputStr("需要什么性格的猫咪?");
        boolean hasCatCharacter = petShop.hasCatCharacter(character);
        if (!hasCatCharacter) {
            System.out.println("你选择的性格本店没有,请重新输入!");
            continue;
        }
        Pet buyCat = petShop.buyCat(character);
        buyCat.show();
        return;
    }
}
private void buyDogView() {
    while (true) {
        String kind = inputStr("要什么品种的狗?");
        boolean hasDogKind = petShop.hasDogKind(kind);
        if (hasDogKind) {
            Pet buyDog = petShop.buyDog(kind);
            buyDog.show();
            return;
        }else{
            System.out.println("你选择的品种本店没有,请重新输入!");
            continue;
    }
}

}
public int inputInt(String tip) {
System.out.println(tip);
return sc.nextInt();
}
public String inputStr(String tip) {
System.out.println(tip);
return sc.next();
}
}

将这些代码连接到数据库,该怎么操作?

jdbc

把原来操作的数据使用数据库来存取
比如

img


这些数据存数据库
主要把这个对象的属性和数据库字段直接映射

img

你好,将你这个demo连接上数据库,主要有如下操作步骤:
1.安装一个centos7虚拟机,在虚拟机上安装一个MySQL数据库;
2.window上使用sqlyog连接数据库,建表;
3.在你这个demo中引入Java数据库依赖,创建一个jdbc增删改查数据的工具类;
4.将demo中数据操作换成jdbc的数据库操作;
我只是说了一个步骤,每一步都需要百度。
做完这个可以学习一下spring、springmvc、mybatis,后续工作中应用多一些。

这个需要jdbc操作不知道你会不会,也可以使用mybatis这些框架

加上数据库连接配置,然后通过sql存入数据库

需要用上jdbc,建议博主可以去B站学习一下

猫狗存入数据库
猫狗从数据库读取
SPT语句插入和读取(Insert、select)
用ODBC链接数据库即可

java怎么与数据库连接?
https://blog.csdn.net/weixin_45987729/article/details/124387545
宠物商店数据库设计
https://blog.csdn.net/ifubing/article/details/115743256

// 驱动
private static final String DRIVER = "com.mysql.jdbc.Driver";
// 地址
private static final String URL = "jdbc:mysql://localhost:3306/test";
//用户名
private static final String USER_NAME = "root";
// 密码
private static final String PSW = "123456";

/**
 *  获取连接
 */
public static Connection getConnection(){
  Connection conn = null;
  try {
    // 加载驱动
    Class.forName(DRIVER);
    // 数据库连接
    conn = DriverManager.getConnection(URL,USER_NAME,PSW);

  } catch (ClassNotFoundException e) {
    System.out.println("加载驱动失败,请检查是否引入Jar包或者驱动名称是否正确");
    throw new RuntimeException("加载驱动失败,请检查是否引入Jar包或者驱动名称是否正确",e);
  } catch (SQLException throwables) {
    System.out.println("连接数据库失败,请检查数据库地址,用户名,密码是否正确");
    throw new RuntimeException("连接数据库失败,请检查数据库地址,用户名,密码是否正确",throwables);
  }
  return conn;
}

/**
* 关闭连接
* @param conn
*/
public static void close(Connection conn){
  if(conn != null){
    try {
      conn.close();
    } catch (SQLException throwables) {
      throwables.printStackTrace();
    }
  }
}