写出该文档中的所有Java代码以及具体步骤
时间: 2024-12-19 07:28:20 浏览: 14
文档《实验5.docx》并未提供具体的Java代码实现细节,但描述了需要完成的任务和步骤。以下是根据文档内容整理出的具体任务和建议的实现步骤:
### 1. 测试数据库连接
**任务描述:**
- 创建一个名为 `PublisherManagement` 的数据库。
- 在数据库中创建一个名为 `publisher` 的表。
- 向表中插入3条出版社记录。
- 编写一个字符界面的 Java Application 程序,输出全部出版社信息。
**步骤:**
1. **创建数据库和表:**
```sql
CREATE DATABASE PublisherManagement;
USE PublisherManagement;
CREATE TABLE publisher (
id INT PRIMARY KEY,
name VARCHAR(255),
postal_code VARCHAR(10),
address VARCHAR(255),
phone_number VARCHAR(15)
);
```
2. **插入数据:**
```sql
INSERT INTO publisher (id, name, postal_code, address, phone_number) VALUES
(1, '人民出版社', '100000', '北京市朝阳区', '010-12345678'),
(2, '科学出版社', '200000', '上海市浦东新区', '021-87654321'),
(3, '教育出版社', '300000', '广州市天河区', '020-98765432');
```
3. **编写 Java Application 程序:**
```java
import java.sql.*;
public class TestDatabaseConnection {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/PublisherManagement";
String user = "root";
String password = "your_password";
try (Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM publisher")) {
System.out.println("出版社列表:");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String postalCode = rs.getString("postal_code");
String address = rs.getString("address");
String phoneNumber = rs.getString("phone_number");
System.out.printf("ID: %d, 名称: %s, 邮编: %s, 地址: %s, 联系电话: %s%n",
id, name, postalCode, address, phoneNumber);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
### 2. 根据类图定义类并编写图形界面程序
**任务描述:**
- 定义 `Publisher` 类。
- 编写图形界面的 Java Application 程序,允许用户输入出版社信息并将其添加到 `publisher` 表中,成功提示“添加成功!”,否则提示“添加失败!”。
**步骤:**
1. **定义 `Publisher` 类:**
```java
public class Publisher {
private int id;
private String name;
private String postalCode;
private String address;
private String phoneNumber;
// 构造函数
public Publisher(int id, String name, String postalCode, String address, String phoneNumber) {
this.id = id;
this.name = name;
this.postalCode = postalCode;
this.address = address;
this.phoneNumber = phoneNumber;
}
// Getter 和 Setter 方法
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getPostalCode() { return postalCode; }
public void setPostalCode(String postalCode) { this.postalCode = postalCode; }
public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
public String getPhoneNumber() { return phoneNumber; }
public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; }
public boolean createPublisher() {
String url = "jdbc:mysql://localhost:3306/PublisherManagement";
String user = "root";
String password = "your_password";
try (Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement pstmt = conn.prepareStatement(
"INSERT INTO publisher (id, name, postal_code, address, phone_number) VALUES (?, ?, ?, ?, ?)")) {
pstmt.setInt(1, id);
pstmt.setString(2, name);
pstmt.setString(3, postalCode);
pstmt.setString(4, address);
pstmt.setString(5, phoneNumber);
int rowsAffected = pstmt.executeUpdate();
return rowsAffected > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
```
2. **编写图形界面程序:**
```java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CreatePublisherUI extends JFrame {
private JTextField idField;
private JTextField nameField;
private JTextField postalCodeField;
private JTextField addressField;
private JTextField phoneNumberField;
private JButton addButton;
public CreatePublisherUI() {
setTitle("添加出版社");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
add(panel);
JLabel idLabel = new JLabel("出版社编号:");
idField = new JTextField(10);
panel.add(idLabel);
panel.add(idField);
JLabel nameLabel = new JLabel("名称:");
nameField = new JTextField(10);
panel.add(nameLabel);
panel.add(nameField);
JLabel postalCodeLabel = new JLabel("邮编:");
postalCodeField = new JTextField(10);
panel.add(postalCodeLabel);
panel.add(postalCodeField);
JLabel addressLabel = new JLabel("地址:");
addressField = new JTextField(10);
panel.add(addressLabel);
panel.add(addressField);
JLabel phoneNumberLabel = new JLabel("联系电话:");
phoneNumberField = new JTextField(10);
panel.add(phoneNumberLabel);
panel.add(phoneNumberField);
addButton = new JButton("添加");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(idField.getText());
String name = nameField.getText();
String postalCode = postalCodeField.getText();
String address = addressField.getText();
String phoneNumber = phoneNumberField.getText();
Publisher publisher = new Publisher(id, name, postalCode, address, phoneNumber);
if (publisher.createPublisher()) {
JOptionPane.showMessageDialog(CreatePublisherUI.this, "添加成功!");
} else {
JOptionPane.showMessageDialog(CreatePublisherUI.this, "添加失败!");
}
}
});
panel.add(addButton);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
CreatePublisherUI frame = new CreatePublisherUI();
frame.setVisible(true);
});
}
}
```
### 3. 修改出版社类,增加删除方法,并编写图形界面程序
**任务描述:**
- 修改 `Publisher` 类,增加删除方法。
- 编写图形界面的 Java Application 程序,允许用户输入出版社编号并将其从 `publisher` 表中删除,成功提示“删除成功!”,否则提示“删除失败!”。
**步骤:**
1. **修改 `Publisher` 类,增加删除方法:**
```java
public class Publisher {
// ...(之前的字段和方法)
public boolean deletePublisher() {
String url = "jdbc:mysql://localhost:3306/PublisherManagement";
String user = "root";
String password = "your_password";
try (Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement pstmt = conn.prepareStatement("DELETE FROM publisher WHERE id = ?")) {
pstmt.setInt(1, id);
int rowsAffected = pstmt.executeUpdate();
return rowsAffected > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
```
2. **编写图形界面程序:**
```java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DeletePublisherUI extends JFrame {
private JTextField idField;
private JButton deleteButton;
public DeletePublisherUI() {
setTitle("删除出版社");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
add(panel);
JLabel idLabel = new JLabel("出版社编号:");
idField = new JTextField(10);
panel.add(idLabel);
panel.add(idField);
deleteButton = new JButton("删除");
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(idField.getText());
Publisher publisher = new Publisher(id, "", "", "", "");
if (publisher.deletePublisher()) {
JOptionPane.showMessageDialog(DeletePublisherUI.this, "删除成功!");
} else {
JOptionPane.showMessageDialog(DeletePublisherUI.this, "删除失败!");
}
}
});
panel.add(deleteButton);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
DeletePublisherUI frame = new DeletePublisherUI();
frame.setVisible(true);
});
}
}
```
### 4. 将第 3 题修改为 4 层架构
**任务描述:**
- 将上述功能改为 4 层架构,包括表示层、业务逻辑层、数据访问层和模型层。
**步骤:**
1. **模型层 (`Model Layer`):**
- 包含 `Publisher` 类,用于封装出版社的信息。
2. **数据访问层 (`Data Access Layer`, DAL):**
- 包含 `PublisherDAO` 接口及其实现类 `PublisherDAOImpl`,负责与数据库交互。
```java
public interface PublisherDAO {
boolean createPublisher(Publisher publisher);
boolean deletePublisher(int id);
}
public class PublisherDAOImpl implements PublisherDAO {
@Override
public boolean createPublisher(Publisher publisher) {
// 实现创建出版社的方法
}
@Override
public boolean deletePublisher(int id) {
// 实现删除出版社的方法
}
}
```
3. **业务逻辑层 (`Business Logic Layer`, BLL):**
- 包含 `PublisherService` 接口及其实现类 `PublisherServiceImpl`,负责处理业务逻辑。
```java
public interface PublisherService {
boolean createPublisher(Publisher publisher);
boolean deletePublisher(int id);
}
public class PublisherServiceImpl implements PublisherService {
private PublisherDAO dao = new PublisherDAOImpl();
@Override
public boolean createPublisher(Publisher publisher) {
return dao.createPublisher(publisher);
}
@Override
public boolean deletePublisher(int id) {
return dao.deletePublisher(id);
}
}
```
4. **表示层 (`Presentation Layer`):**
- 包含图形界面程序,调用业务逻辑层的方法。
```java
public class CreatePublisherUI extends JFrame {
// ...(之前的代码)
private PublisherService service = new PublisherServiceImpl();
public CreatePublisherUI() {
// ...(之前的代码)
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(idField.getText());
String name = nameField.getText();
String postalCode = postalCodeField.getText();
String address = addressField.getText();
String phoneNumber = phoneNumberField.getText();
Publisher publisher = new Publisher(id, name, postalCode, address, phoneNumber);
if (service.createPublisher(publisher)) {
JOptionPane.showMessageDialog(CreatePublisherUI.this, "添加成功!");
} else {
JOptionPane.showMessageDialog(CreatePublisherUI.this, "添加失败!");
}
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
CreatePublisherUI frame = new CreatePublisherUI();
frame.setVisible(true);
});
}
}
public class DeletePublisherUI extends JFrame {
// ...(之前的代码)
private PublisherService service = new PublisherServiceImpl();
public DeletePublisherUI() {
// ...(之前的代码)
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(idField.getText());
if (service.deletePublisher(id)) {
JOptionPane.showMessageDialog(DeletePublisherUI.this, "删除成功!");
} else {
JOptionPane.showMessageDialog(DeletePublisherUI.this, "删除失败!");
}
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
DeletePublisherUI frame = new DeletePublisherUI();
frame.setVisible(true);
});
}
}
```
以上是根据文档内容整理出的具体任务和建议的实现步骤。希望这些信息对你有所帮助。如果有任何疑问或需要进一步的帮助,请随时告诉我。
阅读全文