用javaswing写一个带有数据库且功能完整的疫苗管理系统
时间: 2024-06-08 18:06:53 浏览: 77
基于java的疫苗接种管理系统.zip
好的,以下是一个简单的JavaSwing疫苗管理系统示例,它包括数据库连接和完整的功能。请注意,这只是一个示例,您需要根据自己的需求进行修改和扩展。
首先,我们需要创建一个名为“VaccineManagementSystem”的项目。在该项目中,我们需要创建以下类和界面:
1. Vaccine类:表示疫苗的实体类,包括属性如下
```
private int id;
private String name;
private String manufacturer;
private String type;
private double price;
private int stock;
```
2. VaccineDao类:用于处理疫苗数据库的连接和操作,包括以下方法:
```
public void addVaccine(Vaccine vaccine); //添加疫苗
public void updateVaccine(Vaccine vaccine); //更新疫苗
public void deleteVaccine(int id); //删除疫苗
public Vaccine getVaccineById(int id); //根据id获取疫苗信息
public List<Vaccine> getAllVaccines(); //获取所有疫苗信息
```
3. VaccineForm类:用于添加、更新和删除疫苗的界面
4. VaccineList类:用于显示所有疫苗的界面
现在,我们需要创建一个MySQL数据库并创建一个名为“vaccine”的表来存储疫苗信息。表的结构如下:
```
CREATE TABLE `vaccine` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`manufacturer` varchar(50) NOT NULL,
`type` varchar(50) NOT NULL,
`price` double NOT NULL,
`stock` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
接下来,我们需要在项目中添加MySQL连接器依赖项,以便能够连接到数据库。您可以在pom.xml文件中添加以下依赖项:
```
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
```
然后,我们可以开始编写代码。首先是Vaccine类:
```
public class Vaccine {
private int id;
private String name;
private String manufacturer;
private String type;
private double price;
private int stock;
public Vaccine() {}
public Vaccine(String name, String manufacturer, String type, double price, int stock) {
this.name = name;
this.manufacturer = manufacturer;
this.type = type;
this.price = price;
this.stock = stock;
}
public Vaccine(int id, String name, String manufacturer, String type, double price, int stock) {
this.id = id;
this.name = name;
this.manufacturer = manufacturer;
this.type = type;
this.price = price;
this.stock = stock;
}
// getters and setters
}
```
然后是VaccineDao类:
```
public class VaccineDao {
private Connection connection;
public VaccineDao() {
connection = DBConnection.getConnection();
}
public void addVaccine(Vaccine vaccine) {
try {
PreparedStatement preparedStatement = connection.prepareStatement("insert into vaccine(name, manufacturer, type, price, stock) values (?, ?, ?, ?, ?)");
preparedStatement.setString(1, vaccine.getName());
preparedStatement.setString(2, vaccine.getManufacturer());
preparedStatement.setString(3, vaccine.getType());
preparedStatement.setDouble(4, vaccine.getPrice());
preparedStatement.setInt(5, vaccine.getStock());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateVaccine(Vaccine vaccine) {
try {
PreparedStatement preparedStatement = connection.prepareStatement("update vaccine set name=?, manufacturer=?, type=?, price=?, stock=? where id=?");
preparedStatement.setString(1, vaccine.getName());
preparedStatement.setString(2, vaccine.getManufacturer());
preparedStatement.setString(3, vaccine.getType());
preparedStatement.setDouble(4, vaccine.getPrice());
preparedStatement.setInt(5, vaccine.getStock());
preparedStatement.setInt(6, vaccine.getId());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteVaccine(int id) {
try {
PreparedStatement preparedStatement = connection.prepareStatement("delete from vaccine where id=?");
preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Vaccine getVaccineById(int id) {
Vaccine vaccine = new Vaccine();
try {
PreparedStatement preparedStatement = connection.prepareStatement("select * from vaccine where id=?");
preparedStatement.setInt(1, id);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
vaccine.setId(resultSet.getInt("id"));
vaccine.setName(resultSet.getString("name"));
vaccine.setManufacturer(resultSet.getString("manufacturer"));
vaccine.setType(resultSet.getString("type"));
vaccine.setPrice(resultSet.getDouble("price"));
vaccine.setStock(resultSet.getInt("stock"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return vaccine;
}
public List<Vaccine> getAllVaccines() {
List<Vaccine> vaccines = new ArrayList<>();
try {
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from vaccine");
while (resultSet.next()) {
Vaccine vaccine = new Vaccine();
vaccine.setId(resultSet.getInt("id"));
vaccine.setName(resultSet.getString("name"));
vaccine.setManufacturer(resultSet.getString("manufacturer"));
vaccine.setType(resultSet.getString("type"));
vaccine.setPrice(resultSet.getDouble("price"));
vaccine.setStock(resultSet.getInt("stock"));
vaccines.add(vaccine);
}
} catch (SQLException e) {
e.printStackTrace();
}
return vaccines;
}
}
```
接下来是VaccineForm类:
```
public class VaccineForm extends JFrame {
private final JTextField nameField;
private final JTextField manufacturerField;
private final JTextField typeField;
private final JTextField priceField;
private final JTextField stockField;
private VaccineDao vaccineDao;
private Vaccine vaccine;
public VaccineForm(VaccineDao vaccineDao, Vaccine vaccine) {
this.vaccineDao = vaccineDao;
this.vaccine = vaccine;
setTitle("疫苗信息");
setSize(400, 300);
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(6, 2));
JLabel nameLabel = new JLabel("名称:");
panel.add(nameLabel);
nameField = new JTextField(vaccine.getName());
panel.add(nameField);
JLabel manufacturerLabel = new JLabel("生产厂家:");
panel.add(manufacturerLabel);
manufacturerField = new JTextField(vaccine.getManufacturer());
panel.add(manufacturerField);
JLabel typeLabel = new JLabel("类型:");
panel.add(typeLabel);
typeField = new JTextField(vaccine.getType());
panel.add(typeField);
JLabel priceLabel = new JLabel("价格:");
panel.add(priceLabel);
priceField = new JTextField(String.valueOf(vaccine.getPrice()));
panel.add(priceField);
JLabel stockLabel = new JLabel("库存:");
panel.add(stockLabel);
stockField = new JTextField(String.valueOf(vaccine.getStock()));
panel.add(stockField);
JButton saveButton = new JButton("保存");
saveButton.addActionListener(e -> {
vaccine.setName(nameField.getText());
vaccine.setManufacturer(manufacturerField.getText());
vaccine.setType(typeField.getText());
vaccine.setPrice(Double.parseDouble(priceField.getText()));
vaccine.setStock(Integer.parseInt(stockField.getText()));
if (vaccine.getId() > 0) {
vaccineDao.updateVaccine(vaccine);
} else {
vaccineDao.addVaccine(vaccine);
}
setVisible(false);
dispose();
new VaccineList(vaccineDao);
});
panel.add(saveButton);
JButton cancelButton = new JButton("取消");
cancelButton.addActionListener(e -> {
setVisible(false);
dispose();
});
panel.add(cancelButton);
setContentPane(panel);
setLocationRelativeTo(null);
setVisible(true);
}
}
```
最后是VaccineList类:
```
public class VaccineList extends JFrame {
private final JTable table;
private VaccineDao vaccineDao;
public VaccineList(VaccineDao vaccineDao) {
this.vaccineDao = vaccineDao;
setTitle("疫苗信息");
setSize(800, 600);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
JButton addButton = new JButton("添加疫苗");
addButton.addActionListener(e -> {
setVisible(false);
new VaccineForm(vaccineDao, new Vaccine());
});
panel.add(addButton, BorderLayout.NORTH);
table = new JTable();
table.setModel(new DefaultTableModel(new Object[]{"ID", "名称", "生产厂家", "类型", "价格", "库存"}, 0));
table.getColumnModel().getColumn(0).setPreferredWidth(50);
table.getColumnModel().getColumn(1).setPreferredWidth(150);
table.getColumnModel().getColumn(2).setPreferredWidth(150);
table.getColumnModel().getColumn(3).setPreferredWidth(100);
table.getColumnModel().getColumn(4).setPreferredWidth(100);
table.getColumnModel().getColumn(5).setPreferredWidth(100);
table.getTableHeader().setReorderingAllowed(false);
table.setDefaultEditor(Object.class, null);
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane, BorderLayout.CENTER);
JButton refreshButton = new JButton("刷新");
refreshButton.addActionListener(e -> {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
for (Vaccine vaccine : vaccineDao.getAllVaccines()) {
model.addRow(new Object[]{vaccine.getId(), vaccine.getName(), vaccine.getManufacturer(), vaccine.getType(), vaccine.getPrice(), vaccine.getStock()});
}
});
panel.add(refreshButton, BorderLayout.SOUTH);
setContentPane(panel);
setLocationRelativeTo(null);
setVisible(true);
}
}
```
现在,我们可以在主类中启动应用程序:
```
public class Main {
public static void main(String[] args) {
VaccineDao vaccineDao = new VaccineDao();
new VaccineList(vaccineDao);
}
}
```
这就是一个简单的JavaSwing疫苗管理系统示例,它包括数据库连接和完整的功能。您可以根据自己的需求进行修改和扩展。
阅读全文