如何手动开发JAVA?背景
我们需要实现一个简单的学生成绩管理系统,可以记录学生的基本信息和成绩,并提供查询、添加、删除和修改学生信息的功能。为了实现这个系统,我们需要JAVA开发。
解决方案设计数据库结构首先,为了存储学生的信息和成绩,我们需要设计数据库的结构。我们可以创建两个表:students
和scores
。students
表格包含学生的基本信息,如学号、姓名、性别等;scores
表格包含学生的成绩信息,如学号、科目、成绩等。两个表格可以通过学号关联。
CREATE TABLE students ( id INT AUTO_INCREMENT PRIMARY KEY, student_id VARCHAR(20) NOT NULL, name VARCHAR(50) NOT NULL, gender VARCHAR(10) NOT NULL);CREATE TABLE scores ( id INT AUTO_INCREMENT PRIMARY KEY, student_id VARCHAR(20) NOT NULL, subject VARCHAR(50) NOT NULL, score INT NOT NULL, FOREIGN KEY (student_id) REFERENCES students(student_id));
编制学生信息管理接下来,我们可以编写学生信息管理类,提供学生信息的增加、删除、更改和检查功能。
public class StudentManager { private Connection connection; public StudentManager(Connection connection) { this.connection = connection; } public void addStudent(Student student) { try { PreparedStatement statement = connection.prepareStatement( "INSERT INTO students (student_id, name, gender) VALUES (?, ?, ?, ?)" ); statement.setString(1, student.getStudentId()); statement.setString(2, student.getName()); statement.setString(3, student.getGender()); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public void deleteStudent(String studentId) { try { PreparedStatement statement = connection.prepareStatement( "DELETE FROM students WHERE student_id = ?" ); statement.setString(1, studentId); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public void updateStudent(Student student) { try { PreparedStatement statement = connection.prepareStatement( "UPDATE students SET name = ?, gender = ?, gender = ? WHERE student_id = ?" ); statement.setString(1, student.getName()); statement.setString(2, student.getGender()); statement.setString(3, student.getStudentId()); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public Student getStudent(String studentId) { try { PreparedStatement statement = connection.prepareStatement( "SELECT * FROM students WHERE student_id = ?" ); statement.setString(1, studentId); ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { String name = resultSet.getString("name"); String gender = resultSet.getString("gender"); return new Student(studentId, name, gender); } } catch (SQLException e) { e.printStackTrace(); } return null; }}
编制绩效管理除学生信息管理外,我们还需要一个成绩管理类别来提供学生成绩的增加、删除、更改和检查功能。
public class ScoreManager { private Connection connection; public ScoreManager(Connection connection) { this.connection = connection; } public void addScore(Score score) { try { PreparedStatement statement = connection.prepareStatement( "INSERT INTO scores (student_id, subject, score) VALUES (?, ?, ?, ?)" ); statement.setString(1, score.getStudentId()); statement.setString(2, score.getSubject()); statement.setInt(3, score.getScore()); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public void deleteScore(int scoreId) { try { PreparedStatement statement = connection.prepareStatement( "DELETE FROM scores WHERE id = ?" ); statement.setInt(1, scoreId); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public void updateScore(Score score) { try { PreparedStatement statement = connection.prepareStatement( "UPDATE scores SET subject = ?, score = ?, score = ? WHERE id = ?" ); statement.setString(1, score.getSubject()); statement.setInt(2, score.getScore()); statement.setInt(3, score.getId()); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public List<Score> getScores(String studentId) { List<Score> scores = new ArrayList<>(); try { PreparedStatement statement = connection.prepareStatement( "SELECT * FROM scores WHERE student_id = ?" ); statement.setString(1, studentId); ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { int id
![](/images/780-200-2.jpg)