写一个基于Swing+MYSQL+JDBC的Java学生管理系统代码
时间: 2023-09-12 18:04:30 浏览: 122
### 回答1:
import java.sql.*;
public class StudentManager {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// 注册 JDBC 驱动
Class.forName("com.mysql.jdbc.Driver");
// 打开链接
System.out.println("连接数据库...");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db", "root", "123456");
// 执行查询
System.out.println(" 实例化Statement对象...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, name, age FROM student";
ResultSet rs = stmt.executeQuery(sql);
// 展开结果集数据库
while(rs.next()) {
// 通过字段检索
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
// 输出数据
System.out.print("ID: " + id);
System.out.print(", 姓名: " + name);
System.out.print(", 年龄: " + age);
System.out.print("\n");
}
// 完成后关闭
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
// 处理 JDBC 错误
se.printStackTrace();
} catch (Exception e) {
// 处理 Class.forName 错误
e.printStackTrace();
} finally {
// 关闭资源
try {
if(stmt!=null) stmt.close();
} catch(SQLException se2) {
}
try {
if(conn!=null) conn.close();
} catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
### 回答2:
Java学生管理系统是一个基于Swing和MySQL JDBC的应用程序,用于管理学生的信息和成绩。以下是一个简单的代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class StudentManagementSystem extends JFrame {
private JLabel nameLabel, ageLabel, gradeLabel;
private JTextField nameField, ageField, gradeField;
private JButton addButton, updateButton, deleteButton;
private JTable table;
private DefaultTableModel tableModel;
private Connection connection;
private Statement statement;
public StudentManagementSystem() {
super("学生管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
// 初始化界面组件
nameLabel = new JLabel("姓名:");
nameField = new JTextField(10);
ageLabel = new JLabel("年龄:");
ageField = new JTextField(10);
gradeLabel = new JLabel("分数:");
gradeField = new JTextField(10);
addButton = new JButton("添加");
updateButton = new JButton("更新");
deleteButton = new JButton("删除");
// 创建表格
table = new JTable();
tableModel = new DefaultTableModel();
tableModel.addColumn("姓名");
tableModel.addColumn("年龄");
tableModel.addColumn("分数");
table.setModel(tableModel);
// 添加组件到窗口
add(nameLabel);
add(nameField);
add(ageLabel);
add(ageField);
add(gradeLabel);
add(gradeField);
add(addButton);
add(updateButton);
add(deleteButton);
add(new JScrollPane(table));
// 连接数据库
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
statement = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
// 添加按钮点击事件
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
int grade = Integer.parseInt(gradeField.getText());
try {
statement.executeUpdate("INSERT INTO students (name, age, grade) VALUES ('" + name + "', " + age + ", " + grade + ")");
tableModel.addRow(new Object[]{name, age, grade});
} catch (SQLException ex) {
ex.printStackTrace();
}
}
});
// 设置窗口属性
pack();
setVisible(true);
}
public static void main(String[] args) {
new StudentManagementSystem();
}
}
```
上述代码实现了一个简单的学生管理系统,能够连接到MySQL数据库,并且可以添加学生的信息。你可以根据实际需要进行扩展和修改。请确保你的MySQL数据库已经安装和配置好,并将代码中的连接信息("jdbc:mysql://localhost:3306/test","root","password")替换成你实际的数据库连接信息。
### 回答3:
下面是一个基于Swing、MYSQL JDBC的Java学生管理系统的代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class StudentManagementSystem extends JFrame {
private JLabel nameLabel, ageLabel, gradeLabel, resultLabel;
private JTextField nameField, ageField, gradeField;
private JButton addButton, viewButton;
private static final String DB_URL = "jdbc:mysql://localhost:3306/studentdb";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "password";
public StudentManagementSystem() {
initUI();
initListeners();
}
private void initUI() {
nameLabel = new JLabel("Name:");
ageLabel = new JLabel("Age:");
gradeLabel = new JLabel("Grade:");
resultLabel = new JLabel("");
nameField = new JTextField(20);
ageField = new JTextField(20);
gradeField = new JTextField(20);
addButton = new JButton("Add Student");
viewButton = new JButton("View Students");
JPanel panel = new JPanel(new GridLayout(4, 2));
panel.add(nameLabel);
panel.add(nameField);
panel.add(ageLabel);
panel.add(ageField);
panel.add(gradeLabel);
panel.add(gradeField);
panel.add(addButton);
panel.add(viewButton);
panel.add(resultLabel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Student Management System");
setSize(400, 200);
setLocationRelativeTo(null);
add(panel);
}
private void initListeners() {
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
double grade = Double.parseDouble(gradeField.getText());
addStudent(name, age, grade);
}
});
viewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
viewStudents();
}
});
}
private void addStudent(String name, int age, double grade) {
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
String query = "INSERT INTO student (name, age, grade) VALUES (?, ?, ?)";
PreparedStatement statement = conn.prepareStatement(query);
statement.setString(1, name);
statement.setInt(2, age);
statement.setDouble(3, grade);
int rowsInserted = statement.executeUpdate();
if (rowsInserted > 0) {
resultLabel.setText("Student added successfully.");
} else {
resultLabel.setText("Failed to add student.");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private void viewStudents() {
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
Statement statement = conn.createStatement();
String query = "SELECT * FROM student";
ResultSet resultSet = statement.executeQuery(query);
StringBuilder sb = new StringBuilder();
while (resultSet.next()) {
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
double grade = resultSet.getDouble("grade");
sb.append("Name: ").append(name).append(", Age: ").append(age).append(", Grade: ").append(grade).append("\n");
}
resultLabel.setText(sb.toString());
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new StudentManagementSystem().setVisible(true);
}
});
}
}
```
这个学生管理系统界面上有三个文本字段(姓名、年龄、年级)、两个按钮(添加学生、查看学生)和一个结果标签。当点击"Add Student"按钮时,程序将会将文本字段中的学生信息添加到MYSQL数据库中的"student"表中。当点击"View Students"按钮时,程序将会从数据库中获取所有学生信息并将其显示在结果标签中。
请注意,这只是一个简单的示例代码,实际的学生管理系统可能会更复杂,并且需要更多的错误处理和验证。此外,为了使该示例能够运行,你需要将DB_URL、DB_USER和DB_PASSWORD更改为你的mysql数据库的相应信息。
阅读全文