用java写一个学生选课管理系统用sqlserver数据库 swing组件
时间: 2024-05-12 11:15:49 浏览: 236
好的,下面是一个简单的学生选课管理系统的Java代码示例,使用SQL Server数据库和Swing组件:
```
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class StudentCourseManagementSystem extends JFrame implements ActionListener {
private JLabel nameLabel, idLabel, courseLabel;
private JTextField nameField, idField, courseField;
private JButton addButton, deleteButton, updateButton, searchButton, clearButton;
private JTable table;
private JScrollPane scrollPane;
private Connection con;
private Statement stmt;
private ResultSet rs;
public StudentCourseManagementSystem() {
super("学生选课管理系统");
// 创建界面组件
nameLabel = new JLabel("姓名:");
idLabel = new JLabel("学号:");
courseLabel = new JLabel("课程:");
nameField = new JTextField(10);
idField = new JTextField(10);
courseField = new JTextField(10);
addButton = new JButton("添加");
deleteButton = new JButton("删除");
updateButton = new JButton("修改");
searchButton = new JButton("查询");
clearButton = new JButton("清空");
table = new JTable();
scrollPane = new JScrollPane(table);
// 设置界面布局
JPanel panel = new JPanel(new GridLayout(2, 4));
panel.add(nameLabel);
panel.add(nameField);
panel.add(idLabel);
panel.add(idField);
panel.add(courseLabel);
panel.add(courseField);
panel.add(addButton);
panel.add(deleteButton);
panel.add(updateButton);
panel.add(searchButton);
panel.add(clearButton);
add(panel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
// 注册事件监听器
addButton.addActionListener(this);
deleteButton.addActionListener(this);
updateButton.addActionListener(this);
searchButton.addActionListener(this);
clearButton.addActionListener(this);
// 初始化数据库连接
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName=StudentCourseDB";
String username = "sa";
String password = "123456";
con = DriverManager.getConnection(url, username, password);
stmt = con.createStatement();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
// 显示界面
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String name = nameField.getText();
String id = idField.getText();
String course = courseField.getText();
try {
String sql = "INSERT INTO StudentCourse VALUES('" + name + "', '" + id + "', '" + course + "')";
int rows = stmt.executeUpdate(sql);
if (rows > 0) {
JOptionPane.showMessageDialog(this, "添加成功!");
showData();
} else {
JOptionPane.showMessageDialog(this, "添加失败!");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == deleteButton) {
int row = table.getSelectedRow();
if (row >= 0) {
String id = table.getValueAt(row, 1).toString();
try {
String sql = "DELETE FROM StudentCourse WHERE id='" + id + "'";
int rows = stmt.executeUpdate(sql);
if (rows > 0) {
JOptionPane.showMessageDialog(this, "删除成功!");
showData();
} else {
JOptionPane.showMessageDialog(this, "删除失败!");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(this, "请选择要删除的记录!");
}
} else if (e.getSource() == updateButton) {
int row = table.getSelectedRow();
if (row >= 0) {
String name = nameField.getText();
String id = idField.getText();
String course = courseField.getText();
try {
String sql = "UPDATE StudentCourse SET name='" + name + "', course='" + course + "' WHERE id='" + id + "'";
int rows = stmt.executeUpdate(sql);
if (rows > 0) {
JOptionPane.showMessageDialog(this, "修改成功!");
showData();
} else {
JOptionPane.showMessageDialog(this, "修改失败!");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(this, "请选择要修改的记录!");
}
} else if (e.getSource() == searchButton) {
String id = idField.getText();
try {
String sql = "SELECT * FROM StudentCourse WHERE id='" + id + "'";
rs = stmt.executeQuery(sql);
if (rs.next()) {
nameField.setText(rs.getString("name"));
courseField.setText(rs.getString("course"));
} else {
JOptionPane.showMessageDialog(this, "没有找到该学生选课记录!");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == clearButton) {
nameField.setText("");
idField.setText("");
courseField.setText("");
}
}
private void showData() {
try {
String sql = "SELECT * FROM StudentCourse";
rs = stmt.executeQuery(sql);
table.setModel(new ResultSetTableModel(rs));
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
new StudentCourseManagementSystem();
}
}
class ResultSetTableModel extends AbstractTableModel {
private ResultSet rs;
public ResultSetTableModel(ResultSet rs) {
this.rs = rs;
}
@Override
public int getRowCount() {
try {
rs.last();
return rs.getRow();
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
@Override
public int getColumnCount() {
try {
return rs.getMetaData().getColumnCount();
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
try {
rs.absolute(rowIndex + 1);
return rs.getObject(columnIndex + 1);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
@Override
public String getColumnName(int column) {
try {
return rs.getMetaData().getColumnName(column + 1);
} catch (SQLException e) {
e.printStackTrace();
return "";
}
}
}
```
该示例使用JDBC连接SQL Server数据库,实现了添加、删除、修改、查询和清空操作,并使用JTable显示数据。注意需要在项目中导入SQL Server JDBC驱动程序。
阅读全文