校园宿舍管理系统java+swing+jdbc代码
时间: 2023-06-29 19:19:09 浏览: 130
宿舍管理系统java
以下是一个简单的校园宿舍管理系统的Java代码,使用了Swing和JDBC库。请注意,这只是一个简单的示例代码,您需要根据自己的需求进行修改和完善。
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class DormitoryManagementSystem extends JFrame implements ActionListener {
JLabel title, nameLabel, genderLabel, roomNoLabel, dateLabel;
JTextField nameField, roomNoField, dateField;
JComboBox genderBox;
JButton addBtn, viewBtn, clearBtn;
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
public static void main(String[] args) {
new DormitoryManagementSystem();
}
public DormitoryManagementSystem() {
super("校园宿舍管理系统");
Container container = getContentPane();
container.setLayout(new GridLayout(6, 2));
title = new JLabel("宿舍管理系统", JLabel.CENTER);
nameLabel = new JLabel("姓名", JLabel.CENTER);
genderLabel = new JLabel("性别", JLabel.CENTER);
roomNoLabel = new JLabel("房间号", JLabel.CENTER);
dateLabel = new JLabel("入住日期(yyyy-mm-dd)", JLabel.CENTER);
nameField = new JTextField();
genderBox = new JComboBox(new String[] { "男", "女" });
roomNoField = new JTextField();
dateField = new JTextField();
addBtn = new JButton("添加");
viewBtn = new JButton("查看");
clearBtn = new JButton("清空");
addBtn.addActionListener(this);
viewBtn.addActionListener(this);
clearBtn.addActionListener(this);
container.add(title);
container.add(new JLabel());
container.add(nameLabel);
container.add(nameField);
container.add(genderLabel);
container.add(genderBox);
container.add(roomNoLabel);
container.add(roomNoField);
container.add(dateLabel);
container.add(dateField);
container.add(addBtn);
container.add(viewBtn);
container.add(clearBtn);
setSize(400, 250);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dormitory", "root", "password");
} catch (Exception e) {
System.out.println(e);
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addBtn) {
String name = nameField.getText();
String gender = genderBox.getSelectedItem().toString();
String roomNo = roomNoField.getText();
String date = dateField.getText();
try {
pst = conn.prepareStatement("insert into student(name, gender, roomNo, date) values(?,?,?,?)");
pst.setString(1, name);
pst.setString(2, gender);
pst.setString(3, roomNo);
pst.setString(4, date);
pst.executeUpdate();
JOptionPane.showMessageDialog(this, "添加成功");
} catch (SQLException ex) {
System.out.println(ex);
JOptionPane.showMessageDialog(this, "添加失败");
}
} else if (e.getSource() == viewBtn) {
try {
pst = conn.prepareStatement("select * from student");
rs = pst.executeQuery();
StringBuilder sb = new StringBuilder();
while (rs.next()) {
sb.append(rs.getString("name")).append("\t");
sb.append(rs.getString("gender")).append("\t");
sb.append(rs.getString("roomNo")).append("\t");
sb.append(rs.getString("date")).append("\n");
}
JOptionPane.showMessageDialog(this, sb.toString());
} catch (SQLException ex) {
System.out.println(ex);
JOptionPane.showMessageDialog(this, "查询失败");
}
} else if (e.getSource() == clearBtn) {
nameField.setText("");
genderBox.setSelectedIndex(0);
roomNoField.setText("");
dateField.setText("");
}
}
}
```
该代码使用了MySQL数据库,需要首先安装MySQL并创建一个名为dormitory的数据库。请将代码中的“password”替换为您的MySQL密码。
该代码实现了一个简单的宿舍管理系统,包括添加学生信息、查看学生信息和清空输入框等功能。您可以根据自己的需求进行修改和扩展。
阅读全文