java学校设备管理系统教程
时间: 2023-12-06 22:35:21 浏览: 106
java设备管理系统
4星 · 用户满意度95%
以下是一个简单的Java学校设备管理系统的教程:
1. 首先,我们需要创建一个MYSQL数据库来存储设备信息。可以使用以下命令在MYSQL中创建一个名为“equipment”的数据库:
```sql
CREATE DATABASE equipment;
```
2. 接下来,我们需要创建一个名为“devices”的表来存储设备信息。可以使用以下命令在MYSQL中创建一个名为“devices”的表:
```sql
CREATE TABLE devices (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
type VARCHAR(255) NOT NULL,
quantity INT NOT NULL,
PRIMARY KEY (id)
);
```
3. 然后,我们需要使用Java编写一个程序来连接到MYSQL数据库并执行查询。以下是一个简单的Java程序,它可以连接到MYSQL数据库并查询设备表中的所有设备信息:
```java
import java.sql.*;
public class EquipmentManager {
public static void main(String[] args) {
try {
// Connect to the database
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/equipment", "username", "password");
// Execute the query
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM devices");
// Print the results
while (rs.next()) {
System.out.println(rs.getInt("id") + "\t" + rs.getString("name") + "\t" + rs.getString("type") + "\t" + rs.getInt("quantity"));
}
// Close the connection
conn.close();
} catch (SQLException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
```
4. 接下来,我们可以使用Swing或AWT图形化设计工具来创建一个用户界面,以便用户可以添加、编辑和删除设备信息。以下是一个简单的Swing用户界面,它允许用户添加设备信息:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class EquipmentManagerUI extends JFrame {
private JTextField nameField;
private JTextField typeField;
private JTextField quantityField;
public EquipmentManagerUI() {
// Set up the UI
setTitle("Equipment Manager");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create the input fields
nameField = new JTextField(20);
typeField = new JTextField(20);
quantityField = new JTextField(20);
// Create the labels
JLabel nameLabel = new JLabel("Name:");
JLabel typeLabel = new JLabel("Type:");
JLabel quantityLabel = new JLabel("Quantity:");
// Create the buttons
JButton addButton = new JButton("Add");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
// Connect to the database
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/equipment", "username", "password");
// Insert the new device
PreparedStatement stmt = conn.prepareStatement("INSERT INTO devices (name, type, quantity) VALUES (?, ?, ?)");
stmt.setString(1, nameField.getText());
stmt.setString(2, typeField.getText());
stmt.setInt(3, Integer.parseInt(quantityField.getText()));
stmt.executeUpdate();
// Close the connection
conn.close();
// Clear the input fields
nameField.setText("");
typeField.setText("");
quantityField.setText("");
} catch (SQLException ex) {
System.out.println("Error: " + ex.getMessage());
}
}
});
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Clear the input fields
nameField.setText("");
typeField.setText("");
quantityField.setText("");
}
});
// Create the layout
JPanel inputPanel = new JPanel(new GridLayout(3, 2));
inputPanel.add(nameLabel);
inputPanel.add(nameField);
inputPanel.add(typeLabel);
inputPanel.add(typeField);
inputPanel.add(quantityLabel);
inputPanel.add(quantityField);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(addButton);
buttonPanel.add(clearButton);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(inputPanel, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
setContentPane(mainPanel);
}
public static void main(String[] args) {
EquipmentManagerUI ui = new EquipmentManagerUI();
ui.setVisible(true);
}
}
```
这个用户界面包括三个文本框和两个按钮。用户可以在文本框中输入设备名称、类型和数量,并单击“添加”按钮将设备添加到数据库中。用户还可以单击“清除”按钮清除所有文本框。
阅读全文