本系统设计采用的是Java作为设计语言,MySQL作为数据库,简单来说整个系统涉及到Swing界面的开发、以及对于数据库的一些操作,查询、增加、删除等,通过上述的操作来完成相应的学生管理系统的基本功能。

因为时间和精力的原因(该项目完全由博主一人独立完成),外观巨丑无比,但功能都实现了。因为篇幅太长,总共有25个类,大概2600-2700行代码,所以博主分了两篇文章来写,本文写的是教师功能

这是博主大一期末时候的实训项目,做的相当相当的简陋不喜勿喷。此外,可以借鉴其中功能实现的过程以及思路,但不建议抄袭哦。(由于演示时间过长,所以本文就不像学生功能那样放置演示过程了)

 本文运用到的主要技术为:

GUI:java实现窗体、Swing。其实JAVA Swing的GUI目前企业中已经不用了,主要是一些学校和培训机构用来教导学生写一些游戏、小项目,练练手的

JDBC的全称是JAVA数据库连接(Java Database Connectivity)。它是一套用于执行SQL语句的Java API。应用程序可通过这套API连接到关系型数据库,并使用SQL语句完成对数据库中数据的新增、删除、修改和查询等操作。

实现的功能有:

用户登录功能:

1、可以选择学生或者教师身份登录

学生功能:

1、修改密码

2、查询成绩

3、查询学籍信息

4、查看课程信息

5、退出系统

教师功能:

1、添加用户

2、删除用户

3、修改密码

4、退出系统

5、添加成绩信息

6、删除成绩信息

7、修改成绩信息

8、查询成绩信息

9、添加学籍信息

10、删除学籍信息

11、修改学籍信息

12、查询学籍信息

13、添加课程信息

14、删除课程信息

15、修改课程信息

16、查询课程信息

目录

 教师主页面:

 系统管理:

添加用户功能: 

 删除用户功能:

 成绩管理:

 添加成绩功能:

 修改成绩功能:

 查询成绩功能:

 删除成绩功能:

学籍管理: 

添加学籍功能:

 修改学籍功能:

 查询学籍功能:

 删除学籍功能:

 课程管理:

 添加课程功能:

 修改课程功能:

 查询课程功能:

 删除课程功能

 涉及到的数据库表以及数据: 

 

 

 教师功能实现代码:

登录页面:

用GUI写出窗体和相关内容,通过JDBC连接数据库。然后通过获取的学生身份或者教师身份,遍历数据库中对应的表中是否有该账号,从而完成登录,并将其转到对应的身份所拥有的所有功能页面。

