2、 改造读者模块,在提取读者的同时,提取其未归还的图书信息 第一步:通过程序增加一些借阅纪录 第二步:改造读者javabean,使之包括借阅的图书信息 第三步:改造ReaderManager中的读者提取方法(public BeanReader loadReader(String readerid) throws DbException ),同时提取未归还图书; 第四步:ReaderManager的main函数中调用该方法进行测试,要求输出指定读者的基本信息及其未归还的图书名称。 【实验结果与分析】 A、 javabean类代码。
时间: 2023-11-22 22:56:49 浏览: 70
以下是改造后的Reader javabean类代码,其中包括了借阅的图书信息:
```java
public class BeanReader implements Serializable {
private String readerid;
private String name;
private String sex;
private String dept;
private String type;
private List<BeanBook> borrowBooks;
public String getReaderid() {
return readerid;
}
public void setReaderid(String readerid) {
this.readerid = readerid;
}
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 String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<BeanBook> getBorrowBooks() {
return borrowBooks;
}
public void setBorrowBooks(List<BeanBook> borrowBooks) {
this.borrowBooks = borrowBooks;
}
}
```
其中,`borrowBooks`字段表示读者借阅的图书信息,是一个`List<BeanBook>`类型的列表。
B、ReaderManager类中的`loadReader`方法代码:
```java
public BeanReader loadReader(String readerid) throws DbException {
Connection conn = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM reader WHERE readerid=?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, readerid);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
BeanReader reader = new BeanReader();
reader.setReaderid(readerid);
reader.setName(rs.getString(2));
reader.setSex(rs.getString(3));
reader.setDept(rs.getString(4));
reader.setType(rs.getString(5));
// 查询未归还的图书信息
List<BeanBook> borrowBooks = new ArrayList<>();
sql = "SELECT book.* FROM borrow, book WHERE borrow.bookid=book.bookid AND borrow.readerid=? AND borrow.returndate IS NULL";
pst = conn.prepareStatement(sql);
pst.setString(1, readerid);
rs = pst.executeQuery();
while (rs.next()) {
BeanBook book = new BeanBook();
book.setBookid(rs.getString(1));
book.setTitle(rs.getString(2));
book.setAuthor(rs.getString(3));
book.setPress(rs.getString(4));
book.setPrice(rs.getDouble(5));
book.setNum(rs.getInt(6));
borrowBooks.add(book);
}
reader.setBorrowBooks(borrowBooks);
return reader;
} else {
return null;
}
} catch (SQLException e) {
throw new DbException(e);
} finally {
DBUtil.closeConnection(conn);
}
}
```
在查询读者基本信息的同时,还查询了该读者未归还的图书信息,并将其封装到`BeanReader`对象中的`borrowBooks`字段中。
C、ReaderManager类中的main函数代码:
```java
public static void main(String[] args) {
ReaderManager manager = new ReaderManager();
try {
BeanReader reader = manager.loadReader("R0001");
System.out.println("读者信息:");
System.out.println("读者编号:" + reader.getReaderid());
System.out.println("姓名:" + reader.getName());
System.out.println("性别:" + reader.getSex());
System.out.println("部门:" + reader.getDept());
System.out.println("类型:" + reader.getType());
System.out.println("未归还的图书:");
for (BeanBook book : reader.getBorrowBooks()) {
System.out.println("图书编号:" + book.getBookid());
System.out.println("书名:" + book.getTitle());
System.out.println("作者:" + book.getAuthor());
System.out.println("出版社:" + book.getPress());
System.out.println("价格:" + book.getPrice());
System.out.println("数量:" + book.getNum());
System.out.println();
}
} catch (DbException e) {
e.printStackTrace();
}
}
```
该代码会输出读者基本信息和未归还的图书信息。
阅读全文