21xrx.com
2024-09-19 10:01:16 Thursday
登录
文章检索 我的文章 写文章
Java编写学生管理系统
2023-06-12 06:28:26 深夜i     --     --
Java

Java编写学生管理系统,轻松实现学生信息的录入、修改和查询

在如今的互联网时代,计算机软件系统的开发已经变得非常普遍。而学生管理系统作为一种非常常用的软件系统,其开发也变得越来越受欢迎。学生管理系统主要是针对学校或教育机构等场合,用于帮助该场合更好地管理学生信息,提供学生信息的录入、修改和查询等功能。

Java作为一种优秀的软件开发语言,其编写学生管理系统的应用也变得非常广泛。下面我们将通过一个Java编写的学生管理系统的案例,为大家介绍如何使用Java语言来轻松实现该系统的开发。

首先,我们来看一下Java编写学生管理系统的代码实现。这里我们以Java Swing为界面框架,以MySQL作为数据库来实现该系统。代码如下:


public class StudentManagementSystem extends JFrame {

  private JTextField tfName, tfId, tfAge, tfClass;

  private JTextArea taInfo;

  private JButton btnAdd, btnDelete, btnUpdate, btnSearch;

  public void createSqlTable() throws SQLException, ClassNotFoundException {

    Class.forName("com.mysql.jdbc.Driver");

    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student",

        "root", "admin");

    String sqlTable = "CREATE TABLE IF NOT EXISTS student_info(" +

        "id INT PRIMARY KEY AUTO_INCREMENT," +

        "name TEXT NOT NULL ," +

        "age INT NOT NULL ," +

        "class TEXT NOT NULL ); ";

    Statement statement = con.createStatement();

    statement.execute(sqlTable);

    statement.close();