import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.sql.*;public class 登录功能 {public static void main(String[] args) {new dl();}}class dl {public static String xh;//定义一个参数记录登录的账户public void dl(){ }public static String getXh() {return xh;}public static void setXh(String xh) {dl.xh = xh;}JFrame jf;JLabel l1,l2,l3;JTextField f1;JPasswordField f2;JButton jb1,jb2;JCheckBox jc;String data;//存储学生或教师身份的选择public dl() {jf = new JFrame("系统登录窗口");jf.setSize(320,400);jf.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLayout(null);Font font = new Font("仿宋", Font.BOLD, 20);//设置统一的字体,让代码更加整洁美观JComboBox jcb=new JComboBox();jcb.addItem("身份");jcb.addItem("学生");jcb.addItem("教师");JTextField textField=new JTextField(20);jcb.setBounds(10,55,90,25);jcb.setFont(font);//为JComboBox下拉框组件注册动作监听器jcb.addActionListener(e -> {data=(String)jcb.getSelectedItem();});l3 = new JLabel("欢迎来到学生管理系统");l3.setBounds(55,10,500,40);l3.setFont(font);l1 = new JLabel("账号:");l1.setBounds(10,100,100,40);l1.setFont(font);f1 = new JTextField(null,20);f1.setBounds(90,107,180,30);f1.setFont(font);l2 = new JLabel("密码:");l2.setBounds(8,145,100,40);l2.setFont(font);f2=new JPasswordField(null,20);f2.setBounds(90,150,180,30);f2.setEchoChar('*');//设置密码的外显为*f2.setFont(font);jc=new JCheckBox("显示密码");jc.setBounds(230,250,80,40);jc.addItemListener(new ItemListener() {public void itemStateChanged(ItemEvent e) {if (e.getStateChange() == ItemEvent.SELECTED) {//被选中f2.setEchoChar((char) 0);} else {f2.setEchoChar('*');}}});ActionListener listener=new AbstractAction() {@Overridepublic void actionPerformed(ActionEvent e) {}};jc.addActionListener(listener);jb1 = new JButton("登录");jb1.setBounds(30,200,80,40);//匿名内部类jb1.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {Statement st=null;Connection con=null;ResultSet rs=null;try {//注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//这里的3306/后跟的是数据库名//获取数据库连接String url="jdbc:mysql://localhost:3306/students" />

 教师主页面:

import javax.swing.*;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;public class 教师页面 {public static void main(String[] args) {new teacher();}}class teacher{JFrame jf;JLabel l1;JButton jb1, jb2,jb3,jb4,jb5,jb6;public teacher() {jf = new JFrame("教师管理系统");jf.setSize(260, 400);//设置窗体大小jf.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLayout(null);Font font = new Font("仿宋", Font.BOLD, 20);//设置统一的字体,让代码更加整洁美观l1 = new JLabel("请选择你需要进行的操作");l1.setBounds(10,10,500,40);l1.setFont(font);jb1 = new JButton("系统管理");jb1.setBounds(45, 50, 150, 30);jb1.setFont(font);//匿名内部类jb1.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {//跳转到教师的系统管理页面new txt();jf.dispose();//关闭窗体,释放所有资源}});jb2=new JButton("成绩管理");jb2.setBounds(45,100,150,30);jb2.setFont(font);jb2.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//跳转到教师的成绩管理页面new tcj();jf.dispose();//关闭窗体,释放所有资源}});jb3=new JButton("学籍管理");jb3.setBounds(45,150,150,30);jb3.setFont(font);jb3.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//跳转到教师的学籍管理页面new txj();jf.dispose();//关闭窗体,释放所有资源}});jb4=new JButton("课程管理");jb4.setBounds(45,200,150,30);jb4.setFont(font);jb4.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//跳转到教师课程管理页面new tkc();jf.dispose();//关闭窗体,释放所有资源}});jb5=new JButton("返回");jb5.setBounds(17,250,80,30);jb5.setFont(font);jb5.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//返回登录界面new dl();jf.dispose();//关闭窗体,释放所有资源}});jb6 = new JButton("退出");jb6.setBounds(150, 250, 80, 30);//匿名内部类jb6.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {JOptionPane.showMessageDialog(null, "退出成功!");//系统退出System.exit(0);}});jb6.setFont(font);//将这些按钮和文本等加入到窗体中jf.add(l1);jf.add(jb1);jf.add(jb2);jf.add(jb3);jf.add(jb4);jf.add(jb5);jf.add(jb6);jf.setVisible(true);//让组件显示}}

 系统管理:

import javax.swing.*;import java.awt.*;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;public class 教师系统管理 {public static void main(String[] args) {new txt();}}class txt{JFrame jf;JLabel l1,l2,l3;JTextField f1;JPasswordField f2,f3;JButton jb1,jb2,jb3,jb4,jb5;JCheckBox jc;public txt() {jf = new JFrame("系统管理");jf.setSize(320,300);//设置窗体大小jf.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLayout(null);Font font = new Font("仿宋", Font.BOLD, 20);//设置统一的字体,让代码更加整洁美观l1 = new JLabel("学号:");l1.setBounds(10,10,100,40);l1.setFont(font);f1 = new JTextField(null,20);f1.setBounds(90,15,180,30);f1.setFont(font);l2 = new JLabel("新密码:");l2.setBounds(8, 50, 100, 40);l2.setFont(font);f2 = new JPasswordField(null, 20);f2.setBounds(90, 55, 180, 30);f2.setEchoChar('*');//设置密码的外显为*f2.setFont(font);l3 = new JLabel("确认密码:");l3.setBounds(8, 88, 130, 40);l3.setFont(font);f3 = new JPasswordField(null, 20);f3.setBounds(120, 95, 160, 30);f3.setEchoChar('*');//设置密码的外显为*jc=new JCheckBox("显示密码");//创建一个复选按钮jc.setBounds(230,210,80,40);jc.addItemListener(new ItemListener() {public void itemStateChanged(ItemEvent e) {if (e.getStateChange() == ItemEvent.SELECTED) {//被选中f2.setEchoChar((char) 0);//显示原本的数据f3.setEchoChar((char)0);//显示原本的数据} else {f2.setEchoChar('*');//设置密码的外显为*f3.setEchoChar('*');//设置密码的外显为*}}});f3.setFont(font);jb1 = new JButton("修改密码");jb1.setBounds(15, 130, 120, 30);//匿名内部类jb1.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {PreparedStatement ps = null;//PreparedStatement==>表示预编译的SQL语句的对象。// SQL语句已预编译并存储在PreparedStatement对象中。Connection con = null;//Connection==>与特定数据库的连接try {//注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//这里的3306/后跟的是数据库名//获取数据库连接String url = "jdbc:mysql://localhost:3306/students?serverTimezone=GMT%2B8";//通过DriverManager完成注册con = DriverManager.getConnection(url, "root", "123");//执行SQL语句String sql = "update dl set password=? where studentnumber=?";ps = con.prepareStatement(sql);if(f1.getText().length()!=0) {ps.setString(2, f1.getText());}else{JOptionPane.showMessageDialog(null, "学号不能为空!");//弹出窗口}if(f2.getText().length()!=0){//注:这里的所有getText()都不能写成!=nullps.setString(1, f2.getText());}else{JOptionPane.showMessageDialog(null, "新密码不能为空!");//弹出窗口}} catch (ClassNotFoundException ex) {ex.printStackTrace();} catch (SQLException ex) {ex.printStackTrace();}if (new String(f2.getPassword()).equals(new String(f3.getPassword()))) {//如果新密码与确认密码一致if (new String(f2.getPassword()).equals(new String(f3.getPassword()))&&f1.getText().length()!=0&&f2.getText().length()!=0) {//如果新密码与确认密码一致,学号和密码不为空,则修改成功JOptionPane.showMessageDialog(null, "修改成功!");//修改成功后跳转回教师页面new teacher();jf.dispose();//关闭窗体,释放所有资源try {int i = ps.executeUpdate();//将注册的账户存储到数据库中} catch (SQLException ex) {ex.printStackTrace();}}} else {JOptionPane.showMessageDialog(null, "修改失败!新密码与确认密码不一致!");f2.setText(null);//清空新密码f3.setText(null);//清空确认密码}}});jb1.setFont(font);jb2 = new JButton("退出");jb2.setBounds(180, 170, 80, 40);//匿名内部类jb2.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {JOptionPane.showMessageDialog(null, "退出成功!");//系统退出System.exit(0);}});jb3=new JButton("返回");jb3.setBounds(30,170,80,40);jb3.setFont(font);jb3.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//返回教师主页面new teacher();jf.dispose();//关闭窗体,释放所有资源}});jb2.setFont(font);jb4=new JButton("添加用户");jb4.setBounds(165, 130, 120, 30);jb4.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//转到注册功能页面new zc();jf.dispose();//关闭窗体,释放所有资源}});jb4.setFont(font);jb5 = new JButton("删除用户");jb5.setBounds(85, 220, 120, 30);jb5.setFont(font);jb5.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//转到删除用户页面new scyh();jf.dispose();//关闭窗体,释放所有资源}});//将这些按钮和文本等加入到窗体中jf.add(l1);jf.add(f1);jf.add(l2);jf.add(f2);jf.add(l3);jf.add(f3);jf.add(jc);jf.add(jb1);jf.add(jb2);jf.add(jb3);jf.add(jb4);jf.add(jb5);jf.setVisible(true);//让组件显示}}

添加用户功能: 

import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.sql.*;public class 注册功能 {public static void main(String[] args) { new zc();}}class zc{JFrame jf;JLabel l1,l2,l3;JTextField f1;JPasswordField f2,f3;JButton jb1,jb2,jb3;JCheckBox jc;public zc() {jf = new JFrame("系统注册窗口");jf.setSize(320,300);//设置窗体大小jf.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLayout(null);Font font = new Font("仿宋", Font.BOLD, 20);//设置统一的字体,让代码更加整洁美观l1 = new JLabel("学号:");l1.setBounds(10,10,100,40);l1.setFont(font);f1 = new JTextField(null,20);f1.setBounds(90,15,180,30);f1.setFont(font);l2 = new JLabel("密码:");l2.setBounds(8, 50, 100, 40);l2.setFont(font);f2 = new JPasswordField(null, 20);f2.setBounds(90, 55, 180, 30);f2.setEchoChar('*');//设置密码的外显为*f2.setFont(font);l3 = new JLabel("确认密码:");l3.setBounds(8, 88, 130, 40);l3.setFont(font);f3 = new JPasswordField(null, 20);f3.setBounds(120, 95, 160, 30);f3.setEchoChar('*');//设置密码的外显为*jc=new JCheckBox("显示密码");//创建一个复选按钮jc.setBounds(230,140,80,40);jc.addItemListener(new ItemListener() {public void itemStateChanged(ItemEvent e) {if (e.getStateChange() == ItemEvent.SELECTED) {//被选中f2.setEchoChar((char) 0);//显示原本的数据f3.setEchoChar((char)0);//显示原本的数据} else {f2.setEchoChar('*');//设置密码的外显为*f3.setEchoChar('*');//设置密码的外显为*}}});f3.setFont(font);jb1 = new JButton("注册");jb1.setBounds(100, 140, 80, 40);//匿名内部类jb1.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {PreparedStatement ps = null;//PreparedStatement==>表示预编译的SQL语句的对象。// SQL语句已预编译并存储在PreparedStatement对象中。Connection con = null;//Connection==>与特定数据库的连接try {//注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//这里的3306/后跟的是数据库名//获取数据库连接String url = "jdbc:mysql://localhost:3306/students?serverTimezone=GMT%2B8";//通过DriverManager完成注册con = DriverManager.getConnection(url, "root", "123");//执行SQL语句String sql = "insert into dl(studentnumber,password) values(?,?)";ps = con.prepareStatement(sql);if(f1.getText().length()!=0){//注:这里的所有getText()都不能写成!=nullps.setString(1, f1.getText());}else{JOptionPane.showMessageDialog(null, "学号不能为空!");//弹出窗口}if(f2.getText().length()!=0) {ps.setString(2, f2.getText());}else{JOptionPane.showMessageDialog(null, "密码不能为空!");}} catch (ClassNotFoundException ex) {ex.printStackTrace();} catch (SQLException ex) {ex.printStackTrace();}if (new String(f2.getPassword()).equals(new String(f3.getPassword()))) {//如果密码与确认密码一致if (new String(f2.getPassword()).equals(new String(f3.getPassword()))&&f1.getText().length()!=0&&f2.getText().length()!=0) {//如果密码与确认密码一致,学号和密码不为空,则注册成功JOptionPane.showMessageDialog(null, "注册成功!");f1.setText(null);f2.setText(null);f3.setText(null);try {int i = ps.executeUpdate();//将注册的账户存储到数据库中} catch (SQLException ex) {ex.printStackTrace();}}} else {JOptionPane.showMessageDialog(null, "注册失败!密码与确认密码不一致!");f2.setText(null);//清空密码f3.setText(null);//清空确认密码}}});jb1.setFont(font);jb2 = new JButton("退出");jb2.setBounds(180, 200, 80, 40);//匿名内部类jb2.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {JOptionPane.showMessageDialog(null, "退出成功!");//系统退出System.exit(0);}});jb2.setFont(font);jb3=new JButton("返回");jb3.setBounds(40,200,80,40);jb3.setFont(font);jb3.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//跳转到教师的系统管理页面new txt();jf.dispose();//关闭窗体,释放所有资源}});//将这些按钮和文本等加入到窗体中jf.add(l1);jf.add(f1);jf.add(l2);jf.add(f2);jf.add(l3);jf.add(f3);jf.add(jc);jf.add(jb1);jf.add(jb2);jf.add(jb3);jf.setVisible(true);//让组件显示}}

 删除用户功能:

import javax.swing.*;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;public class 删除用户功能 {public static void main(String[] args) {new scyh();}}class scyh{JFrame jf;JLabel l1;JTextField f1;JButton jb1,jb2,jb3;public scyh(){jf = new JFrame("删除用户");jf.setSize(320,280);//设置窗体大小jf.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLayout(null);Font font = new Font("仿宋", Font.BOLD, 20);//设置统一的字体,让代码更加整洁美观l1 = new JLabel("学号:");l1.setBounds(10,10,100,40);l1.setFont(font);f1 = new JTextField(null,20);f1.setBounds(90,15,180,30);f1.setFont(font);jb1 = new JButton("删除用户");jb1.setBounds(75, 75, 140, 40);jb1.setFont(font);jb1.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {PreparedStatement ps = null;//PreparedStatement==>表示预编译的SQL语句的对象。// SQL语句已预编译并存储在PreparedStatement对象中。Connection con = null;//Connection==>与特定数据库的连接try {//注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//这里的3306/后跟的是数据库名//获取数据库连接String url = "jdbc:mysql://localhost:3306/students?serverTimezone=GMT%2B8";//通过DriverManager完成注册con = DriverManager.getConnection(url, "root", "123");//执行SQL语句String sql = "delete from dl where studentnumber=?";ps = con.prepareStatement(sql);if(f1.getText().length()!=0){ps.setString(1,f1.getText());}else{JOptionPane.showMessageDialog(null, "学号不能为空!");}if(f1.getText().length()!=0){JOptionPane.showMessageDialog(null, "删除成功!");f1.setText(null);int i = ps.executeUpdate();}else{JOptionPane.showMessageDialog(null, "删除失败!");}} catch (ClassNotFoundException ex) {ex.printStackTrace();} catch (SQLException ex) {ex.printStackTrace();}}});jb2 = new JButton("返回");jb2.setBounds(35, 140, 80, 40);jb2.setFont(font);jb2.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//返回到教师系统管理页面new txt();jf.dispose();//关闭窗体,释放所有资源}});jb3 = new JButton("退出");jb3.setBounds(190, 140, 80, 40);jb3.setFont(font);jb3.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {JOptionPane.showMessageDialog(null, "退出成功!");//系统退出System.exit(0);}});//将这些按钮和文本等加入到窗体中jf.add(l1);jf.add(f1);jf.add(jb1);jf.add(jb2);jf.add(jb3);jf.setVisible(true);//让组件显示}}

 成绩管理:

import javax.swing.*;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;public class 教师成绩管理 {public static void main(String[] args) {new tcj();}}class tcj{JFrame jf;JButton jb1,jb2,jb3,jb4,jb5,jb6;public tcj(){jf = new JFrame("成绩管理");jf.setSize(320,350);//设置窗体大小jf.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLayout(null);Font font = new Font("仿宋", Font.BOLD, 20);//设置统一的字体,让代码更加整洁美观jb1 = new JButton("添加成绩信息");jb1.setBounds(55, 20, 200, 40);jb1.setFont(font);jb1.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//跳转到教师添加成绩页面new ttjcj();jf.dispose();//关闭窗体,释放所有资源}});jb2 = new JButton("修改成绩信息");jb2.setBounds(55, 75, 200, 40);jb2.setFont(font);jb2.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//跳转到教师修改成绩页面new txgcj();jf.dispose();//关闭窗体,释放所有资源}});jb3 = new JButton("查询成绩信息");jb3.setBounds(55, 125, 200, 40);jb3.setFont(font);jb3.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//跳转到教师查询成绩功能页面new tcx();jf.dispose();//关闭窗体,释放所有资源}});jb6=new JButton("删除成绩信息");jb6.setBounds(55,175,200,40);jb6.setFont(font);jb6.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//跳转到教师删除成绩功能页面new sccj();jf.dispose();//关闭窗体,释放所有资源}});jb4 = new JButton("返回");jb4.setBounds(30, 245, 80, 40);jb4.setFont(font);jb4.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//返回教师主页面new teacher();jf.dispose();//关闭窗体,释放所有资源}});jb5 = new JButton("退出");jb5.setBounds(200, 245, 80, 40);jb5.setFont(font);jb5.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {JOptionPane.showMessageDialog(null, "退出成功!");//系统退出System.exit(0);}});//将这些按钮和文本等加入到窗体中jf.add(jb1);jf.add(jb2);jf.add(jb3);jf.add(jb4);jf.add(jb5);jf.add(jb6);jf.setVisible(true);//让组件显示}}

 添加成绩功能:

import javax.swing.*;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;public class 添加成绩功能 {public static void main(String[] args) {new ttjcj();}}class ttjcj{JFrame jf;JLabel l1,l2,l3;JTextField f1,f2,f3;JButton jb1,jb2,jb3;public ttjcj(){jf = new JFrame("添加成绩");jf.setSize(320,300);//设置窗体大小jf.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLayout(null);Font font = new Font("仿宋", Font.BOLD, 20);//设置统一的字体,让代码更加整洁美观l1 = new JLabel("学号:");l1.setBounds(10,10,100,40);l1.setFont(font);f1 = new JTextField(null,20);f1.setBounds(90,15,180,30);f1.setFont(font);l2 = new JLabel("课程名:");l2.setBounds(8, 50, 100, 40);l2.setFont(font);f2=new JTextField(null,20);f2.setBounds(90,55,180,30);f2.setFont(font);l3 = new JLabel("成绩:");l3.setBounds(8, 90, 100, 40);l3.setFont(font);f3=new JTextField(null,20);f3.setBounds(90,95,180,30);f3.setFont(font);jb1 = new JButton("添加成绩信息");jb1.setBounds(65, 140, 180, 40);jb1.setFont(font);jb1.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {PreparedStatement ps = null;//PreparedStatement==>表示预编译的SQL语句的对象。// SQL语句已预编译并存储在PreparedStatement对象中。Connection con = null;//Connection==>与特定数据库的连接try {//注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//这里的3306/后跟的是数据库名//获取数据库连接String url = "jdbc:mysql://localhost:3306/students?serverTimezone=GMT%2B8";//通过DriverManager完成注册con = DriverManager.getConnection(url, "root", "123");//执行SQL语句String sql = "insert into chengji(studentnumber,course,cj) values(?,?,?)";ps = con.prepareStatement(sql);if (f1.getText().length() != 0) {ps.setString(1, f1.getText());} else {JOptionPane.showMessageDialog(null, "学号不能为空!");//弹出窗口}if (f2.getText().length() != 0) {ps.setString(2, f2.getText());} else {JOptionPane.showMessageDialog(null, "课程名不能为空!");//弹出窗口}if (f3.getText().length() != 0) {ps.setString(3, f3.getText());} else {JOptionPane.showMessageDialog(null, "成绩不能为空!");//弹出窗口}} catch (ClassNotFoundException ex) {ex.printStackTrace();} catch (SQLException ex) {ex.printStackTrace();}if (f1.getText().length() != 0 && f2.getText().length() != 0 && f3.getText().length() != 0) {//如果学号、课程名、成绩信息都填写了,便添加成功JOptionPane.showMessageDialog(null, "添加成功!");f1.setText(null);f2.setText(null);f3.setText(null);try {int i = ps.executeUpdate();//将数据存储到数据库中} catch (SQLException ex) {ex.printStackTrace();}}}});jb2 = new JButton("返回");jb2.setBounds(35, 200, 80, 40);jb2.setFont(font);jb2.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//返回到教师成绩管理页面new tcj();jf.dispose();//关闭窗体,释放所有资源}});jb3 = new JButton("退出");jb3.setBounds(190, 200, 80, 40);jb3.setFont(font);jb3.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {JOptionPane.showMessageDialog(null, "退出成功!");//系统退出System.exit(0);}});//将这些按钮和文本等加入到窗体中jf.add(l1);jf.add(f1);jf.add(l2);jf.add(f2);jf.add(l3);jf.add(f3);jf.add(jb1);jf.add(jb2);jf.add(jb3);jf.setVisible(true);//让组件显示}}

 修改成绩功能:

import javax.swing.*;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.sql.*;public class 修改成绩功能 {public static void main(String[] args) {new txgcj();}}class txgcj{JFrame jf;JLabel l1,l2,l3;JTextField f1,f2,f3;JButton jb1,jb2,jb3;public txgcj(){jf = new JFrame("修改成绩");jf.setSize(320,280);//设置窗体大小jf.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLayout(null);Font font = new Font("仿宋", Font.BOLD, 20);//设置统一的字体,让代码更加整洁美观l1 = new JLabel("学号:");l1.setBounds(10,10,100,40);l1.setFont(font);f1 = new JTextField(null,20);f1.setBounds(90,15,180,30);f1.setFont(font);l2 = new JLabel("课程名:");l2.setBounds(8, 50, 100, 40);l2.setFont(font);f2=new JTextField(null,20);f2.setBounds(90,55,180,30);f2.setFont(font);l3=new JLabel("成绩:");l3.setBounds(10,90,100,40);l3.setFont(font);f3=new JTextField(null,20);f3.setBounds(90,95,180,30);f3.setFont(font);jb1 = new JButton("修改成绩信息");jb1.setBounds(65, 140, 180, 40);jb1.setFont(font);jb1.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {PreparedStatement ps = null;//PreparedStatement==>表示预编译的SQL语句的对象。// SQL语句已预编译并存储在PreparedStatement对象中。Connection con = null;//Connection==>与特定数据库的连接Statement st=null;ResultSet rs=null;try {//注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//这里的3306/后跟的是数据库名//获取数据库连接String url = "jdbc:mysql://localhost:3306/students?serverTimezone=GMT%2B8";//通过DriverManager完成注册con = DriverManager.getConnection(url, "root", "123");//执行SQL语句String sql2="update chengji set cj=? where studentnumber=? and course=?";ps = con.prepareStatement(sql2);String sql = "select * from chengji";st=con.createStatement();rs=st.executeQuery(sql);int flag=0;while (rs.next()){//根据学号和课程名修改成绩if(rs.getString("studentnumber").equals(f1.getText())&&rs.getString("course").equals(f2.getText())){ps.setString(1,f3.getText());ps.setString(2,f1.getText());ps.setString(3,f2.getText());flag++;}}if(flag==0){JOptionPane.showMessageDialog(null, "没有该学生和课程!");}if(f1.getText().length()!=0&&f2.getText().length()!=0&&f3.getText().length()!=0 && flag!=0){//如果学号、课程名、成绩都填写了,则修改成功JOptionPane.showMessageDialog(null, "修改成功!");f1.setText(null);f2.setText(null);f3.setText(null);try {int i = ps.executeUpdate();//将数据存储到数据库中} catch (SQLException ex) {ex.printStackTrace();}}else{JOptionPane.showMessageDialog(null, "修改失败!");}} catch (ClassNotFoundException ex) {ex.printStackTrace();} catch (SQLException ex) {ex.printStackTrace();}}});jb2 = new JButton("返回");jb2.setBounds(35, 190, 80, 40);jb2.setFont(font);jb2.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//返回到教师成绩管理页面new tcj();jf.dispose();//关闭窗体,释放所有资源}});jb3 = new JButton("退出");jb3.setBounds(190, 190, 80, 40);jb3.setFont(font);jb3.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {JOptionPane.showMessageDialog(null, "退出成功!");//系统退出System.exit(0);}});//将这些按钮和文本等加入到窗体中jf.add(l1);jf.add(f1);jf.add(l2);jf.add(f2);jf.add(l3);jf.add(f3);jf.add(jb1);jf.add(jb2);jf.add(jb3);jf.setVisible(true);//让组件显示}}

 查询成绩功能:

import javax.swing.*;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.sql.*;public class 教师查询成绩功能 {public static void main(String[] args) {new tcx();}}class tcx{//文本域private JTextArea jta;//滚动条private JScrollPane jsp;//面板private JPanel jp;//按钮private JButton jb,jb2,jb3;//窗体JFrame jf;public tcx(){//初始化组件jta=new JTextArea();jf = new JFrame("查询成绩");//将文本域添加到滚动条中,实现滚动效果jsp=new JScrollPane(jta);//面板jp=new JPanel();jb=new JButton("查询");//将文本框和按钮添加到面板中jb2=new JButton("返回");jb2.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//跳转回教师成绩管理页面new tcj();jf.dispose();//关闭窗体,释放所有资源}});jb3=new JButton("退出");jb3.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {JOptionPane.showMessageDialog(null, "退出成功!");//系统退出System.exit(0);}});jp.add(jb);jp.add(jb2);jp.add(jb3);//将滚动条和面板都添加到窗体中jf.add(jsp, BorderLayout.CENTER);jf.add(jp,BorderLayout.SOUTH);jf.setSize(400,400);jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLocationRelativeTo(null);jf.setVisible(true);//设置组件显示//给查询按钮绑定一个监听点击事件jb.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {Statement st=null;Connection con=null;ResultSet rs=null;try {//注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//这里的3306/后跟的是数据库名//获取数据库连接String url = "jdbc:mysql://localhost:3306/students?serverTimezone=GMT%2B8";//通过DriverManager完成注册con = DriverManager.getConnection(url, "root", "123");//执行SQL语句String sql = "select * from chengji";st=con.createStatement();rs=st.executeQuery(sql);String text;while (rs.next()){text ="学号:"+rs.getString("studentnumber")+"\n"+"课程名:"+rs.getString("course")+"\n"+"成绩:"+rs.getString("cj")+"\n\n";jta.append(text);}} catch (ClassNotFoundException ex) {ex.printStackTrace();} catch (SQLException ex) {ex.printStackTrace();}}});}}

 删除成绩功能:

import javax.swing.*;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.sql.*;public class 删除成绩功能 {public static void main(String[] args) {new sccj();}}class sccj{JFrame jf;JLabel l1,l2;JTextField f1,f2;JButton jb1,jb2,jb3;public sccj(){jf = new JFrame("删除成绩");jf.setSize(320,280);//设置窗体大小jf.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLayout(null);Font font = new Font("仿宋", Font.BOLD, 20);//设置统一的字体,让代码更加整洁美观l1 = new JLabel("学号:");l1.setBounds(10,10,100,40);l1.setFont(font);f1 = new JTextField(null,20);f1.setBounds(90,15,180,30);f1.setFont(font);l2 = new JLabel("课程名:");l2.setBounds(8, 50, 100, 40);l2.setFont(font);f2=new JTextField(null,20);f2.setBounds(90,55,180,30);f2.setFont(font);jb1 = new JButton("删除成绩信息");jb1.setBounds(65, 110, 180, 40);jb1.setFont(font);jb1.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {PreparedStatement ps = null;//PreparedStatement==>表示预编译的SQL语句的对象。// SQL语句已预编译并存储在PreparedStatement对象中。Connection con = null;//Connection==>与特定数据库的连接try {//注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//这里的3306/后跟的是数据库名//获取数据库连接String url = "jdbc:mysql://localhost:3306/students?serverTimezone=GMT%2B8";//通过DriverManager完成注册con = DriverManager.getConnection(url, "root", "123");//执行SQL语句String sql = "delete from chengji where studentnumber=? and course=?";ps = con.prepareStatement(sql);if(f1.getText().length()!=0){ps.setString(1,f1.getText());}else{JOptionPane.showMessageDialog(null, "学号不能为空!");}if(f2.getText().length()!=0){ps.setString(2,f2.getText());}else{JOptionPane.showMessageDialog(null, "课程名不能为空!");}if(f1.getText().length()!=0&&f2.getText().length()!=0){JOptionPane.showMessageDialog(null, "删除成功!");f1.setText(null);f2.setText(null);int i = ps.executeUpdate();}else{JOptionPane.showMessageDialog(null, "删除失败!");}} catch (ClassNotFoundException ex) {ex.printStackTrace();} catch (SQLException ex) {ex.printStackTrace();}}});jb2 = new JButton("返回");jb2.setBounds(35, 165, 80, 40);jb2.setFont(font);jb2.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//返回到教师成绩管理页面new tcj();jf.dispose();//关闭窗体,释放所有资源}});jb3 = new JButton("退出");jb3.setBounds(190, 165, 80, 40);jb3.setFont(font);jb3.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {JOptionPane.showMessageDialog(null, "退出成功!");//系统退出System.exit(0);}});//将这些按钮和文本等加入到窗体中jf.add(l1);jf.add(f1);jf.add(l2);jf.add(f2);jf.add(jb1);jf.add(jb2);jf.add(jb3);jf.setVisible(true);//让组件显示}}

学籍管理: 

import javax.swing.*;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;public class 教师学籍管理 {public static void main(String[] args) {new txj();}}class txj{JFrame jf;JButton jb1,jb2,jb3,jb4,jb5,jb6;public txj(){jf = new JFrame("学籍管理");jf.setSize(320,350);//设置窗体大小jf.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLayout(null);Font font = new Font("仿宋", Font.BOLD, 20);//设置统一的字体,让代码更加整洁美观jb1 = new JButton("添加学籍信息");jb1.setBounds(55, 20, 200, 40);jb1.setFont(font);jb1.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//跳转到教师添加学籍页面new ttjxj();jf.dispose();//关闭窗体,释放所有资源}});jb2 = new JButton("修改学籍信息");jb2.setBounds(55, 75, 200, 40);jb2.setFont(font);jb2.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//跳转到教师修改学籍页面new txgxj();jf.dispose();//关闭窗体,释放所有资源}});jb3 = new JButton("查询学籍信息");jb3.setBounds(55, 125, 200, 40);jb3.setFont(font);jb3.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//跳转到教师查询功能页面new txjcx();jf.dispose();//关闭窗体,释放所有资源}});jb6=new JButton("删除学籍信息");jb6.setBounds(55,175,200,40);jb6.setFont(font);jb6.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//跳转到教师删除学籍功能页面new scxj();jf.dispose();//关闭窗体,释放所有资源}});jb4 = new JButton("返回");jb4.setBounds(30, 245, 80, 40);jb4.setFont(font);jb4.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//返回教师主页面new teacher();jf.dispose();//关闭窗体,释放所有资源}});jb5 = new JButton("退出");jb5.setBounds(200, 245, 80, 40);jb5.setFont(font);jb5.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {JOptionPane.showMessageDialog(null, "退出成功!");//系统退出System.exit(0);}});//将这些按钮和文本等加入到窗体中jf.add(jb1);jf.add(jb2);jf.add(jb3);jf.add(jb4);jf.add(jb5);jf.add(jb6);jf.setVisible(true);//让组件显示}}

添加学籍功能:

import javax.swing.*;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;public class 添加学籍功能 {public static void main(String[] args) {new ttjxj();}}class ttjxj{JFrame jf;JLabel l1,l2,l3,l4,l5;JTextField f1,f2,f3,f4,f5;JButton jb1,jb2,jb3;public ttjxj(){jf = new JFrame("添加学籍");jf.setSize(320,450);//设置窗体大小jf.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLayout(null);Font font = new Font("仿宋", Font.BOLD, 20);//设置统一的字体,让代码更加整洁美观l1 = new JLabel("班级:");l1.setBounds(10,10,100,40);l1.setFont(font);f1 = new JTextField(null,20);f1.setBounds(90,15,180,30);f1.setFont(font);l2 = new JLabel("学号:");l2.setBounds(8, 50, 100, 40);l2.setFont(font);f2=new JTextField(null,20);f2.setBounds(90,55,180,30);f2.setFont(font);l3 = new JLabel("姓名:");l3.setBounds(8, 90, 100, 40);l3.setFont(font);f3=new JTextField(null,20);f3.setBounds(90,95,180,30);f3.setFont(font);l4 = new JLabel("性别:");l4.setBounds(8, 130, 100, 40);l4.setFont(font);f4=new JTextField(null,20);f4.setBounds(90,135,180,30);f4.setFont(font);l5 = new JLabel("出生年月:");l5.setBounds(8, 170, 130, 40);l5.setFont(font);f5=new JTextField(null,20);f5.setBounds(90,210,180,30);f5.setFont(font);jb1 = new JButton("添加学籍信息");jb1.setBounds(65, 265, 180, 40);jb1.setFont(font);jb1.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {PreparedStatement ps = null;//PreparedStatement==>表示预编译的SQL语句的对象。// SQL语句已预编译并存储在PreparedStatement对象中。Connection con = null;//Connection==>与特定数据库的连接try {//注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//这里的3306/后跟的是数据库名//获取数据库连接String url = "jdbc:mysql://localhost:3306/students?serverTimezone=GMT%2B8";//通过DriverManager完成注册con = DriverManager.getConnection(url, "root", "123");//执行SQL语句String sql = "insert into student_status(class,studentnumber,name,sex,date) values(?,?,?,?,?)";ps = con.prepareStatement(sql);if (f1.getText().length() != 0) {ps.setString(1, f1.getText());} else {JOptionPane.showMessageDialog(null, "班级不能为空!");//弹出窗口}if (f2.getText().length() != 0) {ps.setString(2, f2.getText());} else {JOptionPane.showMessageDialog(null, "学号不能为空!");//弹出窗口}if (f3.getText().length() != 0) {ps.setString(3, f3.getText());} else {JOptionPane.showMessageDialog(null, "姓名不能为空!");//弹出窗口}if (f4.getText().length() != 0) {ps.setString(4, f4.getText());} else {JOptionPane.showMessageDialog(null, "性别不能为空!");//弹出窗口}if (f5.getText().length() != 0) {ps.setString(5, f5.getText());} else {JOptionPane.showMessageDialog(null, "出生年月不能为空!");//弹出窗口}} catch (ClassNotFoundException ex) {ex.printStackTrace();} catch (SQLException ex) {ex.printStackTrace();}if (f1.getText().length() != 0 && f2.getText().length() != 0 && f3.getText().length() != 0&& f4.getText().length() != 0&& f5.getText().length() != 0) {//如果所有信息都填写了,便添加成功JOptionPane.showMessageDialog(null, "添加成功!");f1.setText(null);f2.setText(null);f3.setText(null);f4.setText(null);f5.setText(null);try {int i = ps.executeUpdate();//将数据存储到数据库中} catch (SQLException ex) {ex.printStackTrace();}}}});jb2 = new JButton("返回");jb2.setBounds(35, 325, 80, 40);jb2.setFont(font);jb2.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//返回到教师学籍管理页面new txj();jf.dispose();//关闭窗体,释放所有资源}});jb3 = new JButton("退出");jb3.setBounds(190, 325, 80, 40);jb3.setFont(font);jb3.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {JOptionPane.showMessageDialog(null, "退出成功!");//系统退出System.exit(0);}});jf.add(l1);jf.add(f1);jf.add(l2);jf.add(f2);jf.add(l3);jf.add(f3);jf.add(l4);jf.add(f4);jf.add(l5);jf.add(f5);jf.add(jb1);jf.add(jb2);jf.add(jb3);jf.setVisible(true);}}

 修改学籍功能:

import javax.swing.*;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.sql.*;public class 修改学籍功能 {public static void main(String[] args) {new txgxj();}}class txgxj{JFrame jf;JLabel l1,l2,l3,l4,l5;JTextField f1,f2,f3,f4,f5;JButton jb1,jb2,jb3;public txgxj(){jf = new JFrame("修改学籍");jf.setSize(320,450);//设置窗体大小jf.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLayout(null);Font font = new Font("仿宋", Font.BOLD, 20);//设置统一的字体,让代码更加整洁美观l1 = new JLabel("班级:");l1.setBounds(10,10,100,40);l1.setFont(font);f1 = new JTextField(null,20);f1.setBounds(90,15,180,30);f1.setFont(font);l2 = new JLabel("学号:");l2.setBounds(8, 50, 100, 40);l2.setFont(font);f2=new JTextField(null,20);f2.setBounds(90,55,180,30);f2.setFont(font);l3 = new JLabel("姓名:");l3.setBounds(8, 90, 100, 40);l3.setFont(font);f3=new JTextField(null,20);f3.setBounds(90,95,180,30);f3.setFont(font);l4 = new JLabel("性别:");l4.setBounds(8, 130, 100, 40);l4.setFont(font);f4=new JTextField(null,20);f4.setBounds(90,135,180,30);f4.setFont(font);l5 = new JLabel("出生年月:");l5.setBounds(8, 170, 130, 40);l5.setFont(font);f5=new JTextField(null,20);f5.setBounds(90,210,180,30);f5.setFont(font);jb1 = new JButton("修改学籍信息");jb1.setBounds(65, 265, 180, 40);jb1.setFont(font);jb1.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {PreparedStatement ps = null;//PreparedStatement==>表示预编译的SQL语句的对象。// SQL语句已预编译并存储在PreparedStatement对象中。Connection con = null;//Connection==>与特定数据库的连接Statement st=null;ResultSet rs=null;try {//注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//这里的3306/后跟的是数据库名//获取数据库连接String url = "jdbc:mysql://localhost:3306/students?serverTimezone=GMT%2B8";//通过DriverManager完成注册con = DriverManager.getConnection(url, "root", "123");//执行SQL语句String sql2="update student_status set name=?,sex=?,date=? where class=? and studentnumber=?";ps = con.prepareStatement(sql2);String sql = "select * from student_status";st=con.createStatement();rs=st.executeQuery(sql);int flag=0;while (rs.next()){if(rs.getString("class").equals(f1.getText())&&rs.getString("studentnumber").equals(f2.getText())){ps.setString(1,f3.getText());ps.setString(2,f4.getText());ps.setString(3,f5.getText());ps.setString(4,f1.getText());ps.setString(5,f2.getText());flag++;}}if(flag==0){JOptionPane.showMessageDialog(null, "没有找到相应的班级和学生!");}if(f1.getText().length()!=0&&f2.getText().length()!=0&&f3.getText().length()!=0&&f4.getText().length()!=0&&f5.getText().length()!=0 && flag!=0){JOptionPane.showMessageDialog(null, "修改成功!");f1.setText(null);f2.setText(null);f3.setText(null);f4.setText(null);f5.setText(null);try {int i = ps.executeUpdate();//将注册的账户存储到数据库中} catch (SQLException ex) {ex.printStackTrace();}}else{JOptionPane.showMessageDialog(null, "信息填写不完整,修改失败!");}} catch (ClassNotFoundException ex) {ex.printStackTrace();} catch (SQLException ex) {ex.printStackTrace();}if (f1.getText().length() != 0 && f2.getText().length() != 0 && f3.getText().length() != 0&& f4.getText().length() != 0&& f5.getText().length() != 0) {//如果所有信息都填写了,便添加成功JOptionPane.showMessageDialog(null, "修改成功!");f1.setText(null);f2.setText(null);f3.setText(null);f4.setText(null);f5.setText(null);try {int i = ps.executeUpdate();//将数据存储到数据库中} catch (SQLException ex) {ex.printStackTrace();}}}});jb2 = new JButton("返回");jb2.setBounds(35, 325, 80, 40);jb2.setFont(font);jb2.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//返回到教师学籍管理页面new txj();jf.dispose();//关闭窗体,释放所有资源}});jb3 = new JButton("退出");jb3.setBounds(190, 325, 80, 40);jb3.setFont(font);jb3.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {JOptionPane.showMessageDialog(null, "退出成功!");//系统退出System.exit(0);}});jf.add(l1);jf.add(f1);jf.add(l2);jf.add(f2);jf.add(l3);jf.add(f3);jf.add(l4);jf.add(f4);jf.add(l5);jf.add(f5);jf.add(jb1);jf.add(jb2);jf.add(jb3);jf.setVisible(true);}}

 查询学籍功能:

import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.sql.*;public class 教师查询学籍功能 {public static void main(String[] args) {new txjcx();}}class txjcx{//文本域private JTextArea jta;//滚动条private JScrollPane jsp;//面板private JPanel jp;//按钮private JButton jb,jb2;//窗体JFrame jf;public txjcx(){//初始化组件jta=new JTextArea();jf = new JFrame("查询学籍");//将文本域添加到滚动条中,实现滚动效果jsp=new JScrollPane(jta);//面板jp=new JPanel();jb=new JButton("查询");//将文本框和按钮添加到面板中jb2=new JButton("返回");jb2.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {new txj();jf.dispose();//关闭窗体,释放所有资源}});jp.add(jb);jp.add(jb2);//将滚动条和面板都添加到窗体中jf.add(jsp, BorderLayout.CENTER);jf.add(jp,BorderLayout.SOUTH);jf.setSize(400,300);jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLocationRelativeTo(null);jf.setVisible(true);//设置组件显示//给查询按钮绑定一个监听点击事件jb.addActionListener(new AbstractAction() {@Overridepublic void actionPerformed(ActionEvent e) {Statement st=null;Connection con=null;ResultSet rs=null;try {//注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//这里的3306/后跟的是数据库名//获取数据库连接String url = "jdbc:mysql://localhost:3306/students?serverTimezone=GMT%2B8";//通过DriverManager完成注册con = DriverManager.getConnection(url, "root", "123");//执行SQL语句String sql = "select * from student_status";st=con.createStatement();rs=st.executeQuery(sql);String text;while (rs.next()){text = "班级:"+rs.getString(1) + "\n" +"学号:"+ rs.getString(2) + "\n" +"姓名:"+ rs.getString(3) +"\n" +"性别:"+ rs.getString(4) + "\n" +"出生年月:"+ rs.getString(5)+"\n\n";jta.append(text);}} catch (ClassNotFoundException ex) {ex.printStackTrace();} catch (SQLException ex) {ex.printStackTrace();}}});}}

 删除学籍功能:

import javax.swing.*;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;public class 删除学籍功能 {public static void main(String[] args) {new scxj();}}class scxj{JFrame jf;JLabel l1,l2;JTextField f1,f2;JButton jb1,jb2,jb3;public scxj(){jf = new JFrame("删除学籍");jf.setSize(320,300);//设置窗体大小jf.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLayout(null);Font font = new Font("仿宋", Font.BOLD, 20);//设置统一的字体,让代码更加整洁美观l1 = new JLabel("学号:");l1.setBounds(10,10,100,40);l1.setFont(font);f1 = new JTextField(null,20);f1.setBounds(90,15,180,30);f1.setFont(font);l2 = new JLabel("姓名:");l2.setBounds(8, 50, 100, 40);l2.setFont(font);f2=new JTextField(null,20);f2.setBounds(90,55,180,30);f2.setFont(font);jb1 = new JButton("删除学籍信息");jb1.setBounds(65, 105, 180, 40);jb1.setFont(font);jb1.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {PreparedStatement ps = null;//PreparedStatement==>表示预编译的SQL语句的对象。// SQL语句已预编译并存储在PreparedStatement对象中。Connection con = null;//Connection==>与特定数据库的连接try {//注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//这里的3306/后跟的是数据库名//获取数据库连接String url = "jdbc:mysql://localhost:3306/students?serverTimezone=GMT%2B8";//通过DriverManager完成注册con = DriverManager.getConnection(url, "root", "123");//执行SQL语句String sql = "delete from student_status where studentnumber=? and name=?";ps = con.prepareStatement(sql);if(f1.getText().length()!=0){ps.setString(1,f1.getText());}else{JOptionPane.showMessageDialog(null, "学号不能为空!");}if(f2.getText().length()!=0){ps.setString(2,f2.getText());}else{JOptionPane.showMessageDialog(null, "姓名不能为空!");}if(f1.getText().length()!=0&&f2.getText().length()!=0){JOptionPane.showMessageDialog(null, "删除成功!");f1.setText(null);f2.setText(null);int i = ps.executeUpdate();//将数据存储到数据库中}else{JOptionPane.showMessageDialog(null, "删除失败!");}} catch (ClassNotFoundException ex) {ex.printStackTrace();} catch (SQLException ex) {ex.printStackTrace();}}});jb2 = new JButton("返回");jb2.setBounds(35, 165, 80, 40);jb2.setFont(font);jb2.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//返回到教师学籍管理页面new txj();jf.dispose();//关闭窗体,释放所有资源}});jb3 = new JButton("退出");jb3.setBounds(190, 165, 80, 40);jb3.setFont(font);jb3.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {JOptionPane.showMessageDialog(null, "退出成功!");//系统退出System.exit(0);}});//将这些按钮和文本等加入到窗体中jf.add(l1);jf.add(f1);jf.add(l2);jf.add(f2);jf.add(jb1);jf.add(jb2);jf.add(jb3);jf.setVisible(true);//让组件显示}}

 课程管理:

import javax.swing.*;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;public class 教师课程管理 {public static void main(String[] args) {new tkc();}}class tkc{JFrame jf;JButton jb1,jb2,jb3,jb4,jb5,jb6;public tkc(){jf = new JFrame("课程管理");jf.setSize(320,350);//设置窗体大小jf.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLayout(null);Font font = new Font("仿宋", Font.BOLD, 20);//设置统一的字体,让代码更加整洁美观jb1 = new JButton("添加课程信息");jb1.setBounds(55, 20, 200, 40);jb1.setFont(font);jb1.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//跳转到教师添加课程页面new ttj();jf.dispose();//关闭窗体,释放所有资源}});jb2 = new JButton("修改课程信息");jb2.setBounds(55, 75, 200, 40);jb2.setFont(font);jb2.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//跳转到教师修改课程页面new txgkc();jf.dispose();//关闭窗体,释放所有资源}});jb3 = new JButton("查询课程信息");jb3.setBounds(55, 125, 200, 40);jb3.setFont(font);jb3.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//跳转到教师查询课程页面 new tcxkc(); jf.dispose();//关闭窗体,释放所有资源}});jb6=new JButton("删除课程信息");jb6.setBounds(55,175,200,40);jb6.setFont(font);jb6.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//跳转到教师删除课程页面new sckc();jf.dispose();//关闭窗体,释放所有资源}});jb4 = new JButton("返回");jb4.setBounds(30, 245, 80, 40);jb4.setFont(font);jb4.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//返回教师主页面new teacher();jf.dispose();//关闭窗体,释放所有资源}});jb5 = new JButton("退出");jb5.setBounds(200, 245, 80, 40);jb5.setFont(font);jb5.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {JOptionPane.showMessageDialog(null, "退出成功!");//系统退出System.exit(0);}});//将这些按钮和文本等加入到窗体中jf.add(jb1);jf.add(jb2);jf.add(jb3);jf.add(jb4);jf.add(jb5);jf.add(jb6);jf.setVisible(true);//让组件显示}}

 添加课程功能:

import javax.swing.*;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;public class 添加课程功能 {public static void main(String[] args) {new ttj();}}class ttj{JFrame jf;JLabel l1,l2;JTextField f1,f2;JButton jb1,jb2,jb3;public ttj(){jf = new JFrame("添加课程");jf.setSize(320,300);//设置窗体大小jf.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLayout(null);Font font = new Font("仿宋", Font.BOLD, 20);//设置统一的字体,让代码更加整洁美观l1 = new JLabel("课程名:");l1.setBounds(10,10,100,40);l1.setFont(font);f1 = new JTextField(null,20);f1.setBounds(90,15,180,30);f1.setFont(font);l2 = new JLabel("学 分:");l2.setBounds(10,50,100,40);l2.setFont(font);f2 = new JTextField(null,20);f2.setBounds(90,55,180,30);f2.setFont(font);jb1 = new JButton("添加");jb1.setBounds(105, 110, 80, 40);jb1.setFont(font);jb1.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {PreparedStatement ps = null;//PreparedStatement==>表示预编译的SQL语句的对象。// SQL语句已预编译并存储在PreparedStatement对象中。Connection con = null;//Connection==>与特定数据库的连接try {//注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//这里的3306/后跟的是数据库名//获取数据库连接String url = "jdbc:mysql://localhost:3306/students?serverTimezone=GMT%2B8";//通过DriverManager完成注册con = DriverManager.getConnection(url, "root", "123");//执行SQL语句String sql = "insert into kecheng(course,xuefen) values(?,?)";ps = con.prepareStatement(sql);if(f1.getText().length()!=0){//注:这里的所有getText()都不能写成!=nullps.setString(1, f1.getText());}else{JOptionPane.showMessageDialog(null, "课程名不能为空!");//弹出窗口}if(f2.getText().length()!=0) {ps.setString(2, f2.getText());}else{JOptionPane.showMessageDialog(null, "学分不能为空!");}if(f1.getText().length()!=0&&f2.getText().length()!=0){JOptionPane.showMessageDialog(null, "添加成功!");f1.setText(null);f2.setText(null);try {int i = ps.executeUpdate();//将数据存储到数据库中} catch (SQLException ex) {ex.printStackTrace();}}} catch (ClassNotFoundException ex) {ex.printStackTrace();} catch (SQLException ex) {ex.printStackTrace();}}});jb2 = new JButton("退出");jb2.setBounds(180, 160, 80, 40);//匿名内部类jb2.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {JOptionPane.showMessageDialog(null, "退出成功!");//系统退出System.exit(0);}});jb2.setFont(font);jb3=new JButton("返回");jb3.setBounds(40,160,80,40);jb3.setFont(font);jb3.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//跳转到教师的课程管理页面new tkc();jf.dispose();//关闭窗体,释放所有资源}});//将这些按钮和文本等加入到窗体中jf.add(f1);jf.add(l1);jf.add(l2);jf.add(f2);jf.add(jb1);jf.add(jb2);jf.add(jb3);jf.setVisible(true);//让组件显示}}

 修改课程功能:

import javax.swing.*;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.sql.*;public class 修改课程功能 {public static void main(String[] args) {new txgkc();}}class txgkc{JFrame jf;JLabel l1,l2;JTextField f1,f2;JButton jb1,jb2,jb3;public txgkc(){jf = new JFrame("修改课程");jf.setSize(320,300);//设置窗体大小jf.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLayout(null);Font font = new Font("仿宋", Font.BOLD, 20);//设置统一的字体,让代码更加整洁美观l1 = new JLabel("课程名:");l1.setBounds(10,10,100,40);l1.setFont(font);f1 = new JTextField(null,20);f1.setBounds(90,15,180,30);f1.setFont(font);l2 = new JLabel("学 分:");l2.setBounds(10,50,100,40);l2.setFont(font);f2 = new JTextField(null,20);f2.setBounds(90,55,180,30);f2.setFont(font);jb1 = new JButton("修改");jb1.setBounds(105, 110, 80, 40);jb1.setFont(font);jb1.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {PreparedStatement ps = null;//PreparedStatement==>表示预编译的SQL语句的对象。// SQL语句已预编译并存储在PreparedStatement对象中。Connection con = null;//Connection==>与特定数据库的连接Statement st=null;ResultSet rs=null;try {//注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//这里的3306/后跟的是数据库名//获取数据库连接String url = "jdbc:mysql://localhost:3306/students?serverTimezone=GMT%2B8";//通过DriverManager完成注册con = DriverManager.getConnection(url, "root", "123");//执行SQL语句String sql2="update kecheng set xuefen=? where course=?";ps = con.prepareStatement(sql2);String sql = "select * from kecheng";st=con.createStatement();rs=st.executeQuery(sql);int flag=0;while (rs.next()){if(rs.getString("course").equals(f1.getText())){//根据课程名修改成绩ps.setString(1,f2.getText());ps.setString(2,f1.getText());flag++;}}if(flag==0){JOptionPane.showMessageDialog(null, "没有该课程!");}if(f1.getText().length()!=0&&f2.getText().length()!=0 && flag!=0){//如果课程名和成绩都填写了JOptionPane.showMessageDialog(null, "修改成功!");f1.setText(null);f2.setText(null);try {int i = ps.executeUpdate();//将数据存储到数据库中} catch (SQLException ex) {ex.printStackTrace();}}else{JOptionPane.showMessageDialog(null, "修改失败!");}} catch (ClassNotFoundException ex) {ex.printStackTrace();} catch (SQLException ex) {ex.printStackTrace();}}});jb2 = new JButton("返回");jb2.setBounds(40,160,80,40);jb2.setFont(font);jb2.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//返回到教师课程管理页面new tkc();jf.dispose();//关闭窗体,释放所有资源}});jb3 = new JButton("退出");jb3.setBounds(180, 160, 80, 40);jb3.setFont(font);jb3.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {JOptionPane.showMessageDialog(null, "退出成功!");//系统退出System.exit(0);}});//将这些按钮和文本等加入到窗体中jf.add(l1);jf.add(f1);jf.add(l2);jf.add(f2);jf.add(jb1);jf.add(jb2);jf.add(jb3);jf.setVisible(true);//让组件显示}}

 查询课程功能:

import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.sql.*;public class 教师查询课程功能 {public static void main(String[] args) {new tcxkc();}}class tcxkc{//文本域private JTextArea jta;//滚动条private JScrollPane jsp;//面板private JPanel jp;//按钮private JButton jb,jb2;//窗体JFrame jf;public tcxkc() {//初始化组件jta = new JTextArea();jf = new JFrame("查询课程");//将文本域添加到滚动条中,实现滚动效果jsp = new JScrollPane(jta);//面板jp = new JPanel();jb = new JButton("查询");//将文本框和按钮添加到面板中jb2 = new JButton("返回");jb2.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//返回教师课程管理页面new tkc();jf.dispose();//关闭窗体,释放所有资源}});jp.add(jb);jp.add(jb2);//将滚动条和面板都添加到窗体中jf.add(jsp, BorderLayout.CENTER);jf.add(jp, BorderLayout.SOUTH);jf.setSize(400, 300);jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLocationRelativeTo(null);jf.setVisible(true);//设置组件显示//给查询按钮绑定一个监听点击事件jb.addActionListener(new AbstractAction() {@Overridepublic void actionPerformed(ActionEvent e) {Statement st = null;Connection con = null;ResultSet rs = null;try {//注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//这里的3306/后跟的是数据库名//获取数据库连接String url = "jdbc:mysql://localhost:3306/students?serverTimezone=GMT%2B8";//通过DriverManager完成注册con = DriverManager.getConnection(url, "root", "123");//执行SQL语句String sql = "select * from kecheng";st = con.createStatement();rs = st.executeQuery(sql);String text;while (rs.next()) {text = "课程名:" + rs.getString(1) + "\n" + "学分:" + rs.getString(2) + "\n";jta.append(text);}} catch (ClassNotFoundException ex) {ex.printStackTrace();} catch (SQLException ex) {ex.printStackTrace();}}});}}

 删除课程功能:

import javax.swing.*;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;public class 删除课程功能 {public static void main(String[] args) {new sckc();}}class sckc{JFrame jf;JLabel l1;JTextField f1;JButton jb1,jb2,jb3;public sckc(){jf = new JFrame("删除课程");jf.setSize(320,280);//设置窗体大小jf.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLayout(null);Font font = new Font("仿宋", Font.BOLD, 20);//设置统一的字体,让代码更加整洁美观l1 = new JLabel("课程名:");l1.setBounds(10,10,100,40);l1.setFont(font);f1 = new JTextField(null,20);f1.setBounds(90,15,180,30);f1.setFont(font);jb1 = new JButton("删除课程信息");jb1.setBounds(65, 65, 180, 40);jb1.setFont(font);jb1.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {PreparedStatement ps = null;//PreparedStatement==>表示预编译的SQL语句的对象。// SQL语句已预编译并存储在PreparedStatement对象中。Connection con = null;//Connection==>与特定数据库的连接try {//注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//这里的3306/后跟的是数据库名//获取数据库连接String url = "jdbc:mysql://localhost:3306/students?serverTimezone=GMT%2B8";//通过DriverManager完成注册con = DriverManager.getConnection(url, "root", "123");//执行SQL语句String sql = "delete from kecheng where course=?";ps = con.prepareStatement(sql);if(f1.getText().length()!=0){ps.setString(1,f1.getText());}else{JOptionPane.showMessageDialog(null, "课程名不能为空!");}if(f1.getText().length()!=0){JOptionPane.showMessageDialog(null, "删除成功!");f1.setText(null);int i = ps.executeUpdate();//将数据存储到数据库中}else{JOptionPane.showMessageDialog(null, "删除失败!");}} catch (ClassNotFoundException ex) {ex.printStackTrace();} catch (SQLException ex) {ex.printStackTrace();}}});jb2 = new JButton("返回");jb2.setBounds(40,125,80,40);jb2.setFont(font);jb2.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {//返回到教师课程管理页面new tkc();jf.dispose();//关闭窗体,释放所有资源}});jb3 = new JButton("退出");jb3.setBounds(180, 125, 80, 40);jb3.setFont(font);jb3.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {JOptionPane.showMessageDialog(null, "退出成功!");//系统退出System.exit(0);}});//将这些按钮和文本等加入到窗体中jf.add(l1);jf.add(f1);jf.add(jb1);jf.add(jb2);jf.add(jb3);jf.setVisible(true);//让组件显示}}

 缺陷:外观并不美观。查询页面应用表格的形式显示,不应该用文本域的形式显示。数据库表与表之间并无关联等。