用java的gui和jdbc实现学生成绩管理,登陆页面实现注册和登陆,操作页面实现学生成绩的增删查改,并给出具体代码
时间: 2024-03-10 14:49:00 浏览: 21
好的,下面是一个简单的示例,实现了学生成绩管理系统的登陆页面、操作页面、数据库的设计和实现。
1. 登陆页面的代码如下:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class LoginFrame extends JFrame implements ActionListener {
private JLabel labelUser;
private JLabel labelPwd;
private JTextField fieldUser;
private JPasswordField fieldPwd;
private JButton buttonLogin;
private JButton buttonRegister;
public LoginFrame() {
super("Login");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
labelUser = new JLabel("Username:");
labelPwd = new JLabel("Password:");
fieldUser = new JTextField(20);
fieldPwd = new JPasswordField(20);
buttonLogin = new JButton("Login");
buttonRegister = new JButton("Register");
JPanel panel = new JPanel(new GridLayout(3, 2));
panel.add(labelUser);
panel.add(fieldUser);
panel.add(labelPwd);
panel.add(fieldPwd);
panel.add(buttonLogin);
panel.add(buttonRegister);
add(panel);
buttonLogin.addActionListener(this);
buttonRegister.addActionListener(this);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonLogin) {
String user = fieldUser.getText();
String pwd = new String(fieldPwd.getPassword());
if (login(user, pwd)) {
dispose();
new MainFrame();
} else {
JOptionPane.showMessageDialog(null, "Invalid username or password.");
}
} else if (e.getSource() == buttonRegister) {
String user = fieldUser.getText();
String pwd = new String(fieldPwd.getPassword());
if (register(user, pwd)) {
JOptionPane.showMessageDialog(null, "Register successfully.");
} else {
JOptionPane.showMessageDialog(null, "Failed to register.");
}
}
}
private boolean login(String user, String pwd) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
PreparedStatement ps = conn.prepareStatement("SELECT * FROM user WHERE username = ? AND password = ?");
ps.setString(1, user);
ps.setString(2, pwd);
ResultSet rs = ps.executeQuery();
return rs.next();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
return false;
}
}
private boolean register(String user, String pwd) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
PreparedStatement ps = conn.prepareStatement("INSERT INTO user (username, password) VALUES (?, ?)");
ps.setString(1, user);
ps.setString(2, pwd);
int result = ps.executeUpdate();
return result > 0;
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) {
new LoginFrame();
}
}
```
2. 操作页面的代码如下:
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class MainFrame extends JFrame implements ActionListener {
private JButton buttonAdd;
private JButton buttonDelete;
private JButton buttonQuery;
private JButton buttonUpdate;
private JTable table;
private DefaultTableModel model;
public MainFrame() {
super("Student Score Management");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonAdd = new JButton("Add");
buttonDelete = new JButton("Delete");
buttonQuery = new JButton("Query");
buttonUpdate = new JButton("Update");
JPanel panelButtons = new JPanel(new FlowLayout());
panelButtons.add(buttonAdd);
panelButtons.add(buttonDelete);
panelButtons.add(buttonQuery);
panelButtons.add(buttonUpdate);
add(panelButtons, BorderLayout.NORTH);
model = new DefaultTableModel(new Object[][]{}, new String[]{"ID", "Name", "Score"});
table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER);
buttonAdd.addActionListener(this);
buttonDelete.addActionListener(this);
buttonQuery.addActionListener(this);
buttonUpdate.addActionListener(this);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonAdd) {
addStudent();
} else if (e.getSource() == buttonDelete) {
deleteStudent();
} else if (e.getSource() == buttonQuery) {
queryStudents();
} else if (e.getSource() == buttonUpdate) {
updateStudent();
}
}
private void addStudent() {
String name = JOptionPane.showInputDialog("Input name:");
String score = JOptionPane.showInputDialog("Input score:");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
PreparedStatement ps = conn.prepareStatement("INSERT INTO student (name, score) VALUES (?, ?)");
ps.setString(1, name);
ps.setString(2, score);
int result = ps.executeUpdate();
if (result > 0) {
JOptionPane.showMessageDialog(null, "Add successfully.");
} else {
JOptionPane.showMessageDialog(null, "Failed to add.");
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Failed to add.");
}
}
private void deleteStudent() {
int row = table.getSelectedRow();
if (row < 0) {
JOptionPane.showMessageDialog(null, "Please select a row to delete.");
return;
}
int id = (int) model.getValueAt(row, 0);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
PreparedStatement ps = conn.prepareStatement("DELETE FROM student WHERE id = ?");
ps.setInt(1, id);
int result = ps.executeUpdate();
if (result > 0) {
JOptionPane.showMessageDialog(null, "Delete successfully.");
} else {
JOptionPane.showMessageDialog(null, "Failed to delete.");
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Failed to delete.");
}
}
private void queryStudents() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
PreparedStatement ps = conn.prepareStatement("SELECT * FROM student");
ResultSet rs = ps.executeQuery();
model.setRowCount(0);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String score = rs.getString("score");
model.addRow(new Object[]{id, name, score});
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
private void updateStudent() {
int row = table.getSelectedRow();
if (row < 0) {
JOptionPane.showMessageDialog(null, "Please select a row to update.");
return;
}
int id = (int) model.getValueAt(row, 0);
String name = JOptionPane.showInputDialog("Input name:");
String score = JOptionPane.showInputDialog("Input score:");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
PreparedStatement ps = conn.prepareStatement("UPDATE student SET name = ?, score = ? WHERE id = ?");
ps.setString(1, name);
ps.setString(2, score);
ps.setInt(3, id);
int result = ps.executeUpdate();
if (result > 0) {
JOptionPane.showMessageDialog(null, "Update successfully.");
} else {
JOptionPane.showMessageDialog(null, "Failed to update.");
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Failed to update.");
}
}
public static void main(String[] args) {
new MainFrame();
}
}
```
3. 数据库的设计和实现
学生信息表的设计如下:
```
CREATE TABLE student (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20),
score INT
);
```
用户信息表的设计如下:
```
CREATE TABLE user (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(20),
password VARCHAR(20)
);
```
需要注意的是,上述代码中的数据库连接信息需要根据实际情况进行修改。