    con.close();

  }

  public void insertIntoDB(String name, int age, String Class) throws ClassNotFoundException, SQLException {

    Class.forName("com.mysql.jdbc.Driver");

    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student",

        "root", "admin");

    String sql = "INSERT INTO student_info (name, age, class) VALUES (?, ?, ?);";

    PreparedStatement ps = con.prepareStatement(sql);

    ps.setString(1, name);

    ps.setInt(2, age);

    ps.setString(3, Class);

    ps.executeUpdate();

    ps.close();

    con.close();

  }

  public void deleteFromDB(String id) throws SQLException, ClassNotFoundException {

    Class.forName("com.mysql.jdbc.Driver");

    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student",

        "root", "admin");

    String sql = "DELETE FROM student_info WHERE id = ? ;";

    PreparedStatement ps = con.prepareStatement(sql);

    ps.setInt(1, Integer.parseInt(id));

    ps.execute();

    ps.close();

    con.close();

  }

  public void updateToDB(String id, String name, int age, String Class) throws ClassNotFoundException, SQLException {

    Class.forName("com.mysql.jdbc.Driver");

    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student",

        "root", "admin");

    String sql = "UPDATE student_info SET name = ?, age = ?, class = ? WHERE id = ? ;";

    PreparedStatement ps = con.prepareStatement(sql);

    ps.setString(1, name);

    ps.setInt(2, age);

    ps.setString(3, Class);

    ps.setInt(4, Integer.parseInt(id));

    ps.executeUpdate();

    ps.close();

    con.close();

  }

  public void searchFromDB(String id) throws ClassNotFoundException, SQLException {

    Class.forName("com.mysql.jdbc.Driver");

    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student",

        "root", "admin");

    String sql = "SELECT * FROM student_info WHERE id = ? ;";

    PreparedStatement ps = con.prepareStatement(sql);

    ps.setInt(1, Integer.parseInt(id));

    ResultSet rs = ps.executeQuery();

    String result = "";

    if (rs.next()) {

      result += "ID: " + rs.getInt("id") + "\n";

      result += "Name: " + rs.getString("name") + "\n";

      result += "Age: " + rs.getInt("age") + "\n";

      result += "Class: " + rs.getString("class") + "\n\n";

    } else {

      result = "No result found!";

    }

    taInfo.setText(result);

    rs.close();

    ps.close();

    con.close();

  }

  public void initUI() {

    this.setLayout(null);

    JLabel lbName = new JLabel("Name: ");

    lbName.setBounds(10, 20, 60, 25);

    this.add(lbName);

    tfName = new JTextField();

    tfName.setBounds(100, 20, 150, 25);

    this.add(tfName);

    JLabel lbId = new JLabel("ID: ");

    lbId.setBounds(10, 50, 60, 25);

    this.add(lbId);

    tfId = new JTextField();

    tfId.setBounds(100, 50, 150, 25);

    this.add(tfId);

    JLabel lbAge = new JLabel("Age: ");

    lbAge.setBounds(10, 80, 60, 25);

    this.add(lbAge);

    tfAge = new JTextField();

    tfAge.setBounds(100, 80, 150, 25);

    this.add(tfAge);

    JLabel lbClass = new JLabel("Class: ");

    lbClass.setBounds(10, 110, 60, 25);

    this.add(lbClass);

    tfClass = new JTextField();

    tfClass.setBounds(100, 110, 150, 25);

    this.add(tfClass);

    btnAdd = new JButton("Add");

    btnAdd.setBounds(20, 140, 80, 25);

    this.add(btnAdd);

    btnAdd.addActionListener(new ActionListener() {

      @Override

      public void actionPerformed(ActionEvent e) {

        try {

          insertIntoDB(tfName.getText(), Integer.parseInt(tfAge.getText()), tfClass.getText());

          JOptionPane.showMessageDialog(StudentManagementSystem.this, "Add success!");

        } catch (ClassNotFoundException | SQLException cnfe) {

          JOptionPane.showMessageDialog(StudentManagementSystem.this, cnfe.getMessage());

        }

      }

    });

    btnDelete = new JButton("Delete");

    btnDelete.setBounds(110, 140, 80, 25);

    this.add(btnDelete);

    btnDelete.addActionListener(new ActionListener() {

      @Override

      public void actionPerformed(ActionEvent e) {

        try {

          deleteFromDB(tfId.getText());

          JOptionPane.showMessageDialog(StudentManagementSystem.this, "Delete success!");

        } catch (ClassNotFoundException | SQLException cnfe) {

          JOptionPane.showMessageDialog(StudentManagementSystem.this, cnfe.getMessage());

        }

      }

    });

    btnUpdate = new JButton("Update");

    btnUpdate.setBounds(200, 140, 80, 25);

    this.add(btnUpdate);

    btnUpdate.addActionListener(new ActionListener() {

      @Override

      public void actionPerformed(ActionEvent e) {

        try {

          updateToDB(tfId.getText(), tfName.getText(), Integer.parseInt(tfAge.getText()), tfClass.getText());

          JOptionPane.showMessageDialog(StudentManagementSystem.this, "Update success!");

        } catch (ClassNotFoundException | SQLException cnfe) {

          JOptionPane.showMessageDialog(StudentManagementSystem.this, cnfe.getMessage());

        }

      }

    });

    btnSearch = new JButton("Search");

    btnSearch.setBounds(290, 140, 80, 25);

    this.add(btnSearch);

    btnSearch.addActionListener(new ActionListener() {

      @Override

      public void actionPerformed(ActionEvent e) {

        try {

          searchFromDB(tfId.getText());

        } catch (ClassNotFoundException | SQLException cnfe) {

          JOptionPane.showMessageDialog(StudentManagementSystem.this, cnfe.getMessage());

        }

      }

    });

    taInfo = new JTextArea();

    taInfo.setBounds(10, 170, 360, 200);

    this.add(taInfo);

    this.setTitle("Student Management System");

    this.setSize(400, 400);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.setLocationRelativeTo(null);

    this.setVisible(true);

  }

  public StudentManagementSystem() {

    try {

      createSqlTable();

    } catch (SQLException | ClassNotFoundException e) {

      System.out.println(e.getMessage());

    }

    initUI();

  }

  public static void main(String[] args) {

    StudentManagementSystem sms = new StudentManagementSystem();

  }

}

通过以上代码,我们不难发现,学生管理系统主要是由以下几个功能组成:

- createSqlTable:用于创建MySQL数据库的表格;

- insertIntoDB:用于将学生信息插入到MySQL数据库中;

- deleteFromDB:用于从MySQL数据库中删除学生信息;

- updateToDB:用于更新MySQL数据库中的学生信息;

- searchFromDB:用于从MySQL数据库中查询学生信息。

此外,整个学生管理系统还采用了Java Swing界面框架来完成整个系统的UI布局。

根据以上Java编写学生管理系统的案例,我们不难发现,使用Java语言来实现这样一个学生管理系统的开发非常简单和便利。而且,Java语言本身也有很高的可移植性和兼容性,能够在各种不同的计算机平台上非常好地运行。

、 MySQL 、学生管理系统。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复