基于Mysql数据库的实现,要完成一个宠物管理系统,这是要求
package com.gg.pet.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.gg.entity.Pet;
import com.gg.jdbc.helper.DBHelper;
public class PetDao {
//1 显示所有的宠物
public static List showAllPets() throws Exception{
List pets = new ArrayList();
//1
Connection con = DBHelper.getConnection();
//2 SQL
String sql = "select * from pet ";
//3
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs= ps.executeQuery();
while(rs.next()) {
String pid = rs.getString("pid");
String pname = rs.getString("pname");
String pgender = rs.getString("pgender");
int page = rs.getInt("page");
Pet pet = new Pet(pid, pname, page, pgender);
pets.add(pet);
}
return pets ;
}
//2
//3
//4
}
package com.gg.pet.view;
import java.util.List;
import java.util.Scanner;
import com.gg.entity.Pet;
import com.gg.pet.dao.PetDao;
public class MainView {
//主界面
public static void showMainView() throws Exception {
while(true) {
Scanner scanner = new Scanner(System.in);
System.out.println("请选择 1~4 ");
System.out.println("**********************************");
System.out.println("* 1.显示所用宠物 *");
System.out.println("* 2.添加宠物 *");
System.out.println("* 3.按编号修改宠物 *");
System.out.println("* 4.按编号删除宠物 *");
System.out.println("* *");
System.out.println("* *");
System.out.println("**********************************");
int number = scanner.nextInt();
switch (number) {
case 1:
List pets = PetDao.showAllPets();
for (Pet pet : pets) {
System.out.println(pet);
}
break;
case 2:
break;
case 3:
break;
case 4:
break;
default:
System.out.println("输入有问题!!!");
break;
}
}
}
}
参考GPT和自己的思路:对于宠物管理系统的部分实现代码,只有宠物查询功能已经实现,其他的添加、修改和删除功能还没有实现,需要继续补充完善。同时,代码中没有涉及到数据库的连接信息和表的设计信息,需要在代码中添加这些内容。
以下是对代码的改进建议:
在PetDao类中添加数据库连接信息和表的设计信息,例如数据库的URL、用户名和密码,以及pet表的定义信息。
在PetDao类中添加宠物添加、修改和删除功能的代码,与现有查询功能相似,使用PreparedStatement执行相应的SQL语句。
在MainView类中,补充宠物添加、修改和删除操作的用户交互界面和逻辑,例如输入宠物信息、编号等。
在MainView类中,可以使用异常处理机制来处理SQL执行过程中可能出现的异常,例如SQL语句错误、数据库连接失败等。
在Pet类中,可以添加宠物类型、宠物主人信息等属性,以构建更为完整的宠物管理系统。
通过以上改进,可以将代码实现为更为完整、可用的宠物管理系统。
针对宠物管理系统,你已经完成了显示所有宠物的功能,现在需要你完成添加宠物、按编号修改宠物、按编号删除宠物的功能。
添加宠物需要用户输入宠物的详细信息,然后将信息插入到数据库中。可以使用PreparedStatement来执行插入操作,代码如下:
public static boolean addPet(Pet pet) throws Exception {
Connection con = DBHelper.getConnection();
String sql = "insert into pet (pid, pname, pgender, page) values (?, ?, ?, ?)";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, pet.getPid());
ps.setString(2, pet.getPname());
ps.setString(3, pet.getPgender());
ps.setInt(4, pet.getPage());
int result = ps.executeUpdate();
ps.close();
con.close();
return result > 0;
}
按编号修改宠物需要用户输入要修改的宠物编号和新的宠物信息,然后根据编号更新数据库中的宠物信息。可以使用PreparedStatement来执行更新操作,代码如下:
public static boolean updatePet(String pid, Pet pet) throws Exception {
Connection con = DBHelper.getConnection();
String sql = "update pet set pname = ?, pgender = ?, page = ? where pid = ?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, pet.getPname());
ps.setString(2, pet.getPgender());
ps.setInt(3, pet.getPage());
ps.setString(4, pid);
int result = ps.executeUpdate();
ps.close();
con.close();
return result > 0;
}
按编号删除宠物需要用户输入要删除的宠物编号,然后根据编号从数据库中删除宠物信息。可以使用PreparedStatement来执行删除操作,代码如下:
public static boolean deletePet(String pid) throws Exception {
Connection con = DBHelper.getConnection();
String sql = "delete from pet where pid = ?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, pid);
int result = ps.executeUpdate();
ps.close();
con.close();
return result > 0;
}
根据题主的代码风格和数据库定义写的代码,纯手写代码,如果对您有帮助希望点一下采纳
宠物的实体类定义
```java
package com.gg.entity;
public class Pet {
private String pId;
private String pName;
private String pGender;
private int pAge;
public Pet() {}
public Pet(String pId, String pName, String pGender, int pAge) {
this.pAge = pAge;
this.pId = pId;
this.pName = pName;
this.pGender = pGender;
}
public String getpId() {
return pId;
}
public void setpId(String pId) {
this.pId = pId;
}
public String getpName() {
return pName;
}
public void setpName(String pName) {
this.pName = pName;
}
public String getpGender() {
return pGender;
}
public void setpGender(String pGender) {
this.pGender = pGender;
}
public int getpAge() {
return pAge;
}
public void setpAge(int pAge) {
this.pAge = pAge;
}
@Override
public String toString() {
return "Pet{" +
"pId='" + pId + '\'' +
", pName='" + pName + '\'' +
", pGender='" + pGender + '\'' +
", pAge=" + pAge +
'}';
}
}
数据库操作类的定义
```java
package com.gg.pet.dao;
import com.gg.entity.Pet;
import com.gg.pet.dao.com.gg.jdbc.helper.DBHelper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class PetDao {
//查询宠物,如果pId为空则查询所有的宠物
public static List<Pet> queryPet(String pId) {
String sql = String.format("select * from pet %s",
pId == null || "".equals(pId) ? "" : " where pid = ?");
List<Pet> resultList = new ArrayList<>();
Connection connection = null;
ResultSet rs = null;
PreparedStatement preparedStatement = null;
try {
connection = DBHelper.getConnection();
preparedStatement = connection.prepareStatement(sql);
if (pId != null && !"".equals(pId)) {
preparedStatement.setString(1, pId);
}
rs = preparedStatement.executeQuery();
while (rs.next()) {
String pid = rs.getString("pid");
String pname = rs.getString("pname");
String pgender = rs.getString("pgender");
int page = rs.getInt("page");
resultList.add(new Pet(pid, pname, pgender, page));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null)
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return resultList;
}
//修改宠物信息
public static boolean updatePet(Pet pet) {
String sql = "update pet set pname = ?, pgender = ?, page = ? where pid = ?";
try (Connection connection = DBHelper.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setString(1, pet.getpName());
preparedStatement.setString(2, pet.getpGender());
preparedStatement.setInt(3, pet.getpAge());
preparedStatement.setString(4, pet.getpId());
return preparedStatement.executeUpdate() > 0;
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return false;
}
//根据宠物编号删除宠物
public static boolean deletePet(String pId) {
String sql = "delete from pet where pid = ?";
try (Connection connection = DBHelper.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql);) {
preparedStatement.setString(1, pId);
return preparedStatement.executeUpdate() > 0;
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return false;
}
//新增宠物
public static boolean addPet(Pet pet) {
String sql = "insert into pet(pid, pname, pgender, page) values (?, ?, ?, ?)";
try (Connection connection = DBHelper.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setString(1, pet.getpId());
preparedStatement.setString(2, pet.getpName());
preparedStatement.setString(3, pet.getpGender());
preparedStatement.setInt(4, pet.getpAge());
return preparedStatement.executeUpdate() > 0;
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return false;
}
}
测试方法类
package com.gg.pet.dao;
import com.gg.entity.Pet;
import java.util.List;
import java.util.Scanner;
import java.util.UUID;
public class PetTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择 1~4 ");
System.out.println("**********************************");
System.out.println("* 1.显示所用宠物 *");
System.out.println("* 2.添加宠物 *");
System.out.println("* 3.按编号修改宠物 *");
System.out.println("* 4.按编号删除宠物 *");
System.out.println("* *");
System.out.println("* *");
System.out.println("**********************************");
switch (scanner.next()) {
case "1":
List<Pet> petList = PetDao.queryPet(null);
for (Pet pet : petList) {
System.out.println(pet);
}
break;
case "2":
Pet pet = collectPetMessage(scanner);
if (PetDao.addPet(pet)) {
System.out.println(String.format("新增宠物:%s成功", pet.toString()));
} else {
System.out.println(String.format("新增宠物:%s失败", pet.toString()));
}
break;
case "3":
System.out.println("请输入要修改的宠物的编号");
String pId = scanner.next();
//校验宠物编号是否存在
if (PetDao.queryPet(pId).size() == 0) {
System.out.println(String.format("您要修改的宠物编号:%s不存在", pId));
break;
}
Pet updatePet = collectPetMessage(scanner);
updatePet.setpId(pId);
if (PetDao.updatePet(updatePet)) {
System.out.println("修改宠物成功");
} else {
System.out.println("修改宠物失败");
}
break;
case "4":
System.out.println("请输入要修改的宠物的编号");
String id = scanner.next();
if (PetDao.deletePet(id)) {
System.out.println(String.format("删除宠物编号为:%s的宠物成功", id));
} else {
System.out.println("删除宠物失败!");
}
break;
}
}
}
//通过控制台收集宠物信息
private static Pet collectPetMessage(Scanner scanner) {
System.out.println("请输入宠物的名称");
String pName = scanner.next();
System.out.println("请输入宠物性别,男或者女");
String pGender = scanner.next();
System.out.println("请输入宠物的年龄");
int pAge = scanner.nextInt();
return new Pet(UUID.randomUUID().toString(), pName, pGender, pAge);
}
}
以下答案由GPT-3.5大模型与博主波罗歌共同编写:
根据你的代码,已经实现了显示所有宠物的功能。下面给出添加宠物,按编号修改宠物和按编号删除宠物的代码实现。
添加宠物:
在PetDao类中添加以下代码:
//2 添加宠物
public static void addPet(Pet pet) throws Exception {
//1
Connection con = DBHelper.getConnection();
//2
String sql = "insert into pet values(?,?,?,?)";
//3
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, pet.getPid());
ps.setString(2, pet.getPname());
ps.setString(3, pet.getPgender());
ps.setInt(4, pet.getPage());
//4
ps.executeUpdate();
}
在MainView类中case 2中添加以下代码:
case 2:
System.out.println("添加宠物");
System.out.println("请输入宠物编号:");
String pid = scanner.next();
System.out.println("请输入宠物名称:");
String pname = scanner.next();
System.out.println("请输入宠物性别:");
String pgender = scanner.next();
System.out.println("请输入宠物年龄:");
int page = scanner.nextInt();
Pet pet = new Pet(pid, pname, page, pgender);
PetDao.addPet(pet);
break;
按编号修改宠物:
在PetDao类中添加以下代码:
//3 按编号修改宠物
public static void updatePetByPid(Pet pet) throws Exception {
//1
Connection con = DBHelper.getConnection();
//2
String sql = "update pet set pname=?,pgender=?,page=? where pid=?";
//3
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, pet.getPname());
ps.setString(2, pet.getPgender());
ps.setInt(3, pet.getPage());
ps.setString(4, pet.getPid());
//4
ps.executeUpdate();
}
在MainView类中case 3中添加以下代码:
case 3:
System.out.println("修改宠物信息");
System.out.println("请输入宠物编号:");
String pid3 = scanner.next();
System.out.println("请输入宠物名称:");
String pname3 = scanner.next();
System.out.println("请输入宠物性别:");
String pgender3 = scanner.next();
System.out.println("请输入宠物年龄:");
int page3 = scanner.nextInt();
Pet pet3 = new Pet(pid3, pname3, page3, pgender3);
PetDao.updatePetByPid(pet3);
break;
按编号删除宠物:
在PetDao类中添加以下代码:
//4 按编号删除宠物
public static void deletePetByPid(String pid) throws Exception {
//1
Connection con = DBHelper.getConnection();
//2
String sql = "delete from pet where pid=?";
//3
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, pid);
//4
ps.executeUpdate();
}
在MainView类中case 4中添加以下代码:
case 4:
System.out.println("删除宠物信息");
System.out.println("请输入宠物编号:");
String pid4 = scanner.next();
PetDao.deletePetByPid(pid4);
break;
如果我的回答解决了您的问题,请采纳!