本学生成绩管理系统具有录入学生成绩、查询学生成绩、输出学 生按成绩的排名、输出学科的分数四个功能,其中后两个功能在“输出成绩”这一目录下。 此系统可以实现学生成绩管理的一些基本操作。
录入成绩
输入若干同学的学号、姓名以及四个科目的成绩(应用数学、大学英语、Java 程序设计、计算机应用基础),并将其保存在建立好的数据库中。
查询成绩
进入该模块后,输入想要查询成绩的学生姓名,即可在数据库中检索该学生 的成绩信息并输出其各科成绩。
输出成绩
该模块主要分为两部分,包括学生排名和各科目平均成绩及各科的最高分和 最低分。
(1)能够计算出平均成绩,以平均成绩降序输出成绩表。
(2)输出全组各科平均分,最高分和最低分。
数据库表的设计
本系统将数据存储在一张表中,这张表名称为:students,能够保存学生的基本信息,包括学生的姓名、学号、应用数学成绩、大学英语成绩、Java 程序 设计成绩、计算机应用基础成绩、总成绩、平均成绩。
该表中 name 和 num 栏指 定的类型为 varchar 型,各科成绩的输入数据类型为 float 型。
建表时命令行输入为:
mysql> create table students(
-> name varchar(50) primary key,
-> num varchar(20),
-> math float(5,2),
-> English float(5,2),
-> Java float(5,2),
-> computer float(5,2),
-> score float(5,2),
-> average float(5,2)
-> );
package kechengsheji; import kechengsheji.Outnum; import java.util.Scanner; import kechengsheji.*; public class Main { public Main() { Scanner in=new Scanner(System.in); int a=4; //a不等于0,进入循环 while(a!=0) { System.out.println(" +++++++++++++++++++++++"); System.out.println(" + 0 退出 +"); System.out.println(" + 1 录入成绩 +"); System.out.println(" + 2 查询成绩 +"); System.out.println(" + 3 输出成绩 +"); System.out.println("请输入0-3:"); a=in.nextInt(); switch(a) { case 0: System.out.println("退出成功!"); break; case 1: new Input(); break; case 2: new Seeknum(); break; case 3: new Outnum(); break; default: System.out.println("输入有误!"); break; } } } public static void main(String[] args) { new Main(); } } package kechengsheji; import java.sql.*; public class SQL { Connection conn; public Connection getConn() { try { Class.forName("com.mysql.cj.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/swy" + "?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false"; //JDBC的URL conn = DriverManager.getConnection(url, "root","266531"); //建立数据库连接,获得连接对象conn }catch(ClassNotFoundException e){ e.printStackTrace(); }catch (SQLException e) { e.printStackTrace(); } return conn; } public static void main(String[] args) { System.out.println("hello world!"); } } package kechengsheji; import java.util.Scanner; import java.sql.*; import kechengsheji.SQL; public class Input { private String b,c; private double d,e,f,g; private double sum,aver; Scanner in=new Scanner(System.in); private int a=2; Input(){ while(a!=0) { System.out.println("+++++++++++++++++++++"); System.out.println("+ 1 录入成绩 +"); System.out.println("+ 0 返回 +"); a=in.nextInt(); switch (a) { case 0: System.out.println("返回成功!"); break; case 1: System.out.println("请输入学生姓名:"); b=in.next(); System.out.println("请输入学生学号:"); c=in.next(); System.out.println("请输入学生应用数学成绩:"); d=in.nextDouble(); System.out.println("请输入学生大学英语成绩:"); e=in.nextDouble(); System.out.println("请输入学生Java程序设计成绩:"); f=in.nextDouble(); System.out.println("请输入学生计算机应用基础成绩:"); g=in.nextDouble(); sum=d+e+f+g; //总成绩 aver=sum/4; //平均成绩 //数据库 SQL bd=new SQL(); Connection conn=bd.getConn(); try { Statement stmt = conn.createStatement(); //创建Statement对象 try { String sql = "insert into students values(?,?,?,?,?,?,?,?)"; //要执行的SQL PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1,b); //传入带占位符的SQL语句 pst.setString(2,c); pst.setDouble(3,d); pst.setDouble(4,e); pst.setDouble(5,f); pst.setDouble(6,g); pst.setDouble(7,sum); pst.setDouble(8,aver); pst.executeUpdate(); //执行 update和insert、delete等sql语句 System.out.println("保存成功!"); pst.close(); }catch(SQLIntegrityConstraintViolationException e) { System.out.println("该学生已存在!"); } stmt.close(); conn.close(); }catch(SQLException e){ e.printStackTrace(); } //数据库 break; default: System.out.println("输入错误!"); } } } public static void main(String[] args) { new Input(); } } package kechengsheji; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; import kechengsheji.SQL;; public class Seeknum { private String num,nam; int i=0; Seeknum(){ Scanner s1 =new Scanner(System.in); int a = 3; while(a!=0){ System.out.println("*********************");//查找界面并选择 System.out.println("* 0 返回 *"); System.out.println("* 1 查询成绩 *"); System.out.println("请输入0或1:"); a =s1.nextInt(); switch (a) { case 0: System.out.println("返回成功!"); break; case 1: //数据库 SQL bd=new SQL(); Connection conn=bd.getConn(); try { System.out.println("请输入姓名:"); nam=s1.next(); Statement stmt = conn.createStatement(); //创建Statement对象 String sql = "select * from students;"; //要执行的SQL ResultSet rs = stmt.executeQuery(sql);//创建数据对象,产生单个结果集的语句 while(rs.next()) { if(rs.getString("name").equals(nam)) { i=1; System.out.println("成绩如下:"); //从数据库查找成绩 System.out.println(); System.out.println("姓名:"+rs.getString(1) ); System.out.println("学号:" +rs.getString(2) ); System.out.println("应用数学:" +rs.getDouble(3) ); System.out.println("大学英语 :" +rs.getDouble(4) ); System.out.println("java程序设计:" +rs.getDouble(5) ); System.out.println("计算机应用基础:" +rs.getDouble(6) ); System.out.println("总分:" +rs.getDouble(7) ); System.out.println("平均分:" +rs.getDouble(8) ); System.out.println(); //数据库 break; } } if(i==0) System.out.println("暂无该学生成绩!"); rs.close(); stmt.close(); conn.close(); }catch (SQLException e) { e.printStackTrace(); } break; default: System.out.println("输入有误!"); break; } } } public static void main(String[] args){ new Seeknum(); } } package kechengsheji; import kechengsheji.paiming; import kechengsheji.kemuchengji; import java.util.Scanner; import java.sql.*; public class Outnum { Scanner in=new Scanner(System.in); private int a=3; Outnum(){ while(a!=0){ System.out.println("+++++++++++++++++++"); System.out.println("+ 1 排名 +"); System.out.println("+ 2 科目成绩 +"); System.out.println("+ 0 返回 +"); System.out.println("请输入0-2:"); a=in.nextInt(); switch(a) { case 0: System.out.println("返回成功!"); System.out.println(); break; case 1: new paiming(); break; case 2: new kemuchengji(); break; default: System.out.println("输入有误!"); } } } public static void main(String[] args) { new Outnum(); } } package kechengsheji; import java.sql.*; public class paiming { SQL bd=new SQL(); Connection conn=bd.getConn(); private int a=1; paiming(){ try { //数据库 Statement stmt; stmt = conn.createStatement(); String sql="select name,num,average from students order by average desc;"; ResultSet rs = stmt.executeQuery(sql); System.out.println(); System.out.println("排名"+" "+"姓名"+" "+" 学号"+" "+"平均成绩"); while(rs.next()) { System.out.println(a+" "+rs.getString("name")+" "+rs.getString("num")+" "+rs.getDouble("average")); a++; } rs.close(); stmt.close(); conn.close(); }catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args) { new paiming(); } } package kechengsheji; import java.sql.*; import kechengsheji.SQL; public class kemuchengji { private double sum,max,min; SQL bd=new SQL(); Connection conn=bd.getConn(); kemuchengji(){ System.out.println(); System.out.println(" "+"平均成绩"+" "+"最高成绩"+" "+"最低成绩"); try { Statement stmt = conn.createStatement(); //创建Statement对象 int i=0; String sql = "select math from students order by math desc;"; //要执行的SQL ResultSet rs = stmt.executeQuery(sql); while(rs.next()) { sum=sum+rs.getDouble("math"); i++; if(rs.isFirst()) max=rs.getDouble("math"); if(rs.isLast()) min=rs.getDouble("math"); } sum=sum/i;//平均成绩 System.out.println("应用数学成绩: "+String.format("%.2f", sum)+" "+max+" "+min); i=0; sum=0.0; String sql1 = "select English from students order by English desc;"; //要执行的SQL ResultSet rs1 = stmt.executeQuery(sql1); while(rs1.next()) { sum=sum+rs1.getDouble("English"); i++; if(rs1.isFirst()) max=rs1.getDouble("English"); if(rs1.isLast()) min=rs1.getDouble("English"); } sum=sum/i; //平均成绩 System.out.println("大学英语成绩: "+String.format("%.2f", sum)+" "+max+" "+min); i=0; sum=0.0; //数据库 String sql2 = "select Java from students order by Java desc;"; //要执行的SQL ResultSet rs2 = stmt.executeQuery(sql2); while(rs2.next()) { sum=sum+rs2.getDouble("Java"); i++; if(rs2.isFirst()) max=rs2.getDouble("Java"); if(rs2.isLast()) min=rs2.getDouble("Java"); } sum=sum/i;//平均成绩 System.out.println("Java程序设计成绩: "+String.format("%.2f", sum)+" "+max+" "+min); i=0; sum=0.0; String sql3 = "select computer from students order by computer desc;"; //要执行的SQL ResultSet rs3 = stmt.executeQuery(sql3); while(rs3.next()) { sum=sum+rs3.getDouble("computer"); i++; if(rs3.isFirst()) max=rs3.getDouble("computer"); if(rs3.isLast()) min=rs3.getDouble("computer"); } sum=sum/i; //平均成绩 System.out.println("计算机应用基础成绩:"+String.format("%.2f", sum)+" "+max+" "+min); System.out.println(); rs.close(); rs1.close(); rs2.close(); rs3.close(); stmt.close(); conn.close(); }catch(SQLException e) { e.printStackTrace(); } } public static void main(String[] args) { new kemuchengji(); } }
源代码
package kechengsheji; import java.awt.*; import java.awt.event.*; import javax.sql.rowset.serial.SerialArray; public class GUImain extends Frame implements ActionListener{ private Button b1,b2,b3,b4; private Label a1,a2; private GridBagLayout gb; private GridBagConstraints gbc; private GUImain(){ a1=new Label(" 欢迎使用学生成绩管理系统"); a1.setFont(new Font(null,Font.LAYOUT_RIGHT_TO_LEFT,20)); a2=new Label(" "); gb=new GridBagLayout(); //初始化 gb setLayout(gb); //设置窗口布局管理器 gb gbc=new GridBagConstraints(); //初始化网格包容器 b1=new Button("输入成绩"); //初始化按钮 btn1 b2=new Button("查询成绩"); b3=new Button("输出成绩"); b4=new Button("退出系统"); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ setVisible(false); dispose(); System.exit(0); //程序退出 } }); gbc.fill=GridBagConstraints.HORIZONTAL; //设置 gbc 的 fill 域 addComponent(a1,0, 2, 1, 4); addComponent(a2,4, 2, 1, 2); addComponent(b1,6, 2, 1, 2); addComponent(b2,8, 2, 1, 2); addComponent(b3,10, 2, 1, 2); addComponent(b4,12, 2, 1, 2); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand()=="输入成绩") { GUIinput mygb =new GUIinput(); mygb.setSize(600,400); mygb.setVisible(true); }else if(e.getActionCommand()=="查询成绩") { GUISeeknum mygb =new GUISeeknum(); mygb.setSize(600,400); mygb.setVisible(true); }else if(e.getActionCommand()=="输出成绩") { GUIOutnum mygb=new GUIOutnum(); mygb.setSize(700,500); mygb.setVisible(true); }else if(e.getActionCommand()=="退出系统") { setVisible(false); dispose(); System.exit(0); } } public void addComponent(Component c,int row,int col, int nrow,int ncol){ gbc.gridx=col; //设置组件显示区域的开始边单元格 gbc.gridy=row; //设置组件显示区域的顶端单元格 gbc.gridheight=ncol; //设置组件显示区域一列的单元格数 gbc.gridwidth=nrow; //设置组件显示区域一行的单元格数 gb.setConstraints(c,gbc); //设置布局的约束条件 add(c); //组件 c 添加到容器中 } public static void main(String[] args) { GUImain mygb =new GUImain(); mygb.setSize(700,500); mygb.setVisible(true); } } package kechengsheji; import java.awt.*; import java.awt.event.*; import java.util.Scanner; import java.sql.*; import kechengsheji.SQL; public class GUIinput extends Frame implements ActionListener{ Frame my; private int a=0; private Label l1,l2,l3,l4,l5,l6; private TextField tf1,tf2,tf3,tf4,tf5,tf6; private Button btn1,btn2,b1; private GridBagLayout gb; private GridBagConstraints gbc; private String a1,a2; private double a3,a4,a5,a6,sum,aver; public GUIinput() { l1=new Label("姓名 "); l2=new Label("学号 "); l3=new Label("应用数学 "); l4=new Label("大学英语 "); l5=new Label("Java程序设计 "); l6=new Label("计算机应用基础"); tf1=new TextField(20); tf2=new TextField(20); tf3=new TextField(20); tf4=new TextField(20); tf5=new TextField(20); tf6=new TextField(20); gb=new GridBagLayout(); //初始化 gb setLayout(gb); //设置窗口布局管理器 gb gbc=new GridBagConstraints(); //初始化网格包容器 btn1=new Button("提交"); //初始化按钮 btn1 btn2=new Button("退出"); btn1.addActionListener(this); btn2.addActionListener(this); Panel p0 = new Panel(); //创建,并初始化面板 p1 p0.add(btn1); p0.add(btn2); Panel p1 = new Panel(); p1.add(l1); p1.add(tf1); Panel p2 = new Panel(); p2.add(l2); p2.add(tf2); Panel p3 = new Panel(); p3.add(l3); p3.add(tf3); Panel p4 = new Panel(); p4.add(l4); p4.add(tf4); Panel p5 = new Panel(); p5.add(l5); p5.add(tf5); Panel p6 = new Panel(); p6.add(l6); p6.add(tf6); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ setVisible(false); dispose(); System.exit(0); //程序退出 } }); gbc.fill=GridBagConstraints.HORIZONTAL; //设置 gbc 的 fill 域 addComponent(p1,0, 2, 1, 1); addComponent(p2,2, 2, 1, 1); addComponent(p3,3, 2, 1, 1); addComponent(p4,4, 2, 1, 1); addComponent(p5,5, 2, 1, 1); addComponent(p6,6, 2, 1, 1); addComponent(p0,9, 2, 1, 1); } //********************************************************************************8 public void gui() { my=new Frame(); my.setBounds(300,300,350,250); my.setLayout(null); my.setVisible(true); b1=new Button("返回"); b1.addActionListener(this); Label a1=new Label("该学生已存在! "); Label a2=new Label("添加成功!"); Label a3=new Label("输入为空!"); if(a==1) { my.add(a1); a1.setBounds(140,100,100,40); } else if(a==0) { my.add(a2); a2.setBounds(140,100,100,40); } else if(a==2) { my.add(a3); a3.setBounds(140,100,100,40); } my.add(b1); b1.setBounds(230,190,80,30); my.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { my.setVisible(false); my.dispose(); } }); } //****************************************************************************************************** public void actionPerformed(ActionEvent e) { if(e.getSource()==btn1) { a1=tf1.getText(); if(a1.equals("")) a=2; else { a=0; a2=tf2.getText(); a3=Double.parseDouble(tf3.getText() ); a4=Double.parseDouble(tf4.getText() ); a5=Double.parseDouble(tf5.getText() ); a6=Double.parseDouble(tf5.getText() ); sum=a3+a4+a5+a6; //总成绩 aver=sum/4; //平均成绩 SQL bd=new SQL(); Connection conn=bd.getConn(); try { Statement stmt = conn.createStatement(); //创建Statement对象 try { String sql = "insert into students values(?,?,?,?,?,?,?,?)"; //要执行的SQL PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1,a1); //传入带占位符的SQL语句 pst.setString(2,a2); pst.setDouble(3,a3); pst.setDouble(4,a4); pst.setDouble(5,a5); pst.setDouble(6,a6); pst.setDouble(7,sum); pst.setDouble(8,aver); pst.executeUpdate(); //执行 update和insert、delete等sql语句 pst.close(); }catch(SQLIntegrityConstraintViolationException e1) { a=1; } stmt.close(); conn.close(); }catch(SQLException e2){ e2.printStackTrace(); } } gui(); }else if(e.getSource()==btn2) { setVisible(false); dispose(); }else if(e.getSource()==b1) { my.setVisible(false); my.dispose(); } } //******************************************************************************************* public void addComponent(Component c,int row,int col, int nrow,int ncol){ gbc.gridx=col; //设置组件显示区域的开始边单元格 gbc.gridy=row; //设置组件显示区域的顶端单元格 gbc.gridheight=ncol; //设置组件显示区域一列的单元格数 gbc.gridwidth=nrow; //设置组件显示区域一行的单元格数 gb.setConstraints(c,gbc); //设置布局的约束条件 add(c); //组件 c 添加到容器中 } //******************************入口*********************************************************** public static void main(String[] args) { GUIinput mygb =new GUIinput(); mygb.setSize(600,400); mygb.setVisible(true); } } package kechengsheji; import java.awt.*; import java.awt.event.*; import java.sql.*; import kechengsheji.SQL; public class GUISeeknum extends Frame implements ActionListener{ Frame my; String b; private Label a1; private Button b1,b2; private TextField c1; private GridBagLayout gb; private GridBagConstraints gbc; public GUISeeknum() { a1=new Label("请输入学生姓名: "); c1=new TextField(20); gb=new GridBagLayout(); //初始化 gb setLayout(gb); //设置窗口布局管理器 gb gbc=new GridBagConstraints(); //初始化网格包容器 b1=new Button("查询"); //初始化按钮 btn1 b2=new Button("退出"); b1.addActionListener(this); b2.addActionListener(this); Panel p0 = new Panel(); //创建,并初始化面板 p1 p0.add(a1); p0.add(c1); Panel p1 = new Panel(); p1.add(b1); p1.add(b2); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ setVisible(false); dispose(); System.exit(0); //程序退出 } }); gbc.fill=GridBagConstraints.HORIZONTAL; //设置 gbc 的 fill 域 addComponent(p0,0, 2, 1, 2); addComponent(p1,4, 2, 1, 1); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand()=="查询") { b=c1.getText(); gui1(b); }else if(e.getActionCommand()=="退出") { setVisible(false); dispose(); }else if(e.getActionCommand()=="返回") { my.setVisible(false); my.dispose(); } } public void gui1(String n) { Label a0,a1,a2,a3,a4,a5,a6,a7,a8; my=new Frame(); my.setBounds(300,300,450,450); my.setLayout(null); my.setVisible(true); Button b1=new Button("返回"); b1.addActionListener(this); a0=new Label("该学生不存在! "); int a=0; SQL bd=new SQL(); Connection conn=bd.getConn(); try { Statement stmt = conn.createStatement(); //创建Statement对象 String sql = "select * from students;"; //要执行的SQL ResultSet rs = stmt.executeQuery(sql);//创建数据对象,产生单个结果集的语句 while(rs.next()) { if(rs.getString("name").equals(n)) { a=1;//有成绩 //从数据库查找成绩 a1=new Label("姓名:"+rs.getString(1)); a2=new Label("学号:"+rs.getString(2)); a3=new Label("应用数学:" +rs.getDouble(3)); a4=new Label("大学英语 :" +rs.getDouble(4)); a5=new Label("java程序设计:" +rs.getDouble(5)); a6=new Label("计算机应用基础:" +rs.getDouble(6)); a7=new Label("总分:" +rs.getDouble(7)); a8=new Label("平均分:" +rs.getDouble(8)); my.add(a1); my.add(a2); my.add(a3); my.add(a4); my.add(a5); my.add(a6); my.add(a7); my.add(a8); a1.setBounds(70,20,300,60);// 距左边的距离 距顶部的距离 长度 高度 a2.setBounds(70, 70,300,50); a3.setBounds(70, 110, 300,50); a4.setBounds(70, 150, 300,50); a5.setBounds(70, 190, 300,50); a6.setBounds(70, 230, 300,50); a7.setBounds(70, 270, 300,50); a8.setBounds(70, 310, 300,50); //数据库 break; } } if(a==0) { my.add(a0); a0.setBounds(170,180,300,60); } my.add(b1); b1.setBounds(300, 360, 100,40); my.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { my.setVisible(false); my.dispose(); } }); rs.close(); stmt.close(); conn.close(); }catch (SQLException e) { e.printStackTrace(); } } public void addComponent(Component c,int row,int col, int nrow,int ncol){ gbc.gridx=col; //设置组件显示区域的开始边单元格 gbc.gridy=row; //设置组件显示区域的顶端单元格 gbc.gridheight=ncol; //设置组件显示区域一列的单元格数 gbc.gridwidth=nrow; //设置组件显示区域一行的单元格数 gb.setConstraints(c,gbc); //设置布局的约束条件 add(c); //组件 c 添加到容器中 } public static void main(String[] args) { GUISeeknum mygb =new GUISeeknum(); mygb.setSize(600,400); mygb.setVisible(true); } } package kechengsheji; import java.awt.*; import java.awt.event.*; import java.sql.*; import kechengsheji.SQL; public class GUIOutnum extends Frame implements ActionListener{ private double sum,max,min; private String s; private TextArea m1; Frame c1,c2; private Button b1,b2,b3,b4,b5; private GridBagLayout gb; private GridBagConstraints gbc; public GUIOutnum() { gb=new GridBagLayout(); //初始化 gb setLayout(gb); //设置窗口布局管理器 gb gbc=new GridBagConstraints(); //初始化网格包容器 b1=new Button(" 总体排名 "); //初始化按钮 btn1 b2=new Button(" 科目成绩 "); b3=new Button(" 返回 "); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ setVisible(false); dispose(); System.exit(0); //程序退出 } }); gbc.fill=GridBagConstraints.HORIZONTAL;//设置 gbc 的 fill 域horizontal addComponent(b1,0, 2, 1, 2); addComponent(b2,2, 2, 1, 2); addComponent(b3,4, 2, 1, 2); } public void addComponent(Component c,int row,int col, int nrow,int ncol){ gbc.gridx=col; //设置组件显示区域的开始边单元格 gbc.gridy=row; //设置组件显示区域的顶端单元格 gbc.gridheight=ncol; //设置组件显示区域一列的单元格数 gbc.gridwidth=nrow; //设置组件显示区域一行的单元格数 gb.setConstraints(c,gbc); //设置布局的约束条件 add(c); //组件 c 添加到容器中 } public void actionPerformed(ActionEvent e) { if(e.getSource()==b1) { //*********总体排名********** c2=new Frame(); c2.setBounds(600,130,500,750); c2.setLayout(null); c2.setVisible(true); b5=new Button("返回"); b5.addActionListener(this); c2.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { c2.setVisible(false); c2.dispose(); } }); SQL bd=new SQL(); Connection conn=bd.getConn(); int a=1; try { //数据库 Statement stmt; stmt = conn.createStatement(); String sql="select name,num,average from students order by average desc;"; ResultSet rs = stmt.executeQuery(sql); s="排 名"+" "+"姓 名"+" "+" 学 号"+" "+"平 均 成 绩"+" "; while(rs.next()) { s=s+" "+" "+a+" "+rs.getString("name")+" "+rs.getString("num")+" "+rs.getDouble("average"); a++; } m1=new TextArea(s); m1.setEditable(false); m1.setBounds(50,50,400,550); c2.add(m1); b5.setBounds(380,690,80,30); c2.add(b5); rs.close(); stmt.close(); conn.close(); }catch(Exception e1){ e1.printStackTrace(); } }else if(e.getSource()==b2) { //********科目成绩*********** c1=new Frame(); c1.setBounds(300,300,450,450); c1.setLayout(null); c1.setVisible(true); Label a1,a2,a3,a4,a5; a1=new Label(" 平均成绩 最高成绩 最低成绩"); b4=new Button("返回"); b4.addActionListener(this); c1.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { c1.setVisible(false); c1.dispose(); } }); SQL bd=new SQL(); Connection conn=bd.getConn(); try { Statement stmt = conn.createStatement(); //创建Statement对象 int i=0; String sql = "select math from students order by math desc;"; //要执行的SQL ResultSet rs = stmt.executeQuery(sql); while(rs.next()) { sum=sum+rs.getDouble("math"); i++; if(rs.isFirst()) max=rs.getDouble("math"); if(rs.isLast()) min=rs.getDouble("math"); } sum=sum/i;//平均成绩 a2=new Label("应用数学成绩: "+String.format("%.2f", sum)+" "+max+" "+min); i=0; sum=0.0; String sql1 = "select English from students order by English desc;"; //要执行的SQL ResultSet rs1 = stmt.executeQuery(sql1); while(rs1.next()) { sum=sum+rs1.getDouble("English"); i++; if(rs1.isFirst()) max=rs1.getDouble("English"); if(rs1.isLast()) min=rs1.getDouble("English"); } sum=sum/i; //平均成绩 a3=new Label("大学英语成绩: "+String.format("%.2f", sum)+" "+max+" "+min); i=0; sum=0.0; //数据库 String sql2 = "select Java from students order by Java desc;"; //要执行的SQL ResultSet rs2 = stmt.executeQuery(sql2); while(rs2.next()) { sum=sum+rs2.getDouble("Java"); i++; if(rs2.isFirst()) max=rs2.getDouble("Java"); if(rs2.isLast()) min=rs2.getDouble("Java"); } sum=sum/i;//平均成绩 a4=new Label("Java程序设计成绩: "+String.format("%.2f", sum)+" "+max+" "+min); i=0; sum=0.0; String sql3 = "select computer from students order by computer desc;"; //要执行的SQL ResultSet rs3 = stmt.executeQuery(sql3); while(rs3.next()) { sum=sum+rs3.getDouble("computer"); i++; if(rs3.isFirst()) max=rs3.getDouble("computer"); if(rs3.isLast()) min=rs3.getDouble("computer"); } sum=sum/i; //平均成绩 a5=new Label("计算机应用基础成绩: "+String.format("%.2f", sum)+" "+max+" "+min); a1.setBounds(70,20,300,60);// 距左边的距离 距顶部的距离 长度 高度 a2.setBounds(10, 70,300,50); a3.setBounds(10, 110, 300,50); a4.setBounds(10, 150, 300,50); a5.setBounds(10, 190, 300,50); b4.setBounds(300, 360, 100,40); c1.add(a1); c1.add(a2); c1.add(a3); c1.add(a4); c1.add(a5); c1.add(b4); rs.close(); rs1.close(); rs2.close(); rs3.close(); stmt.close(); conn.close(); }catch(SQLException e1) { e1.printStackTrace(); } }else if(e.getSource()==b3) { setVisible(false); dispose(); }else if(e.getSource()==b4) { c1.setVisible(false); c1.dispose(); }else if(e.getSource()==b5) { c2.setVisible(false); c2.dispose(); } } public static void main(String[] args) { GUIOutnum mygb=new GUIOutnum(); mygb.setSize(700,500); mygb.setVisible(true); } } package kechengsheji; import java.sql.*; public class SQL { Connection conn; public Connection getConn() { try { Class.forName("com.mysql.cj.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/swy" + "?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false"; //JDBC的URL conn = DriverManager.getConnection(url, "root","266531"); //建立数据库连接,获得连接对象conn }catch(ClassNotFoundException e){ e.printStackTrace(); }catch (SQLException e) { e.printStackTrace(); } return conn; } public static void main(String[] args) { System.out.println("hello world!"); } }
附上源码的下载链接,由于本人是初学者,此系统是我做的课程设计,由于时间比较仓促,水平有限,仅供大家参考学习。
有疑问私聊或+qq:1663301665
源码下载链接