通过JDBC相关的API实现学生成绩的添加、修改、查询、统计等功能,采用图形界面,数据库采用MySQL,写出完整java代码
时间: 2023-12-10 21:40:35 浏览: 64
学生信息管理系统3,学生信息管理系统查询,Java源码.zip
以下是一个简单的示例代码,实现学生成绩的添加、修改、查询、统计等功能,采用图形界面,数据库采用MySQL:
```java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class StudentGradeGUI extends JFrame {
private JPanel contentPane;
private JTextField textFieldName;
private JTextField textFieldScore;
private JTable table;
private DefaultTableModel model;
private Connection conn;
private PreparedStatement stmt;
private ResultSet rs;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StudentGradeGUI frame = new StudentGradeGUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public StudentGradeGUI() {
// connect to database
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/student";
conn = DriverManager.getConnection(url, "root", "password");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblName = new JLabel("Name");
lblName.setBounds(10, 11, 46, 14);
contentPane.add(lblName);
textFieldName = new JTextField();
textFieldName.setBounds(66, 8, 86, 20);
contentPane.add(textFieldName);
textFieldName.setColumns(10);
JLabel lblScore = new JLabel("Score");
lblScore.setBounds(10, 36, 46, 14);
contentPane.add(lblScore);
textFieldScore = new JTextField();
textFieldScore.setBounds(66, 33, 86, 20);
contentPane.add(textFieldScore);
textFieldScore.setColumns(10);
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addStudentGrade();
}
});
btnAdd.setBounds(10, 61, 89, 23);
contentPane.add(btnAdd);
JButton btnUpdate = new JButton("Update");
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateStudentGrade();
}
});
btnUpdate.setBounds(109, 61, 89, 23);
contentPane.add(btnUpdate);
JButton btnSearch = new JButton("Search");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchStudentGrade();
}
});
btnSearch.setBounds(208, 61, 89, 23);
contentPane.add(btnSearch);
JButton btnStat = new JButton("Stat");
btnStat.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
statStudentGrade();
}
});
btnStat.setBounds(307, 61, 89, 23);
contentPane.add(btnStat);
table = new JTable();
model = new DefaultTableModel(new Object[][] {}, new String[] { "Name", "Score" });
table.setModel(model);
table.setBounds(10, 95, 386, 156);
contentPane.add(table);
}
// add student grade to database
private void addStudentGrade() {
try {
String name = textFieldName.getText();
int score = Integer.parseInt(textFieldScore.getText());
String sql = "INSERT INTO grade (name, score) VALUES (?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1, name);
stmt.setInt(2, score);
stmt.executeUpdate();
model.addRow(new Object[] { name, score });
} catch (SQLException e) {
e.printStackTrace();
}
}
// update student grade in database
private void updateStudentGrade() {
try {
String name = textFieldName.getText();
int score = Integer.parseInt(textFieldScore.getText());
String sql = "UPDATE grade SET score = ? WHERE name = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, score);
stmt.setString(2, name);
int rowsAffected = stmt.executeUpdate();
if (rowsAffected == 0) {
System.out.println("No rows affected.");
} else {
System.out.println(rowsAffected + " rows affected.");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// search student grade from database
private void searchStudentGrade() {
try {
String name = textFieldName.getText();
String sql = "SELECT * FROM grade WHERE name LIKE ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, "%" + name + "%");
rs = stmt.executeQuery();
model.setRowCount(0);
while (rs.next()) {
String resultName = rs.getString("name");
int resultScore = rs.getInt("score");
model.addRow(new Object[] { resultName, resultScore });
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// stat student grade from database
private void statStudentGrade() {
try {
String sql = "SELECT AVG(score) as avg_score, COUNT(*) as count FROM grade";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
model.setRowCount(0);
while (rs.next()) {
double avgScore = rs.getDouble("avg_score");
int count = rs.getInt("count");
model.addRow(new Object[] { "Average Score", avgScore });
model.addRow(new Object[] { "Number of Students", count });
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
注意:这只是一个简单的示例代码,实际使用时需要根据实际情况进行修改和完善。例如,需要对用户输入进行验证和处理,需要处理数据库连接和关闭等异常情况。
阅读全文