用javaswing写一个带有数据库且功能完整的疫苗管理系统
时间: 2024-06-08 19:06:54 浏览: 27
好的,以下是一个用Java Swing和MySQL数据库编写的疫苗管理系统的示例代码:
1. 创建数据库表
首先,我们需要在MySQL中创建一个名为“vaccine”的数据库,并创建一个名为“vaccine_info”的表,包含以下字段:
```
CREATE TABLE vaccine_info (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50) DEFAULT NULL,
manufacturer varchar(50) DEFAULT NULL,
batch_no varchar(50) DEFAULT NULL,
quantity int(11) DEFAULT NULL,
expiry_date date DEFAULT NULL,
PRIMARY KEY (id)
);
```
2. 创建Java项目
在Eclipse或NetBeans中创建一个Java项目,并添加MySQL连接器JAR文件到项目的classpath中。
3. 创建Swing用户界面
创建一个Swing用户界面,包括以下组件:
- 文本框和标签用于输入和显示疫苗名称、制造商、批号、数量和过期日期等信息;
- 按钮用于添加、修改、删除和搜索疫苗信息;
- 表格用于显示疫苗信息列表。
以下是示例代码:
```
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class VaccineManagementSystem extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField txtName;
private JTextField txtManufacturer;
private JTextField txtBatchNo;
private JTextField txtQuantity;
private JTextField txtExpiryDate;
private JTable table;
private Connection conn;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
VaccineManagementSystem frame = new VaccineManagementSystem();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public VaccineManagementSystem() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/vaccine", "root", "password");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
contentPane = new JPanel();
contentPane.setBorder(null);
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Name");
lblNewLabel.setBounds(20, 20, 100, 20);
contentPane.add(lblNewLabel);
txtName = new JTextField();
txtName.setBounds(130, 20, 150, 20);
contentPane.add(txtName);
txtName.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("Manufacturer");
lblNewLabel_1.setBounds(20, 50, 100, 20);
contentPane.add(lblNewLabel_1);
txtManufacturer = new JTextField();
txtManufacturer.setBounds(130, 50, 150, 20);
contentPane.add(txtManufacturer);
txtManufacturer.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("Batch No.");
lblNewLabel_2.setBounds(20, 80, 100, 20);
contentPane.add(lblNewLabel_2);
txtBatchNo = new JTextField();
txtBatchNo.setBounds(130, 80, 150, 20);
contentPane.add(txtBatchNo);
txtBatchNo.setColumns(10);
JLabel lblNewLabel_3 = new JLabel("Quantity");
lblNewLabel_3.setBounds(20, 110, 100, 20);
contentPane.add(lblNewLabel_3);
txtQuantity = new JTextField();
txtQuantity.setBounds(130, 110, 150, 20);
contentPane.add(txtQuantity);
txtQuantity.setColumns(10);
JLabel lblNewLabel_4 = new JLabel("Expiry Date");
lblNewLabel_4.setBounds(20, 140, 100, 20);
contentPane.add(lblNewLabel_4);
txtExpiryDate = new JTextField();
txtExpiryDate.setBounds(130, 140, 150, 20);
contentPane.add(txtExpiryDate);
txtExpiryDate.setColumns(10);
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addVaccine();
}
});
btnAdd.setBounds(20, 180, 80, 20);
contentPane.add(btnAdd);
JButton btnUpdate = new JButton("Update");
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateVaccine();
}
});
btnUpdate.setBounds(110, 180, 80, 20);
contentPane.add(btnUpdate);
JButton btnDelete = new JButton("Delete");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteVaccine();
}
});
btnDelete.setBounds(200, 180, 80, 20);
contentPane.add(btnDelete);
JButton btnSearch = new JButton("Search");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchVaccine();
}
});
btnSearch.setBounds(290, 180, 80, 20);
contentPane.add(btnSearch);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(20, 220, 750, 330);
contentPane.add(scrollPane);
table = new JTable();
table.setModel(new DefaultTableModel(
new Object[][] {},
new String[] {"ID", "Name", "Manufacturer", "Batch No.", "Quantity", "Expiry Date"}
));
scrollPane.setViewportView(table);
loadData();
}
private void loadData() {
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM vaccine_info");
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
while (rs.next()) {
Object[] row = new Object[6];
row[0] = rs.getInt("id");
row[1] = rs.getString("name");
row[2] = rs.getString("manufacturer");
row[3] = rs.getString("batch_no");
row[4] = rs.getInt("quantity");
row[5] = rs.getDate("expiry_date");
model.addRow(row);
}
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void addVaccine() {
try {
String name = txtName.getText();
String manufacturer = txtManufacturer.getText();
String batchNo = txtBatchNo.getText();
int quantity = Integer.parseInt(txtQuantity.getText());
Date expiryDate = new SimpleDateFormat("yyyy-MM-dd").parse(txtExpiryDate.getText());
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO vaccine_info (name, manufacturer, batch_no, quantity, expiry_date) VALUES (?, ?, ?, ?, ?)");
pstmt.setString(1, name);
pstmt.setString(2, manufacturer);
pstmt.setString(3, batchNo);
pstmt.setInt(4, quantity);
pstmt.setDate(5, new java.sql.Date(expiryDate.getTime()));
pstmt.executeUpdate();
pstmt.close();
loadData();
} catch (Exception e) {
e.printStackTrace();
}
}
private void updateVaccine() {
try {
int selectedRow = table.getSelectedRow();
if (selectedRow < 0) {
return;
}
int id = (int) table.getModel().getValueAt(selectedRow, 0);
String name = txtName.getText();
String manufacturer = txtManufacturer.getText();
String batchNo = txtBatchNo.getText();
int quantity = Integer.parseInt(txtQuantity.getText());
Date expiryDate = new SimpleDateFormat("yyyy-MM-dd").parse(txtExpiryDate.getText());
PreparedStatement pstmt = conn.prepareStatement("UPDATE vaccine_info SET name=?, manufacturer=?, batch_no=?, quantity=?, expiry_date=? WHERE id=?");
pstmt.setString(1, name);
pstmt.setString(2, manufacturer);
pstmt.setString(3, batchNo);
pstmt.setInt(4, quantity);
pstmt.setDate(5, new java.sql.Date(expiryDate.getTime()));
pstmt.setInt(6, id);
pstmt.executeUpdate();
pstmt.close();
loadData();
} catch (Exception e) {
e.printStackTrace();
}
}
private void deleteVaccine() {
try {
int selectedRow = table.getSelectedRow();
if (selectedRow < 0) {
return;
}
int id = (int) table.getModel().getValueAt(selectedRow, 0);
PreparedStatement pstmt = conn.prepareStatement("DELETE FROM vaccine_info WHERE id=?");
pstmt.setInt(1, id);
pstmt.executeUpdate();
pstmt.close();
loadData();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void searchVaccine() {
try {
String name = txtName.getText();
String manufacturer = txtManufacturer.getText();
String batchNo = txtBatchNo.getText();
int quantity = 0;
if (!txtQuantity.getText().isEmpty()) {
quantity = Integer.parseInt(txtQuantity.getText());
}
Date expiryDate = null;
if (!txtExpiryDate.getText().isEmpty()) {
expiryDate = new SimpleDateFormat("yyyy-MM-dd").parse(txtExpiryDate.getText());
}
String sql = "SELECT * FROM vaccine_info WHERE 1=1";
if (!name.isEmpty()) {
sql += " AND name='" + name + "'";
}
if (!manufacturer.isEmpty()) {
sql += " AND manufacturer='" + manufacturer + "'";
}
if (!batchNo.isEmpty()) {
sql += " AND batch_no='" + batchNo + "'";
}
if (quantity > 0) {
sql += " AND quantity=" + quantity;
}
if (expiryDate != null) {
sql += " AND expiry_date='" + new java.sql.Date(expiryDate.getTime()) + "'";
}
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
while (rs.next()) {
Object[] row = new Object[6];
row[0] = rs.getInt("id");
row[1] = rs.getString("name");
row[2] = rs.getString("manufacturer");
row[3] = rs.getString("batch_no");
row[4] = rs.getInt("quantity");
row[5] = rs.getDate("expiry_date");
model.addRow(row);
}
rs.close();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
4. 运行程序
在Java IDE中运行程序,即可使用疫苗管理系统的功能,包括添加、修改、删除和搜索疫苗信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)