由java和sql server技术编写出来的一个简单的学生课程管理系统,实现增、删、查、改等功能。以窗体的方式实现,需要完整代码和步骤。
时间: 2024-02-18 07:04:31 浏览: 66
好的,以下是一个简单的学生课程管理系统的Java代码和SQL Server数据库操作步骤。
Java代码:
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class StudentCourseManagementSystem extends JFrame implements ActionListener {
JLabel nameLabel, courseLabel, gradeLabel;
JTextField nameField, courseField, gradeField;
JButton addButton, deleteButton, updateButton, searchButton;
JTable table;
DefaultTableModel model;
public StudentCourseManagementSystem() {
super("学生课程管理系统");
setLayout(null);
setSize(550, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
nameLabel = new JLabel("姓名:");
nameLabel.setBounds(20, 20, 50, 20);
add(nameLabel);
nameField = new JTextField();
nameField.setBounds(80, 20, 100, 20);
add(nameField);
courseLabel = new JLabel("课程:");
courseLabel.setBounds(200, 20, 50, 20);
add(courseLabel);
courseField = new JTextField();
courseField.setBounds(260, 20, 100, 20);
add(courseField);
gradeLabel = new JLabel("成绩:");
gradeLabel.setBounds(380, 20, 50, 20);
add(gradeLabel);
gradeField = new JTextField();
gradeField.setBounds(440, 20, 50, 20);
add(gradeField);
addButton = new JButton("添加");
addButton.setBounds(20, 60, 80, 20);
addButton.addActionListener(this);
add(addButton);
deleteButton = new JButton("删除");
deleteButton.setBounds(120, 60, 80, 20);
deleteButton.addActionListener(this);
add(deleteButton);
updateButton = new JButton("更新");
updateButton.setBounds(220, 60, 80, 20);
updateButton.addActionListener(this);
add(updateButton);
searchButton = new JButton("查找");
searchButton.setBounds(320, 60, 80, 20);
searchButton.addActionListener(this);
add(searchButton);
model = new DefaultTableModel();
table = new JTable(model);
JScrollPane pane = new JScrollPane(table);
pane.setBounds(20, 100, 500, 250);
add(pane);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
addRecord();
} else if (e.getSource() == deleteButton) {
deleteRecord();
} else if (e.getSource() == updateButton) {
updateRecord();
} else if (e.getSource() == searchButton) {
searchRecord();
}
}
public void addRecord() {
String name = nameField.getText();
String course = courseField.getText();
String grade = gradeField.getText();
if (name.equals("") || course.equals("")) {
JOptionPane.showMessageDialog(null, "姓名和课程不能为空!");
return;
}
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=StudentCourseDB;user=sa;password=123456");
Statement stmt = conn.createStatement();
String sql = "INSERT INTO StudentCourseTable VALUES ('" + name + "', '" + course + "', '" + grade + "')";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "添加成功!");
nameField.setText("");
courseField.setText("");
gradeField.setText("");
conn.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
public void deleteRecord() {
int selectedRow = table.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(null, "请选择要删除的记录!");
return;
}
String name = (String) model.getValueAt(selectedRow, 0);
String course = (String) model.getValueAt(selectedRow, 1);
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=StudentCourseDB;user=sa;password=123456");
Statement stmt = conn.createStatement();
String sql = "DELETE FROM StudentCourseTable WHERE Name='" + name + "' AND Course='" + course + "'";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "删除成功!");
conn.close();
model.removeRow(selectedRow);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
public void updateRecord() {
int selectedRow = table.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(null, "请选择要更新的记录!");
return;
}
String name = (String) model.getValueAt(selectedRow, 0);
String course = (String) model.getValueAt(selectedRow, 1);
String grade = (String) model.getValueAt(selectedRow, 2);
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=StudentCourseDB;user=sa;password=123456");
Statement stmt = conn.createStatement();
String sql = "UPDATE StudentCourseTable SET Grade='" + grade + "' WHERE Name='" + name + "' AND Course='" + course + "'";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "更新成功!");
conn.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
public void searchRecord() {
String name = nameField.getText();
String course = courseField.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=StudentCourseDB;user=sa;password=123456");
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM StudentCourseTable WHERE Name='" + name + "' OR Course='" + course + "'";
ResultSet rs = stmt.executeQuery(sql);
model.setRowCount(0);
while (rs.next()) {
String[] rowData = {rs.getString("Name"), rs.getString("Course"), rs.getString("Grade")};
model.addRow(rowData);
}
conn.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
public static void main(String[] args) {
new StudentCourseManagementSystem();
}
}
```
SQL Server数据库操作步骤:
1. 创建一个新的数据库,命名为StudentCourseDB。
2. 在该数据库中创建一个名为StudentCourseTable的表格,包含三个列:Name、Course和Grade。
3. 将以下SQL语句复制并执行,插入一些测试数据:
```sql
INSERT INTO StudentCourseTable VALUES ('张三', '数学', '90');
INSERT INTO StudentCourseTable VALUES ('李四', '英语', '80');
INSERT INTO StudentCourseTable VALUES ('王五', '历史', '70');
```
数据库表格应该长这样:
| Name | Course | Grade |
| ---- | ------ | ----- |
| 张三 | 数学 | 90 |
| 李四 | 英语 | 80 |
| 王五 | 历史 | 70 |
4. 使用SQL Server Management Studio连接数据库,确保数据库连接信息(主机名、端口号、用户名、密码)与Java代码中的一致。
5. 运行Java代码,即可使用学生课程管理系统。
阅读全文