用JAVA语言编写中学排课系统的界面设计,要求有学生教室登录界面和查询相应课表,且可以连接sqlserver数据库,给出代码
时间: 2024-03-24 17:38:12 浏览: 21
以下是一个简单的中学排课系统的Java界面设计示例,包括学生和教室登录界面以及查询相应课表功能。其中,使用了Java Swing组件库实现界面设计,使用了JDBC连接SQL Server数据库。
```java
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SchoolScheduleSystem extends JFrame implements ActionListener {
private JLabel userLabel, passLabel;
private JTextField userText;
private JPasswordField passText;
private JButton loginButton;
private Connection conn;
private PreparedStatement stmt;
private ResultSet rs;
public SchoolScheduleSystem() {
super("中学排课系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
userLabel = new JLabel("用户名:");
passLabel = new JLabel("密码:");
userText = new JTextField(20);
passText = new JPasswordField(20);
loginButton = new JButton("登录");
loginButton.addActionListener(this);
JPanel panel = new JPanel(new GridLayout(3, 2));
panel.add(userLabel);
panel.add(userText);
panel.add(passLabel);
panel.add(passText);
panel.add(loginButton);
setContentPane(panel);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String user = userText.getText();
String pass = new String(passText.getPassword());
if (user.equals("") || pass.equals("")) {
JOptionPane.showMessageDialog(this, "用户名和密码不能为空!");
} else {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databaseName=SchoolSchedule", user, pass);
stmt = conn.prepareStatement("SELECT CourseName, TeacherName, RoomNo, DayOfWeek, ClassPeriod FROM Schedule WHERE StudentID = ?");
stmt.setInt(1, 1); // TODO: replace with actual student ID
rs = stmt.executeQuery();
String[] columns = {"课程名称", "教师姓名", "教室编号", "星期几", "第几节课"};
Object[][] data = new Object[32][5];
int i = 0;
while (rs.next()) {
data[i][0] = rs.getString("CourseName");
data[i][1] = rs.getString("TeacherName");
data[i][2] = rs.getString("RoomNo");
data[i][3] = rs.getInt("DayOfWeek");
data[i][4] = rs.getInt("ClassPeriod");
i++;
}
JTable table = new JTable(data, columns);
JScrollPane scrollPane = new JScrollPane(table);
JOptionPane.showMessageDialog(this, scrollPane);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "登录失败!请检查用户名和密码。");
ex.printStackTrace();
} finally {
try { rs.close(); } catch (Exception ex) {}
try { stmt.close(); } catch (Exception ex) {}
try { conn.close(); } catch (Exception ex) {}
}
}
}
public static void main(String[] args) {
new SchoolScheduleSystem();
}
}
```
这个示例代码中,使用了JDBC连接SQL Server数据库,查询了学生ID为1的课表,并在对话框中显示了查询结果。你可以根据实际需求修改代码中的查询语句和数据库连接信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)